Ride hailing: a driver in two seconds
60,000 drivers are on the road, each sending their position every 4 seconds: 15,000 updates a second. Riders open the app, ask for the nearest car and want a price. Every position goes straight into one database, and if the pricing service dies, nobody can book.
Play this levelPart of Bugpit Pro. It runs in your browser: build the design, press Run, and watch what breaks.
The lesson
A ride app is mostly drivers, not riders. Each moving car is a stream of writes, and every one is out of date a few seconds later. So positions go down an event stream, and workers keep only the latest one in a Geo Index.
"Who's nearest?" is a map question. A Geo Index checks only the cells around the rider, so it takes milliseconds however many drivers there are. Then a Maps API gives the route and the arrival time. It has a quota, so you pay for each step of it.
Riders and drivers both watch the car move, so the app holds a live connection through a Realtime Gateway.
Pricing is useful but not essential. If it dies, charge the normal price and keep booking rides. That's a circuit breaker on the pricing service.
What you have to achieve
16,000 requests a second at the busiest hour, 94% of them writes.
- No broken or illegal connections
- Use a Realtime Gateway
- Use an Event Stream
- Use a Geo Index with at least 2 instances
- Use a Maps API
- No component is overloaded
- Workers clear the queue within the day
- If every Internal Service machine dies, under 0.1% of requests fail
- Fewer than 0.1% of requests fail
- p99 response time under 400 ms
What you learn
Crash the pricing service: rides still get booked. Now count the writes: almost all your load is cars moving, and none of it touches the database. When data is out of date in seconds, keep only the latest copy and let the rest flow past.
Check yourself
Why do driver positions go through a stream instead of into a database?
- Streams are cheaper to store
- Each position is out of date in seconds, so only the latest one is worth keeping
- Databases can't store map data
Show the answer
Each position is out of date in seconds, so only the latest one is worth keeping. Workers read the stream and keep just each driver's newest position in the Geo Index. The old ones flow past.
The pricing service dies. What should happen?
- Rides are booked at the normal price
- Nobody can book until it's back
- The app retries until it answers
Show the answer
Rides are booked at the normal price. A circuit breaker stops calling it and falls back. A ride at a normal price beats no ride.
Why did the pricing service need 10 machines, even with a breaker?
- Breakers need spare machines
- Pricing is slow
- A breaker that trips on a normal day means users always get the fallback
Show the answer
A breaker that trips on a normal day means users always get the fallback. The breaker is for real failures. If the service is simply too small, it trips all the time and protects nothing.