LLD Guide · Module 2: Problem prep · Lesson 02.09
Prep: Cab Booking System
Rides, cabs and fares in Postgres, a three-step state machine, one winner when cabs race for a ride, and a nearby search that stays fast.
By Cruscible Engineering · Published
Four drivers tap "accept" on the same ride in the same second. Exactly one of them gets it, and the other three need to hear "no" cleanly.
This lesson prepares you for Build a Cab Booking System. It doesn't solve it.
1. What the cab booking contract asks for
Drivers register cabs with a GPS position. Riders request rides from a pickup to a drop-off. A cab accepts a ride, completes it, and becomes available again. All of it lives in Postgres.
| Method | Returns / does |
|---|---|
registerCab(driver, vehicleNumber, type, lat, lng) | a unique cab id; the cab starts AVAILABLE |
requestRide(rider, pickupLat, pickupLng, dropLat, dropLng) | a unique ride id; the ride starts REQUESTED |
acceptRide(rideId, cabId) | true and the cab becomes BUSY, or false if the ride isn't REQUESTED |
completeRide(rideId) | true from ACCEPTED only; the cab is AVAILABLE again |
cancelRide(rideId) | true from REQUESTED only |
getRideDetails(rideId) | rideId, riderName, status, cabId (or null), pickup and drop coordinates; null if unknown |
updateCabLocation(cabId, lat, lng) | true, or false for an unknown cab |
findAvailableCabs(lat, lng, radiusKm) | AVAILABLE cabs within the radius, nearest first, each with distanceKm |
getDriverHistory(cabId) | that cab's finished rides, most recent first, each with a fare |
calculateFare(rideId) | 50.0 + distanceKm × 15.0 (great-circle distance), or -1.0 if unknown |
Figure: Four cabs, one ride: the same race as two bookings for the last seat
- Read, then write: every cab's transaction sees the ride REQUESTED, and each one accepts it.
- Conditional UPDATE: the first cab's update changes 1 row and wins; the others change 0 rows and get false.
2. Lessons to read before the cab booking
- 01.04 Postgres for LLD: conditional updates and row locks. Accepting a ride is the lost-update race above.
- 01.05 State machines: the ride lifecycle, and what each method returns for an illegal move.
3. Cab booking design decisions to make first
- What are your tables, and which constraints do the work? What stops one ride having two cabs, or one cab holding two rides?
- What exactly does
acceptRidechange? The ride's status, the ride's cab, and the cab's status. Do all three happen together, or not at all? And what if the cab is unknown or alreadyBUSY? - How does the nearby search stay fast? Under load, nearly half the calls are searches over a growing table of cabs. How do you avoid computing the distance to every cab on every call?
- Where is distance computed, and is the radius inclusive? In SQL or in Java, the same formula must back both
findAvailableCabsandcalculateFare. - What can the driver history contain? A ride can only be cancelled while
REQUESTED, before any cab has it. Which rides does that leave, and what orders them "most recent first"? - What does a repeated vehicle number do? The brief calls vehicle numbers unique but doesn't say what happens to a duplicate.
4. Cab booking pitfalls
⚠️ Trap: this ride machine is not lesson 01.05's example. There is no
IN_PROGRESS:ACCEPTEDgoes straight toCOMPLETED, and cancelling works only fromREQUESTED.
⚠️ Trap: "check the status, then update it" in two statements lets two cabs both accept. The check and the change must be one atomic step.
⚠️ Trap: a cab marked
BUSYmust vanish fromfindAvailableCabsat once, and come back when its ride completes. A ride accepted without the cab turningBUSYbreaks that.
⚠️ Trap: under load the same vehicle number can arrive twice. If
registerCabthrows for it, every repeat counts against your error rate.
💡 Tip:
calculateFareworks for a ride in any status, as long as it exists, and returns the same value every time it's called.
Before you submit
- Cab and ride ids are unique
- New cabs are
AVAILABLE; new rides areREQUESTEDwith no cab - Only
REQUESTED → ACCEPTED,ACCEPTED → COMPLETEDandREQUESTED → CANCELLEDsucceed; every other move returnsfalse - When several cabs accept one ride at once, exactly one gets
true - Accepting makes the cab
BUSY; completing makes itAVAILABLEagain getRideDetailsshows the status and the accepting cab, andnullfor an unknown ridefindAvailableCabsreturns onlyAVAILABLEcabs within the radius, nearest first, and an empty list (notnull) when there are nonegetDriverHistorylists the cab's finished rides, most recent first, and an empty list for a new cabcalculateFareis50.0 + distanceKm × 15.0by great-circle distance, and-1.0for an unknown ride- All data is in Postgres, and the nearby search stays fast as cabs pile up
Further reading
SELECT … FOR UPDATEandSKIP LOCKED: the locking clause (PostgreSQL docs)- Indexes (PostgreSQL docs)
- Haversine formula (Wikipedia)
After you've tried it
Submit first. The full walkthrough of one solution: Building a Cab Booking System with a Ride State Machine.