KatInterview

Fixing a Non-Atomic Charge-Then-Record Trip Payment Flow

System DesignModel answer
MediumModel answerSystem Designidempotencypaymentsdistributed-systems

Last updated

Review a Java method that charges a card through an external processor and then saves a trip row, and explain how failures cause lost or duplicate charges and how to make it safe.

A ride-hailing backend contains the Java service below. When a trip ends, it collects the fare from the rider's card using an external payment processor, then saves a record of the trip in the local database.

import java.util.Map;

public class FareCaptureService {
    private final CardProcessor processor;
    private final Database db;

    public Map<String, String> captureFare(String riderId, int fareCents) {
        String captureId = processor.capture(riderId, fareCents);
        db.insert("trips", Map.of(
            "rider_id", riderId,
            "fare_cents", fareCents,
            "capture_id", captureId
        ));
        return Map.of("status", "ok", "capture_id", captureId);
    }
}

Assumptions:

  • processor.capture performs a network call that actually moves money. It returns a processor-assigned capture ID and throws if it fails.
  • db.insert writes one row and has no other side effects.
  • The caller (a dispatch service) may retry when it times out or sees an error.

Question. Suppose the process running this method crashes, or a call times out, partway through. What can go wrong? How would you change the design and the code so that riders are never charged twice and no charge goes unrecorded?

This is a discussion exercise. Your code does not need to compile, but your proposed fix should be concrete.

A strong answer should:

  1. Identify each failure point in and around the two calls and the resulting inconsistent state (money moved but unrecorded, money moved twice, or outcome unknown).
  2. Explain why simply wrapping the work in a database transaction does not solve the problem.
  3. Propose a mechanism that makes retries safe, and show how it is enforced both locally and at the processor.
  4. Say what the method should return or throw when the capture succeeded but the save failed, versus when the capture call timed out with an unknown result.
  5. Describe a background process that repairs any remaining inconsistencies.

Hints

Hint 1

List every moment at which a crash or lost response could happen: before the call, while the request is in flight, after the processor answers but before the row is written, and after the row is written. For each, ask whether money moved and whether the database knows.

Hint 2

A local database transaction can only roll back local writes. Think about which of the two steps it cannot undo, and what that means for retries.

Hint 3

Make a retry recognizable as the same attempt. Who should create the identifier, when, and where must it be checked (your database and the processor)?

Hint 4

Record intent before acting: store a pending row first, then update it to captured, failed, or unknown. Then think about a periodic job that resolves rows that stay pending or unknown.