Scrivio Interview library
DesignFoundations6 min read · updated 13 November 2026

A ride-hailing service matches riders with nearby drivers. Driver apps send their position every few seconds; a rider requests a trip, a driver accepts, the trip moves through pickup and drop-off, and the fare is charged at the end.

Define the main APIs, the trip data model and the trip states for a ride-hailing service, and outline the services involved from ride request to payment.

The main APIs are POST /trips with pickup, drop-off, product type and an idempotency key, GET /trips/{id}, a driver location stream over a persistent connection, driver accept or decline of an offer, and trip updates pushed to the rider. A trip is a guarded state machine from REQUESTED through DRIVER_ASSIGNED, DRIVER_ARRIVED and IN_PROGRESS to COMPLETED, with CANCELLED and NO_DRIVER_FOUND branches, stored in a transactional database. Current driver positions live in an in-memory geospatial index, history in a log or time-series store, and the work is split across location ingestion, dispatch, trip, pricing, payments and notification services, with high-volume location updates kept away from the trip database.

Why this question is asked

Ride-hailing is a popular system design question because it mixes two very different kinds of data: a flood of approximate, short-lived location updates, and a trickle of trips that involve money and must never be lost. A good design keeps them apart. At this level, interviewers want clear APIs, a trip state machine, a sensible data model and an outline of the services. In this answer, we will size the problem, define the APIs, design the trip state machine, choose where each kind of data lives, and walk through the services from ride request to payment.

1Requirements and Rough Numbers

Riders request trips from a pickup to a drop-off and see an estimate, then follow the assigned driver on a map. Drivers send their position every few seconds, receive offers, and accept or decline. The trip moves through pickup and drop-off, and the fare is charged at the end. Trips and payments must be durable and correct; positions only need to be fresh.

Sizing early shows why the two must be separated. With 500,000 drivers online at peak, each reporting every 4 seconds, we get 500,000 / 4 = 125,000 location writes per second. Trips are far fewer: even 2 million trips a day is about 23 per second on average, perhaps 100 at peak, each with a handful of state changes. So the location path is about a thousand times busier than the trip path, and location updates must never touch the trip database.

2Defining the APIs

The rider creates a trip with a POST, including an idempotency key, a unique value the client generates so that if the request is retried after a timeout, the server returns the same trip instead of creating a second one. The rider then reads the trip and receives pushed updates. Drivers stream locations and answer offers.

Java
POST /trips Idempotency-Key: 5b1e-...
{ "pickup": {"lat": 52.5200, "lng": 13.4050}, "dropoff": {"lat": 52.4800, "lng": 13.3600},
"product": "standard" }
-> 201 { "trip_id": "t_9", "state": "REQUESTED", "fare_quote": 18.40 }
(same key again -> 201 with the same trip t_9, no second trip)
GET /trips/t_9 -> { "state": "DRIVER_ASSIGNED", "driver": {...}, "eta_s": 240 }
// Driver app, over a persistent connection (WebSocket or gRPC stream)
-> { "type": "location", "lat": 52.5011, "lng": 13.4190, "heading": 270, "ts": 1767225600 }
<- { "type": "offer", "offer_id": "o_3", "trip_id": "t_9", "expires_in_s": 15 }
POST /offers/o_3/accept -> 200 (or /decline)
// Rider app receives pushed updates: driver assigned, driver location, arrived, started, completed

A persistent connection suits drivers because they send updates every few seconds and must receive offers instantly; opening a new HTTPS request each time would waste handshakes. Frequent small POSTs also work at smaller scale.

3The Trip State Machine

A trip moves through a fixed set of states, and only certain moves are allowed. A trip starts REQUESTED. When a driver accepts, it becomes DRIVER_ASSIGNED; when the driver reaches the pickup, DRIVER_ARRIVED; when the rider is in the car, IN_PROGRESS; and at drop-off, COMPLETED. It can become CANCELLED before the ride starts, by rider or driver, and NO_DRIVER_FOUND if dispatch gives up.

Transitions must be guarded, because two events can race: the rider cancels at the same moment the driver accepts. A version column makes each transition a conditional update, so only one of them succeeds, and the loser sees the new state and reacts.

SQL
UPDATE trips
SET state = 'DRIVER_ASSIGNED', driver_id = 'd_17', version = version + 1
WHERE id = 't_9' AND state = 'REQUESTED' AND version = 3;
-- 1 row: assigned
-- 0 rows: the trip changed meanwhile (e.g. the rider cancelled), so the accept is refused
Step throughTrip states
  1. REQUESTEDrider asked; dispatch searching
  2. Driver accepted?
    YesDRIVER_ASSIGNEDNoNO_DRIVER_FOUND after the search limit
  3. DRIVER_ARRIVEDdriver at pickup; waiting time starts
  4. Rider in the car?
    YesIN_PROGRESSNoCANCELLED, perhaps with a fee
  5. COMPLETEDfare calculated and charged

