FastAPI interview question · Most asked

Can you use synchronous SQLAlchemy with FastAPI? What are the trade-offs?

Summary

Yes: use plain def endpoints and a sync session dependency, and FastAPI runs them in a threadpool. It's simpler and mature, but concurrency is limited by the threadpool size (about 40 by default via AnyIO) and the DB pool.

Longer Answer

Never call a sync session from async def, because it blocks the loop. Mixing styles is fine per endpoint. You can raise the AnyIO thread limiter if needed. For high-concurrency, I/O-heavy services, async drivers scale better.

Key Points

  • Sync DB → def endpoints
  • Threadpool capacity limits concurrency
  • Never use sync sessions in async def

More most-asked FastAPI questions

← All FastAPI questions