Async SQLAlchemy — asyncpg and AsyncSession — Concept Check
Questions on Async SQLAlchemy — asyncpg and AsyncSession. create_async_engine + AsyncSession + asyncpg make DB calls awaitable, so the event loop serves other requests during queries — but lazy loading breaks in async, making eager loading mandatory.
Questions in this quiz
7 questions on Async SQLAlchemy — asyncpg and AsyncSession — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A FastAPI endpoint using async DB calls is experiencing high latency. The code uses lazy loading for related data. What could be the issue?
- ALazy loading is causing MissingGreenlet errors, blocking the event loop.
- BThe async def functions should be replaced with sync def.
- CThe database connection pool size is too large.
- DThe asyncpg driver is not compatible with FastAPI.
2.You are designing a FastAPI service with high concurrency needs. Which setup should you choose for database operations?
- Aasync def with sync DB to prevent blocking the event loop.
- Basync def with async DB to handle high concurrency and multiple awaits.
- Cplain def with sync DB for simplicity and speed.
- Dplain def with async DB for better performance.
3.A developer is using asyncpg with SQLAlchemy for async operations. They encounter MissingGreenlet errors. What is a likely cause?
- AThe database URL is incorrect.
- BThey need to increase the connection pool size.
- CThe asyncpg driver is misconfigured.
- DThey are not using eager loading for relationships.
4.A FastAPI endpoint using async DB calls with asyncpg is not performing well. What might be the issue if the endpoint is purely CRUD?
- AThe connection pool size is too small.
- BUsing async DB when a plain-def CRUD service is sufficient.
- CThe asyncpg driver is not optimized for CRUD operations.
- DThe async def functions should be replaced with sync def.
5.In a FastAPI application, why is it important to use eager loading with async DB operations?
- ATo improve the performance of asyncpg driver.
- BTo reduce the number of database connections needed.
- CTo simplify the database schema.
- DTo prevent MissingGreenlet errors when accessing unloaded relationships.
6.What happens if you use a sync DB call inside an async def endpoint in FastAPI?
- AThe sync DB call automatically converts to async.
- BThe endpoint runs faster due to thread optimization.
- CThe asyncpg driver handles it seamlessly.
- DThe event loop gets blocked, stalling all in-flight requests.
7.Why might an async def + async DB setup be preferred over a plain def + sync DB setup?
- AIt automatically optimizes database queries.
- BIt allows handling high concurrency and multiple awaits efficiently.
- CIt simplifies the code structure significantly.
- DIt eliminates the need for a connection pool.