Why synchronous DB libraries block async FastAPI endpoints and correct SQLAlchemy usage

This tests event loop blocking: sync DB calls in async def halt all requests. Answer: sync drivers block the loop despite releasing the GIL; use asyncpg with SQLAlchemy create_async_engine and AsyncSession. Red flag: recommending run_in_executor as default.
What's really being asked
Understanding of the asyncio event loop and the distinction between concurrency and parallelism in FastAPI. The interviewer wants to know if you recognize that synchronous I/O operations block the event loop thread even if the underlying C extension releases the GIL, which destroys the concurrency model that async endpoints rely on.
The full answer
First, explain that a standard synchronous library like psycopg2 issues blocking socket calls. In an async def endpoint, this blocks the entire event loop thread, so while that one request waits for the database, no other request can be processed by that worker. Second, note that releasing the GIL is not enough; the event loop needs an await point to switch tasks, which sync code never yields. Third, describe the correct SQLAlchemy stack: use an async driver such as asyncpg, create an engine with create_async_engine, use AsyncSession from sqlalchemy.ext.asyncio, and explicitly await every query with await session.execute(). Fourth, mention that dependency injection in FastAPI should provide the async session to endpoints so that the framework can manage the async context correctly.
The mistakes people make
Claiming that psycopg2 is fine because it releases the GIL and Python can run other threads. This misses the single-threaded event loop model. Suggesting run_in_executor or a thread pool as the default or primary solution; while this can wrap sync code, it adds thread overhead and is not the idiomatic pattern for high-throughput async services. Proposing to use the standard synchronous SQLAlchemy engine and session inside an async endpoint without any thread isolation, which directly blocks the loop. Failing to mention the need for an async database driver entirely.
What usually comes next
How would you handle database migrations with an async SQLAlchemy setup? What is the performance difference between asyncpg and psycopg2 in a real benchmark? How do you manage connection pooling with create_async_engine? What happens if you accidentally call a sync ORM method inside an AsyncSession? How would you test async database endpoints?
A concrete example
Imagine an endpoint that runs SELECT sleep(1) using psycopg2 inside async def read_item. With ten simultaneous requests, each blocks the loop sequentially and total latency approaches ten seconds. Replacing this with asyncpg and await session.execute(text("SELECT pg_sleep(1)")) allows the event loop to interleave all ten queries, and with a connection pool they return in roughly one second total.
Interview question
Why does a synchronous database call inside an async FastAPI endpoint prevent other requests from progressing even when the underlying driver releases the GIL?
- a.Because the single-threaded event loop never receives an await point during blocking socket calls, so it cannot interleave other requestsCorrect
- b.Because the database connection pool is exhausted by long-running synchronous queries, forcing subsequent requests to wait
- c.Because synchronous socket calls hold the GIL, preventing the event loop from switching to another task until the query returns
- d.Because releasing the GIL lets other Python threads run, but the event loop cannot migrate tasks to those threads without run_in_executor
Why? this is the answer
The event loop requires await points to switch tasks, which synchronous database calls never yield, so the thread blocks even if the GIL is released. Distractor D is tempting because it sounds like the GIL is the culprit, but the card explicitly notes that synchronous I/O blocks the loop despite releasing the GIL.
Just read this? Test yourself on what you have been reading.
Read the original → fastapi.tiangolo.com
- #fastapi
- #asyncio
- #sqlalchemy
- #databases
- #python
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.
See open roles