Design Uber: location, matching, and trip service
Uber
Guided ModeStep 8 β Trip State Cache
Request Flow
Write-through on transition
When Trip Service writes a state transition to Cassandra, it simultaneously writes current state to Cache: SET trip:<id>:state {state, driver_lat, driver_lng, eta} EX 7200. Both happen in one atomic operation.
Cache-first reads
The rider's tracking screen refreshes every 3 seconds. The API Server checks Cache first: GET trip:<id>:state. On a 10-minute trip with 200 refreshes, all 200 hits serve from cache β 0 DB reads.
TTL + explicit invalidation
Cache TTL is 2 hours β covers any possible trip. On COMPLETED, the Trip Service explicitly DELetes the cache key immediately so a stale IN_PROGRESS state is never served after completion.
Why We Need This
300 active trips Γ every rider's tracking screen refreshing every 3 seconds = potentially thousands of DB reads/minute just for trip-state polling. Redis handles this at 1M reads/second with <1ms latency, making DB reads for this path essentially zero.
Key Insight
Cache the current state (what riders see now), not the full event log (what auditors need). Write-through on every state transition keeps the cache perfectly consistent β no stale reads, no async invalidation job.
Step 8 of 8