5 steps in order. Play them, or step through them one at a time.

CANCELLED is also allowed from REQUESTED and DRIVER_ASSIGNED.

4Where Each Kind of Data Lives

Trips go in a transactional store such as PostgreSQL: rider, driver, state, version, pickup, drop-off, fare quote, final fare and timestamps for each state. This is the system of record; every trip and every payment can be traced from here.

Current driver locations go in an in-memory geospatial index, which answers which free drivers are near this point quickly. Redis GEO commands (GEOADD and GEOSEARCH) can do this, or an in-memory index keyed by grid cells such as H3 hexagons, held in the dispatch service. It is not the system of record: if it is lost, it refills from the next updates within 4 seconds.

Location history is appended to a log such as Kafka and stored in a time-series store or object storage such as S3. It is used to draw the route, compute the distance for the fare and settle disputes, so it must be kept, but it is written in bulk and read rarely.

Data and its store
DataStoreWhy
Trips and state transitionsPostgreSQLDurable, transactional, guarded updates
Current driver positionsIn-memory geo index (Redis GEO or H3 cells)Fast nearby search, rebuilt from updates
Location historyKafka, then time-series store or S3High volume, appended, used for fares and disputes
PaymentsPayments service databaseMoney, audited, idempotent charges

5The Services From Request to Payment

Location ingestion receives the driver streams, updates the geospatial index and appends to the history log. It is high-volume and loss-tolerant: a dropped update is replaced 4 seconds later. The pricing service gives estimates and applies surge pricing when demand exceeds supply in an area. The trip service owns the trip record and its state machine. The dispatch service finds candidate drivers near the pickup, ranks them and sends offers. The notification service pushes updates to rider and driver apps. The payments service charges the fare at the end, using the trip id as its idempotency key so a retry never charges twice.

Step throughFrom ride request to payment
Rider appTrip serviceDispatchDriver appPayments
POST /trips (idempotency key)
store REQUESTED, get price quote
find a driver for t_9
offer o_3, expires in 15 s
accept
assign d_17 (conditional update)
push: driver assigned, ETA 4 min
arrived, started, completed
charge final fare (key: trip id)
charged

10 messages in order. Play them, or step through them one at a time.

6Follow-up questions

Why keep current locations out of the trip database?

Because the rates differ by about a thousand times: 125,000 location writes per second against roughly 100 trip changes per second at peak. Putting locations in the trip database would make the most important data, trips and money, compete with a flood of approximate updates. Locations also have different needs: only the latest value matters, and losing one is harmless, which suits an in-memory index.

How is the final fare computed?

From the route actually driven, using the location history between IN_PROGRESS and COMPLETED to compute distance and time, plus any surge multiplier fixed at request time, waiting time and tolls. Many services quote an upfront price and only recompute if the trip differs a lot from the estimate, for example because of a changed destination.

What happens if no driver accepts?

Dispatch moves through ranked candidates, perhaps widening the search area, until a time limit such as two minutes, then moves the trip to NO_DRIVER_FOUND and notifies the rider, who can try again. The rider is never charged for a trip that did not start.

7Conclusion

In this answer, we designed the core of a ride-hailing service. The APIs create trips with an idempotency key, stream driver locations and offers over a persistent connection, and push updates to riders. Trips follow a guarded state machine stored in PostgreSQL, current positions live in an in-memory geospatial index, and location history is appended to Kafka and long-term storage. Location ingestion, dispatch, trip, pricing, payments and notification services split the work, and sizing early showed that 125,000 location writes per second must never touch the trip database.

What to say in the interviewAbout 80 seconds aloud

Open with

I would size it first: 500,000 online drivers reporting every 4 seconds is about 125,000 location writes per second, while trip changes are maybe 100 per second, so the two must be separated.

Then, if they want more

The APIs: POST /trips with pickup, drop-off, product type and an idempotency key so a retry does not create two trips; GET /trips/{id}; drivers stream locations over a persistent connection and receive offers on it, and accept or decline with a call; riders get trip updates pushed.

The trip is a state machine: REQUESTED, DRIVER_ASSIGNED, DRIVER_ARRIVED, IN_PROGRESS, COMPLETED, with CANCELLED and NO_DRIVER_FOUND branches, and each transition is a conditional update on a version column, so a cancel and an accept racing cannot both win. Trips live in a transactional store like PostgreSQL.

Current driver positions live in an in-memory geospatial index, like Redis GEO or H3 cells, which is not the system of record and refills from updates. Location history goes to Kafka and then a time-series store or S3, for routes, fares and disputes.

The services are location ingestion, dispatch, trip, pricing for estimates and surge, payments, which charge once using the trip id as the idempotency key, and notifications.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Define the main APIs, the trip data model and the trip states for a ride-hailing...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/57979f5452/