KatInterview

Building a Reliable Earnings Pipeline for Gig Delivery Couriers

System DesignModel answer
HardModel answerSystem Designsystem-designidempotencyevent-driven

Last updated

Design a service that turns courier delivery lifecycle events into correct, auditable payouts, covering idempotency, ordering, time zones, data repair, and reconciliation.

Courier Payout Service

A delivery platform employs independent couriers. A Delivery service emits lifecycle events for every job, and a separate Payout service must turn those events into money owed to each courier.

Each event carries a courier ID, a job ID, a timestamp, and a status of accepted, fulfilled, or cancelled. Some simpler variants emit only accept and fulfill events.

Design the Payout service end to end and justify your choices in each area below.

  1. Interfaces. How does Delivery hand events to Payout (synchronous or asynchronous)? Define the event schema and the read API couriers use to see their earnings. What should that response include, and why?
  2. Pay rules. What is paid on each status, how do cancellations change pay, and which timestamp determines the period an earning belongs to? Explain how totals over a window and per-job breakdowns are computed.
  3. Time. How are timestamps represented? How do you handle time zones, daylight saving transitions, event ordering, and late or out-of-order events?
  4. Storage. Which tables, indexes, or in-memory structures support high write throughput, deduplication, lookups by courier and time range, and cheap recalculation?
  5. Idempotency and delivery guarantees. How do you avoid paying twice when the transport is at-least-once?
  6. Bad or missing data. Assume inputs are valid for the happy path, then explain how you detect and repair: a fulfill with no prior accept, an accept that never completes, duplicates, and out-of-order delivery.
  7. Batch vs. streaming. How do near-real-time earnings coexist with finalization at period close? How do you backfill and reconcile?
  8. Reliability. Cover retries, timeouts, circuit breakers, dead-letter queues, logging, and metrics.
  9. Consistency and operations. State your consistency guarantees, invariants you would check, monitoring and alerts, schema versioning and contract testing, and the trade-offs you accept.

If time allows, sketch the main classes and methods for event ingestion and payout calculation, plus the query API for a courier's earnings over a local-time pay period, including DST behavior.

Hints

Hint 1

Start with scale: a few million jobs a day is only hundreds to low thousands of events per second, so the difficulty is correctness and repair, not throughput.

Hint 2

Separate raw events (append-only, deduplicated by event ID) from derived state (a per-job state machine) and from money (an append-only ledger with a deterministic idempotency key per job and entry type).

Hint 3

Attribute pay to the event time of the terminal event, store everything in UTC, and convert local period boundaries with an IANA zone using half-open intervals so DST weeks are 167 or 169 hours.

Hint 4

Never mutate closed periods: corrections become reversal or adjustment entries in the current open period, and a reconciler compares against the Delivery source of truth to find stuck or missing jobs.