r/FastAPI • • Feb 26 '22

Question Microservices architecture with FastAPI

Hi all,

I need some advice in regards to a microservices app design.

I have started to work on a new project to practice system design/architecture.
It's going to be a very simple payments API.

I want the app to have 2 main services:

  1. Users service

  2. Payment service

The user's service will be responsible for user login and registration, and the payment service will be responsible for sending payments to other people.

The stack I am going to use: Python, FastAPI, Docker, Nginx.

My questions are:

  1. What will be the best way to put the services together? Each service will be in a different Docker container?

  2. When it comes to networking between the containers, is there's anything I need to consider?

  3. In regards to the database, should have one database (say Postgres)?

  4. If I should have only one database, how both services will be able to access it?

Thanks!

18 Upvotes

12 comments sorted by

View all comments

0

u/[deleted] Feb 26 '22

[deleted]

4

u/Finish-Square Feb 26 '22

Nice, thanks.

But what you should do when the payment service is depending on the users service?

Ex: The user is sending a POST request to /api/payment in order to create a payment.The headers consist of the API token. Behind the scenses, in the route /api/payment, there is a function that validates the user's token to check if it's valid by querying the database. However, the database is in the users service and if I get it right, not accessible from the payment service.

Am I making sense?

3

u/projosh_dev Feb 26 '22

Oftentimes in microservice architecture, you'd want to communicate between services using message/event driven mechanism.

So that instead of calling a payment API from user service, you'd want to drop the payload/data on a queue (Redis, rabbitmq) or Kafka etc and payment service would pick it up to process. This approach is common than API calling which might be inefficient, think of when Payment service is down.

If you still want to use this API calling method, you'd want user service to handle everything authentication/authorization this means when user calls payment service for payment with the token in the header, payment service would have to call user service back to verify the token (hmm, seems like too many calls here, God forbid, user service was down at that time also) and then process the payment.

So you see the message pattern is better.

3

u/Finish-Square Feb 26 '22

So that instead of calling a payment API from user service, you'd want to drop the payload/data on a queue (Redis, rabbitmq) or Kafka etc and payment service would pick it up to process. This approach is common than API calling which might be inefficient, think of when Payment service is down.

Cool! So, let me see if I got this right:
When a user is creating a payment, the payment request will be sent to the payment service which will add the request to a queue, and then will be sent to the users service in order to validate the user's API token?

Also, do you have any idea what I should look for on Google to find an example? (preferably with FastAPI/Python and docker)

1

u/AxelBlaz3 Dec 31 '22

Very nicely explained! I appreciate that.

I have a question though. So if the payments service makes use of a queue to verify the token from user service, then the user service would definitely need to respond back to the payments service (two-way communication). If we use a callback for receiving the verification in the payments service, then I guess, this callback is somewhere and asynchronous. So the request doesn't wait for the callback.

I hope you get what I'm asking for. Let me know if I'm not clear.