Lesson 32 / 32
Design a Ride-Sharing Matching System
Matching riders to nearby drivers in near real time.
Requirements
Drivers stream their location continuously; a rider requests a trip and should be matched with a nearby available driver within a few seconds; the system must handle a surge (concert ending) without collapsing.
Geospatial indexing
Divide the map into cells using geohashing or a quadtree, and keep an in-memory index of cell -> available drivers. A match query looks up the rider's cell and expanding rings around it instead of scanning every driver on Earth.
Air traffic control, not a phone book
You don't find a nearby driver by scanning a list of every driver's exact address, like a phone book. It's more like air traffic control watching a live radar grid — drivers constantly update their cell, and the matcher only looks at the handful of nearby blips.
Handle the race: two riders, one driver
Two match requests can target the same driver simultaneously. Use an atomic compare-and-set (or a per-driver lock) so exactly one request wins the driver and the other retries against the next-nearest candidate.