I am running a flask application with a connection to a database using sqlalchemy. I am running 9 workers and each of the workers should have their own database engine (since each gunicorn worker runs a copy of the flask api script). While it does appear that these are running in independent workers (I have print statements that appear before the call is over which indicate to me that there are multiple workers working on calls concurrently).
Lets say calls A, B, C and D are all mostly the same with slight dissimilarities. When I run a single call -- call A -- it takes x amount of time. When I run two calls -- call A and B -- it takes x + amount of time. When I run three calls -- call A and B and C -- it takes x ++ amount of time.
I do not understand why this would be the case. Each worker is getting its own connection, and each engine is only running read requests.
app.route("/A")
def A():
stmt = select([x,y,z])
res = engine.execute(stmt)
app.route("/B")
def B():
stmt = select([x,x,z])
res = engine.execute(stmt)
app.route("/C")
def C():
stmt = select([x,y,x])
res = engine.execute(stmt)
app.route("/D")
def D():
stmt = select([z,y,z])
res = engine.execute(stmt)
I don't understand why one worker running a call would slow down another worker running a call (where the slowdown is occurring is on the database side to return the data).
After a lot of digging, turns out that the requests are concurrent. However, the teardown and set up as well as the work the database has to do to ensure that all the calls share the same information to pull from in case one does a write are leading to longer times in which multiple calls affect each other. There is no problem with parallelization, the issue lies in my view being 2 GB big and the database struggling to scan the tables and allocate resources in a short period of time