Part 7 of 8 · 3 min read
Trade-offs
Surge, money and failure.
Pricing, payments, scale and trade-offs
Surge pricing. When requests in an area exceed free drivers, raise the multiplier to cut demand and attract drivers. Each pricing region (a cell or group of cells) counts requests and free drivers over a short window (say 1 minute), compares them, and sets a multiplier within limits. The multiplier shown at request time is locked into the quote for that trip. Smooth changes over time so the price does not jump wildly. Surge needs a stream processor over the request and location events (Level 12).
Payments. The rider's payment method is authorised at request or at the start, and charged at the end. Use a payment provider, with an idempotency key on every charge (page 9). The driver's earnings are recorded in a ledger and paid out in batches. Never charge twice: the trip id is the key.
Scale out.
- Partition by region (a city or group of cities). A rider's request goes to the region of the pickup point. Most data stays inside a region, which also reduces latency. A global directory holds accounts and routes requests.
- Each region runs its own location service, dispatch and trip database.
- Cross-region trips (rare) are handled as one region owning the trip.
Failure handling.
- Location service down: matching uses slightly old positions, or the region degrades to larger search areas.
- Dispatch worker dies: its in-flight requests are picked up by another worker, since the trip state is in the database and driver reservations expire by themselves.
- Region failover: move to a nearby region's replicas, with a short pause.
| Choice | Gain | Cost |
|---|---|---|
| In-memory geo index | Very fast search | Rebuilt after a crash |
| Rank by pickup ETA | Better matches | More calls to ETA |
| One offer at a time | Fair, simple | Slower matching |
| Batch matching | Better overall times | A few seconds of wait |
| Compare-and-set reservation | No double assignment | Needs a consistent store |
| Region partitioning | Local and fast | Cross-region cases are special |
| Surge multiplier | Balances supply and demand | Public sensitivity, needs limits |
Interview angle
Answer in your head first, then open it.
How do you find nearby drivers?
A geospatial index (geohash, quadtree or cell ids) in memory, refreshed by location updates.
How do you avoid two trips for one driver?
An atomic compare-and-set reservation with a lease.
What is the biggest load?
Driver location updates, about half a million a second in this example.
How do you handle retries?
A request id makes trip creation idempotent.
How do you scale?
Partition by region and run a full stack in each.
One follow-up to try. "A stadium event ends and 30,000 people request rides in 10 minutes." A good reply: the cell of the stadium is hot, so shard its location data finer. Use batch matching over short windows. Raise surge to draw drivers from nearby, and offer pickup zones away from the crowd to spread the load. Queue requests and show an honest wait time.
Check yourself: 1. What data does surge pricing need? 2. How is a charge made safe against repeats? 3. Why partition by region?
Words you will meet
| Word | Meaning |
|---|---|
| Dispatch | Choosing a driver and sending the offer |
| Geohash | A cell name where nearby places share a prefix |
| Lease | A reservation that expires by itself |
| State machine | A set of states with allowed moves |
| Surge | A price multiplier when demand exceeds supply |
| ETA | Estimated time of arrival |
| Idempotency key | A token that makes a repeated request count once |