Redis-backed tier-aware queuing cuts LLM p99 latency from 20s to under 2s

An AI companion app running on a single 4-core server faced severe latency spikes during peak hours, with paid users experiencing 20-plus-second response times due to free-tier traffic saturating shared LLM call slots. The initial fix using a per-process asyncio.Semaphore failed on two counts: it multiplied slots across eight Uvicorn workers instead of enforcing a true global cap, and its FIFO ordering gave free and paying users equal priority. Engineers replaced it with a Redis-backed slot manager using an atomic Lua script to enforce both a global concurrency ceiling of 30 in-flight LLM calls and separate per-tier caps. The new system also applies different timeout behaviors by tier, letting free and guest requests fail fast while paid users queue patiently, ensuring paying customers always have reserved capacity. The entire implementation runs to roughly 40 lines of code and brought peak p99 latency down from over 20 seconds to under 2 seconds.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in