← All writing

Real-time ML Inference with FastAPI, Async Workers & Streaming

By Venkata Subbaraju Dommaraju · MLOps · 9 min read

A model that answers in 200ms feels instant; one that takes four seconds feels broken. In production, serving is half the job — here are the patterns I use to keep ML and LLM endpoints fast and reliable under load.

Why real-time serving is its own discipline

Training optimizes a metric; serving optimizes a distribution of user experiences under concurrency, spikes, and failures. The bottleneck is rarely the model maths — it is I/O, queuing, and tail latency.

The serving stack

  • FastAPI async endpoints, so one slow call never blocks the event loop.
  • A queue plus worker pool (Celery / Redis) for heavy or batchable work.
  • Token streaming over Server-Sent Events, so users see output immediately.
  • Timeouts and backpressure, so overload degrades gracefully instead of collapsing.
  • Horizontal autoscaling keyed to queue depth and p95 latency.

Backpressure beats blowing up

When demand exceeds capacity, shed load deliberately: cap the queue, return a fast 'try again shortly' rather than timing out every request, and protect the healthy traffic. A predictable slowdown is far better than a cascading failure.

Latency tricks that actually move p95

  • Cache embeddings and frequent answers.
  • Route easy cases to a smaller, faster model; reserve the big model for the hard ones.
  • Batch where you can; stream where you cannot.
  • Pool database and HTTP connections; never open one per request.

What streaming bought us

On the agentic AI application I built, the model's answer was never the slow part users complained about — the silence before it was. Streaming agent tokens over SSE and WebSocket cut time to first response from several seconds to under one second, without making the full answer any faster.

Measure what users feel

Averages lie. Track p95 and p99 latency, error rate, and queue depth — and alert on them. The goal is a tight tail, not a pretty mean.

Takeaway

Real-time ML is an engineering problem as much as a modeling one. Async I/O, queues, streaming, and honest tail-latency monitoring are what make a demo survive contact with real traffic.

Related projects

Contact

Open to full-time Senior AI / Data Engineer roles. Based in Downingtown, PA.

LinkedIn · GitHub · Résumé (PDF) · Book a call