r/FastAPI • u/Finish-Square • 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:
Users service
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:
What will be the best way to put the services together? Each service will be in a different Docker container?
When it comes to networking between the containers, is there's anything I need to consider?
In regards to the database, should have one database (say Postgres)?
If I should have only one database, how both services will be able to access it?
Thanks!
3
u/sasmariozeld Mar 07 '22
if u are working alone 99.99% of the time what you want is a monolith
1
u/vietanh2000april Oct 01 '22
I was advised the same but considering that I want to create services that are reusable so that I can just copy and paste the service folders when I start working on a new project, wouldn't microservices be more suitable?
3
u/madara_73 Feb 26 '22
Hey OP, I know this might not be of much help but please check out my repo for https://github.com/sumeetbansaloo7/microservices
1
u/catz_11 Feb 28 '26
Hi! I know it's late, but have you been able to complete this project? I'm looking to build a similiar project like this.
0
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/paymentin 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.
1
u/CrackerJackKittyCat Feb 26 '22
Yes, makes sense.
Checking the user token, from any service other than the user service, is done through making a request of the user service. The things chosen to live in the user database are the total possession and responsibility of the user service.
Some shops end up having denormalization or caching across the separate databases, reconciled and hopefully eventually consistent. Others aim to have the lowest-order services aim to be as simple and bulletproof and resilient and rolling upgradeable as possible so that they won't be observably down to the services that depend on them. And so on.
1
u/Finish-Square Feb 26 '22
Gotcha, thanks!
So, it'll look something like this: ?
def create_payment(beneficiary): api_token = request.headers['token'] # check if the API is valid by calling the users service: res = request.post(f'http://address.to/users-service/{api_token}') if res.status_code == 200: return create_payment(beneficiary) return 'Invalid API key'Also, do you have any ideas how can I have only one database that will be accessible by all services?
2
u/CrackerJackKittyCat Feb 26 '22
Sure, either explicitly call via REST like this, or implicitly interact with through message queueing tech like the other respondent answered.
If using REST, then you'll need some sort of mechanism to learn the current URLs of each subordinate service -- service discovery -- then becomes an issue. In the message queueing scenario, then lots of services can share the same messaging backplane, and just respond to different well-known topics or channels, but the machines and / or number of processes or threads responding to any particular topic can vary over time.
There's ... worlds of different microservice architectures and interaction styles.
My original response, I was mainly trying to emphasize that for purity of design, separate services, if thinking large scale and truly independent, then should not share the same database.
3
u/2readitol Feb 26 '22
I'm not an expert but maybe something like this:
Yes, each service is in a different container. You can use docker-compose to manage them.
Make sure to expose needed ports.
One database should be fine and it probably should be a separate service.
You can provide all services with the connection string to your database via environment variables.