StaffCoder
Spring Boot
Spring BootSpring Boot 4.1Java 25
12 modules · 48 challengesSign up
Module 12 · Microservices and Resilience

Ride Hailing Dispatch Platform

hard50%1 of 2 solved

Problem statement

Complete the dispatch core of a ride hailing platform: one dispatch coordinates three backing services over HTTP, survives any of them stalling or failing, and never leaves a driver reserved for a ride that was not billed.

Requirements

PeerGateway

riderExists(Long riderId), assignDriver(Long riderId) and quotePrice(Long riderId, String pickup, String dropoff) each make one hop to their peer, and releaseDriver(String driverId) makes the same kind of hop to hand a driver back. Every hop that fetches an answer must:

  • Leave this application as a real HTTP request to the peer address resolved from the running server, so the peer records an inbound call for it
  • Be given up on when the peer has not answered within the configured hop timeout, and that attempt counted as failed
  • Be tried again after a failed attempt until the configured attempt budget is spent: a hop that answers within the budget still completes the ride, and a hop that fails every allowed attempt ends the dispatch
  • Take its hop out of service once its failure window is full of failures: from then on a dispatch gives up on that hop at once, without reaching the peer and without waiting for it
  • Try the peer once more on the first dispatch after the configured wait has passed, and put the hop back into normal service when that try succeeds
  • Be taken out of service on its own: a hop that is out of service never stops the other hops from being called
  • Treat a rider the rider service does not know as an answer rather than a hop failure: that reply is never tried again and never counts towards the rider hop's failure window
  • Take the hop timeout, the attempt budget, the failure window size and the wait from configuration, never from a value written into the code

DispatchService

dispatch(DispatchRequest request)

  • Handle one dispatch by checking the rider, then reserving a driver, then pricing the ride, in that order
  • Store the ride with its rider, the reserved driver, the quoted price and status DISPATCHED, and answer 201 Created carrying dispatchId, driverId, priceCents and status
  • A rider the rider service does not know: answer 404 Not Found with the error message "Rider not found with id: " followed by the requested rider id
  • A rider hop given up on for taking too long: answer 504 Gateway Timeout with the error message "Rider service timed out"
  • Any other hop that cannot be completed, including a driver or pricing hop given up on for taking too long: answer 503 Service Unavailable with the error message "Dispatch failed, please retry"
  • A dispatch that fails before a driver is reserved releases nothing and stores nothing
  • A dispatch that cannot be priced after a driver was reserved gives that driver back to the driver service and stores the attempt with its rider, the released driver, no price and status ROLLED_BACK
  • No driver is ever left reserved for a ride that was not billed: over a run of dispatches, reservations minus releases equals the number of stored DISPATCHED rides

Provided endpoint, peers and configuration

  • POST /dispatches reads a request carrying riderId, pickup and dropoff, hands it to DispatchService.dispatch, and returns what that produces as 201 Created
  • GET /peers/riders/{riderId} is the rider service: it answers 200 OK with the rider, or 404 Not Found when it does not know that rider (riders 101, 102 and 103 are known)
  • POST /peers/drivers/assign answers 200 OK with a freshly reserved driverId; POST /peers/drivers/release takes a driverId back; POST /peers/pricing/quote answers 200 OK with the priceCents for the trip
  • Every peer records each inbound call it receives, and answers 503 Service Unavailable or stalls for as long as it is scripted to
  • POST /peers/control/{peer}/fail, /stall, /heal and POST /peers/control/reset script a peer (rider, driver or pricing) so the failure paths can be tried by hand
  • PeerEndpoints resolves the address of each peer from the port the server actually started on
  • The Dispatch entity with its riderId, driverId, priceCents and status, its repository, and the error responses used above are provided
  • The hop timeout, the attempt budget, the failure window size and the wait before a hop is tried again are all declared in application.properties under the hop names rider, driver and pricing, with the two durations given in milliseconds

Sample Requests & Responses

Expand to view details on sample requests and responses.

POST /dispatches

Request:

{
   "riderId": 101,
   "pickup": "Pier 9",
   "dropoff": "Museum Row"
}

Response (201 Created):

{
   "dispatchId": 1,
   "driverId": "DRV-1",
   "priceCents": 698,
   "status": "DISPATCHED"
}

POST /dispatches

Request:

{
   "riderId": 102,
   "pickup": "Airport Terminal Four",
   "dropoff": "Riverside Depot"
}

Response (201 Created):

{
   "dispatchId": 2,
   "driverId": "DRV-2",
   "priceCents": 978,
   "status": "DISPATCHED"
}

POST /dispatches

Request:

{
   "riderId": 777,
   "pickup": "Pier 9",
   "dropoff": "Museum Row"
}

Response (404 Not Found):

{
   "error": "Rider not found with id: 777",
   "timestamp": "2026-08-11T10:11:30.076316Z",
   "path": "/dispatches"
}

POST /peers/control/rider/stall?calls=2&millis=2000 then POST /dispatches

Request:

{
   "riderId": 101,
   "pickup": "Pier 9",
   "dropoff": "Museum Row"
}

Response (504 Gateway Timeout):

{
   "error": "Rider service timed out",
   "timestamp": "2026-08-11T10:11:38.590454Z",
   "path": "/dispatches"
}

POST /peers/control/pricing/fail?calls=2 then POST /dispatches

Request:

{
   "riderId": 103,
   "pickup": "North Yard",
   "dropoff": "Sunset Park"
}

Response (503 Service Unavailable):

{
   "error": "Dispatch failed, please retry",
   "timestamp": "2026-08-11T10:11:38.654743Z",
   "path": "/dispatches"
}

More in Microservices and Resilience

See the module
Service Registry Lookup ClientThis problem covers looking a service address up before each call, then reaching that address over the network.—No data yethardEdge Router with Rate LimitingThis problem covers passing calls on to backing services by their path, while holding each caller to a call allowance.—No data yethardFallback Cache for Downstream OutageThis problem covers serving rates from a live feed and falling back to the last known answer while that feed is down.0%0 of 1 solvedhard

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.