Backendcore
Async & concurrency
LLM apps mostly wait on the network — concurrency, not CPU, decides throughput.
What it is and why it exists
A model call is seconds of waiting. Async I/O (an event loop) lets one process hold thousands of in-flight calls; threads do the same with more memory; processes are for CPU-bound work. The traps: blocking calls inside async code, unbounded fan-out that trips provider rate limits, and no backpressure.
Quick prototype
Sequential calls in a for-loop.
In production, at scale
Async HTTP clients, semaphores capping concurrent calls per provider and per tenant, timeouts on every await, bounded queues for backpressure, CPU-heavy work (parsing, embedding locally) in worker processes.
Vendors and tools
| asyncio / httpx / anyio | Python async I/O |
|---|---|
| Node.js event loop | Same model |
| Go goroutines | Cheap concurrency |
On AWS: Lambda concurrency limits and reserved concurrency; ECS tasks sized by in-flight requests.
How it fails
- requests.get inside an async FastAPI handler freezing every request
- asyncio.gather over 1,000 calls → 429 storm
What the interviewer asks next
Your FastAPI service handles 2 requests/s and CPU is idle. Why?
Likely a blocking call (sync SDK, time.sleep, sync DB driver) inside async def, so the event loop is stuck. Use the async client or run sync code in a thread pool.