CSE Roadmap

Specialist tier · about 5 hours · 8 parts

Part 6 of 8 · 2 min read

Trips

Safe states, retries and live tracking.

States. REQUESTED, MATCHED (driver assigned), DRIVER_ARRIVED, IN_PROGRESS, COMPLETED, CANCELLED. Allowed moves are fixed: for example, COMPLETED cannot go back to IN_PROGRESS. Each change is recorded as an event with a time (event sourcing, page 9), so the trip history can be rebuilt and disputes can be checked.

Storage. A database partitioned by trip id (Level 3), replicated. Trips are few (230 a second on average), so one relational or key-value cluster is fine. Add a secondary index by rider and by driver for history screens. Active trips stay in a hot table and old ones move to cheaper storage.

Updates to the trip. Each state change checks the current state first (a conditional write), so a repeated "driver arrived" message is harmless.

Live tracking. During a trip, the driver app sends locations. They go to the location service, and also to a tracking channel for the trip. The rider's app subscribes over a WebSocket (page 1) and receives the driver's position every few seconds. The map on the rider's phone moves smoothly by predicting between updates. If the WebSocket is down, fall back to polling.

ETA and route. The ETA service uses the road graph and live traffic from page 3. It is called at request time (pickup ETA of each candidate, trip ETA), and during the trip to update arrival time. Traffic data itself comes from the driver location stream (the same updates, aggregated per road segment).

Fare. Fare = base + rate per distance × distance + rate per minute × time, times a surge multiplier. Distance and time are recorded from the actual route during the trip, not only from the estimate. Show the estimate before the ride, charge the real fare after.

Cancellations and failures. If the driver's app goes silent during MATCHED, a timer cancels the match and re-dispatches. If the rider cancels, the driver is freed. A fee may apply after a time limit. Each of these is a transition with a timer, handled by a job scheduler (page 7).

State machine

A state machine lists the states of a thing and the moves allowed between them, so that illegal moves are rejected.

Example: a trip may go from MATCHED to DRIVER_ARRIVED, but never from COMPLETED to MATCHED.

Check yourself: 1. Which moves are not allowed from COMPLETED? 2. How does a repeated message become harmless? 3. How does the rider see the driver move?