← Back to Index

12. Real-Time Inference Pipeline

Real-time inference pipeline showing request flow through feature fetch, model eval, cache, and fallback

Real-time inference requires an optimized pipeline that delivers predictions within strict latency budgets. Every millisecond counts.

Core Pipeline Components

# End-to-end inference flow (fraud detection example)
1. Client sends transaction data (REST/gRPC)
2. API Gateway: authenticate, validate, rate-limit
3. Feature fetch: lookup user features from online feature store (Redis)
4. Feature computation: compute real-time features (tx amount vs avg, location match)
5. Cache check: has this exact transaction been scored recently?
6. Model inference: run ensemble of 3 XGBoost models (50ms budget)
7. Post-processing: apply business rules (amount > 10K → force review)
8. Response: return fraud_score + explanation + action
9. Async: log to Kafka for monitoring, feedback collection

Micro-Batching

Micro-batching groups individual inference requests into small batches to improve GPU utilization and throughput, at the cost of some latency.

# Micro-batching server logic
Incoming requests go into a queue
Every 10ms (or when queue reaches 32 requests):
  Pop all requests from queue
  Stack them into a batch tensor
  Run model inference on the batch (GPU)
  Map results back to individual responses
  Return responses

Latency added: avg 5ms (half of 10ms window)
Throughput increase: 3-5x for small LLMs

Fraud Detection Example

A complete real-time fraud detection system illustrates all the patterns:

ComponentTechnologyLatency Budget
API GatewayKong / Envoy5ms
Feature fetchRedis (Feature Store)15ms
Feature computationIn-app computation20ms
Cache lookupRedis5ms
Model inference (3 models)Triton (XGBoost)100ms (parallel)
Post-processing & rulesIn-app10ms
Kafka loggingAsync (non-blocking)0ms added
Total (p99)~160ms ✅

Performance Optimization Checklist

Exercise: Design a real-time content moderation API that detects toxic text and NSFW images. The API must return a verdict within 500ms for text-only content and 2s for images. Scale: 1,000 req/s. Identify each pipeline stage, assign latency budgets, describe the model serving strategy (separate or combined), and plan for graceful degradation if the image model is slow.