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.

MethodReturns / 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

2. Lessons to read before the cab booking

3. Cab booking design decisions to make first

  1. What are your tables, and which constraints do the work? What stops one ride having two cabs, or one cab holding two rides?
  2. What exactly does acceptRide change? 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 already BUSY?
  3. 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?
  4. Where is distance computed, and is the radius inclusive? In SQL or in Java, the same formula must back both findAvailableCabs and calculateFare.
  5. 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"?
  6. 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: ACCEPTED goes straight to COMPLETED, and cancelling works only from REQUESTED.

⚠️ 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 BUSY must vanish from findAvailableCabs at once, and come back when its ride completes. A ride accepted without the cab turning BUSY breaks that.

⚠️ Trap: under load the same vehicle number can arrive twice. If registerCab throws for it, every repeat counts against your error rate.

💡 Tip: calculateFare works for a ride in any status, as long as it exists, and returns the same value every time it's called.

Before you submit

Further reading

After you've tried it

Submit first. The full walkthrough of one solution: Building a Cab Booking System with a Ride State Machine.

Open the problem