Requirements:
- Functional: assign nearest/optimal driver per request in ≤100 ms P95
- Scale: 10k TPS, regional failover
- Non-functional: high availability, graceful degradation
High-level architecture:
- Ingress/load-balancer: Regional API Gateway + global DNS (Route53/anycast) → regional ELBs.
- Streaming/event layer: Kafka (regional clusters, cross-region replication via MirrorMaker2) for asynchronous updates (driver status, telemetry).
- State/store & feature serving: Redis Cluster (sharded by region + geohash) for real-time driver availability and precomputed features (ETA, surge multiplier, recent acceptance rate). Durable store: CockroachDB or Spanner for authoritative records and cross-region replication.
- Feature updater: Lightweight stream processors (Flink/ksql) consuming Kafka to compute streaming features and write to Redis with TTLs.
- Model inference: Two-tier inference:
- Fast local model: optimized lightweight model (TensorRT/ONNX Runtime) deployed per region (K8s + GPU/CPU pods) serving via gRPC with batching micro-latency.
- Heavy global model: larger model for offline training and periodic updates.
- Router/Scoring service (stateless): Receives assignment request, queries Redis for candidate drivers (spatial index via geohash + Redis geo), fetches features, calls local inference (parallel RPCs with timeout), applies business rules and returns assignment.
- Observability & autoscaling: Prometheus/Grafana, SLO-based autoscaling, circuit breakers.
Data partitioning:
- Primary partition by region (legal/latency boundary).
- Within region, shard by geohash prefix (hot spots get more shards).
- Kafka topics keyed by driver_id; Redis slots per shard.
- Cross-region replication only for critical state; prefer eventual consistency.
Latency and throughput strategies:
- Pre-filter candidates in Redis (top N by distance); limit model calls to N (e.g., 5–10).
- Use async pipelining and bounded concurrent RPCs.
- Model batching small (micro-batches) to keep P95 <100ms.
- Reserve capacity for 10k TPS per region; autoscale inference pods and Redis.
Fallback logic if model unavailable:
- Short-circuit: If local model call fails or exceeds timeout (e.g., 30 ms), fallback to:
- Heuristic scorer: deterministic rule-based score (distance, driver acceptance rate, idle time, vehicle type).
- Cached model outputs: last-known model scores from Redis (TTL short).
- Circuit breaker: If model error rate > threshold, flip to heuristic mode and trigger warm-up of new model pods.
- Degraded mode: Increase candidate count and use simple ranking to avoid starvation.
- Post-facto correction: Log assignments for offline re-ranking and learning; generate alerts.
Trade-offs:
- Strong regional partitioning optimizes latency but complicates cross-region rebalancing.
- Heuristics ensure availability at slight quality loss; periodic A/B test to measure impact.
This design balances sub-100 ms P95 latency using in-memory feature serving, lightweight regional inference, sharded spatial indexing, and robust fallbacks for availability.