Menu

1 / 3

asyncpg vs SQLAlchemy ORM - The 9x Slowdown Nobody Warns You About

DEV Community: fastapi·Anas Issath·4 days ago
#bx9yH490
#devtofastapi#devto#dev#async#sqlalchemy
Reading 0:00
15s threshold

Async doesn't mean fast. Your ORM might be quietly throwing away everything asyncpg gives you.

We migrated a FastAPI service from sync SQLAlchemy to async SQLAlchemy, expecting a performance win. Async is supposed to be faster, right? Non-blocking I/O, more concurrent requests per worker, better resource utilization.

Our load test showed the opposite. The async version was slower per-request than the sync version had been. Not by a little: a specific endpoint that fetched 500 rows and mapped them to ORM objects went from 340ms to 890ms .

I assumed we'd misconfigured something. Wrong connection pool size, maybe, or a missing index. I profiled the endpoint with cProfile and found the answer: the async ORM layer itself was the bottleneck.

Object instantiation, identity map tracking, and attribute loading (the same overhead that exists in sync SQLAlchemy) was still there in async SQLAlchemy.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.