Arsh Singhal’s Post

I thought FastAPI was fast. Turns out I was just using it wrong. Client called me one day. Why is the API so slow? I thought you said FastAPI is fast? I said yes. FastAPI is async. It handles concurrent requests beautifully. What I didn't say.... My routes weren't async at all. Here's what my code looked like: # What I wrote @app.get("/users/{id}") def get_user(id: int, db: Session = Depends(get_db)): return db.query(User).filter(User.id == id).first() Looks fine right? That def instead of async def — that one word was blocking FastAPI's entire event loop. Every request was waiting for the previous one to finish. Not concurrent. Not async. Just a very expensive queue. The fix was literally one word: # What it should have been @app.get("/users/{id}") async def get_user(id: int, db: AsyncSession = Depends(get_db)): result = await db.execute(select(User).where(User.id == id)) return result.scalars().first() def → async def Response time under load: 4 seconds → 200ms. The lesson nobody tells you clearly: FastAPI is only as async as you make it. The framework won't save you from blocking code. It just gives you the tools — you still have to use them correctly. → Always use async def for route handlers → Use httpx not requests inside async endpoints → Use AsyncSession not Session for DB calls One word. One month of slow APIs. One very unhappy client. #FastAPI #Python #BackendDevelopment #AsyncProgramming #SoftwareEngineering #LessonsLearned #WebDevelopment

To view or add a comment, sign in

Explore content categories