Python & FastAPI•Part 1: Candid Experience, Architecture & Intro (1 - 5)
Describe a severe Python backend performance bottleneck you resolved.
How to Explain in an Interview:
- Problem: In a FastAPI ingestion API, request latency jumped from 40ms to 4.2 seconds under heavy load.
- Root Cause: A developer used synchronous
requests.get()and calledjson.loads()on a 25MB payload inside anasync defroute, which blocked the AsyncIO Event Loop and froze all other concurrent requests. - Fix: Switched to non-blocking
httpx.AsyncClient, offloaded heavy JSON parsing to a thread pool withasyncio.to_thread(), and upgraded toorjson(Rust-based fast serializer), dropping response times back to 35ms.
python
# Non-blocking HTTP client with thread-offloaded JSON deserialization:
import httpx
import asyncio
import orjson
async def fetch_and_parse(url: str):
async with httpx.AsyncClient() as client:
response = await client.get(url)
# Offload CPU-heavy parsing away from the main event loop:
return await asyncio.to_thread(orjson.loads, response.content)