Multiple Clients for FastAPI and MongoDb are not being handled concurrently

58 Views Asked by At

I am working an analytic web application, the backend is built on FastApi and using Mongodb. There is a aggregate operation which takes 40sec to complete, that is the best I could achieve after creating indexes, following ESR rule. the database contains 4.1 million records so that does make sense. we used fastAPI with motor driver. When I hit the endpoint from three different clients I expected all the 3 clients gets served simultaneously in parallel, but to my surprise they are being served one after the other. on the server logs everything happened in parallel till getting the cursors, when fetching data from the cursor the blocking is happening. I tried two different ways in fetching the records:

1: async for doc in cursor: data.append(doc)

2: await cursor.to_list(None)

I am running 4 workers in local, I expect each worker to pick a request, process it and return the corresponding response but that is not happening.

am I missing something? can't we serve multiple client connections simultaneously? Note: I am creating a new connection on every request so, no request is waiting for the connection to get released.

0

There are 0 best solutions below