Cab booking
Riders, drivers and trips: location-based matching, a trip lifecycle and swappable pricing and matching strategies.
On the roadmap:Software Design · step 5: Low-level design: bookings and payments
Lesson 8 of 16 · 11 min read · quiz · exercise
The problem
Riders request a trip from A to B. The system finds a nearby available driver, the driver accepts, the trip happens, and the rider pays. Drivers go online and offline and move continuously.
Entities
Figure SDA-LLD-08-1 · Cab booking classes · © forcsguy.com
The trip lifecycle
Figure SDA-LLD-08-2 · Trip states · © forcsguy.com
Matching
Figure SDA-LLD-08-3 · Matching a request · © forcsguy.com
interface MatchingStrategy { choose(req: Request, candidates: Driver[]): Driver | null }
const nearest: MatchingStrategy = {
choose: (req, ds) => ds.sort((a, b) => dist(a.loc, req.pickup) - dist(b.loc, req.pickup))[0] ?? null,
};
class TripService {
request(rider: Rider, pickup: Point, dropoff: Point): Trip {
const trip = new Trip(rider, pickup, dropoff); // REQUESTED
for (const d of this.locations.nearby(pickup, 3_000)) { // candidates
if (d.tryClaim(trip.id)) return trip.offerTo(d); // atomic: one trip per driver
}
return trip.cancel("no drivers");
}
}
tryClaim is atomic so a driver cannot be offered two trips at once. The location lookup is a spatial index (geohash or quadtree; see the nearby-places lesson).
Design notes
- Pricing is a strategy (base, per km, per minute, surge) and the fare is stored on the trip when booked.
- Live location updates are frequent and approximate; keep them in a fast store and do not write every point to the main database.
- Notifications to rider and driver on each state change use the Observer pattern.
- Edge cases: driver goes offline mid-offer, rider cancels after pickup, payment fails (retry, then flag the account).
- Extensions: shared rides, scheduled rides, ratings, and driver incentives.
Real-world reading
Related reading
- Akutan: Distributed Knowledge Graph Store at eBayeBay engineers · eBayCase Studies
- Location Intelligence Resources | Blog & GuidesFoursquare engineers · FoursquareBlogs
- Rate Limiting with NGINXNGINX · F5 / NGINXBlogs
- TAO: Distributed Data Store for the Social Graph at FacebookFacebookPapers
Show 6 moreShow fewer
- TAO: Facebook’s distributed data store for the social graphUwaterlooPapers
- Optimizing CAL Report Hadoop MapReduce Jobs at eBayeBay engineers · eBayCase Studies
- Cloud Bouncer: Distributed Rate Limiting at YahooYahoo engineers · YahooCase Studies
- Concourse: Generating Personalized Content Notifications in Near-Real-Time at LinkedInLinkedIn engineers · LinkedInCase Studies
- Linux Foundation BlogLinux Foundation engineers · Linux FoundationBlogs
- Gojek BlogGojek engineers · GojekBlogs
Check your understanding
Question 1 of 3
0 correct so far
Which pattern suits 'choose the best driver for this request'?
Go deeper
- Refactoring Guru: state pattern by Refactoring Guru: for the trip lifecycle
Practise it: URL shortener
Everything below is already set up for this lesson: change the design, run the traffic, and see whether you meet the goal.
- 1The task
- 2Build and test
Read-heavy, a few links are very popular.
Add or remove parts, move traffic up and down, and break something to see what gives way first.
Try it yourself
Add surge pricing and a shared ride (two riders in one car). What changes?
Click what a strong design here would need:
More in Design practice
© 2026 forcsguy.com. Original lesson text and diagrams. Books and sites linked above belong to their authors.