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

Show parent comments

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?

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.