Design an Idempotent Payment Processor That Handles Client Retries
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design an idempotent payment processing system that processes each payment request only once, even if the client retries the same request several times after network timeouts or other failures.
Each payment request is identified by a unique idempotency key. When a request with a given key is received for the first time, the payment is processed and the result is stored. If the same idempotency key is used again, the system returns the previously stored result without processing the payment again.
The processor starts with an `initial_balance`. Each successful payment deducts the `amount` from the balance. If the balance is insufficient, the payment is declined. Duplicate submissions (retries with the same idempotency key) never modify the balance, regardless of whether the original was accepted or declined.
Implement a class `IdempotentPaymentProcessor(initial_balance: int)` with these methods:
- `submitPayment(idempotency_key: str, amount: int, currency: str) -> str`: submit a payment request identified by `idempotency_key` and return its outcome as described below.
- `getStatus(idempotency_key: str) -> str`: return the status of a previously submitted payment: `ACCEPTED`, `DECLINED`, or `NOT_FOUND` if the key was never used.
- `getBalance() -> int`: return the current account balance.
The outputs follow these rules:
- A new key whose `amount` is at most the current balance is accepted. The balance drops by `amount` and the call returns `ACCEPTED:txn_XXXX`.
- A new key whose `amount` is greater than the balance returns `DECLINED:INSUFFICIENT_FUNDS` and leaves the balance unchanged. Both accepted and declined outcomes are stored under the key.
- Transaction IDs are `txn_` followed by a sequential counter padded with zeros to at least four digits (`txn_0001`, `txn_0002`, and so on). Only accepted payments advance the counter.
- A key that was already used returns `DUPLICATE:` followed by the stored transaction ID when the original was accepted (for example `DUPLICATE:txn_0001`), or `DUPLICATE:DECLINED` when it was declined. The key alone decides: the amount and currency of the repeated call are ignored.
- `currency` is recorded with the payment and does not affect the balance.
Example 1, a retry of an accepted payment:
```text
p = IdempotentPaymentProcessor(100)
p.submitPayment("order-1", 30, "USD") -> "ACCEPTED:txn_0001"
p.submitPayment("order-1", 30, "USD") -> "DUPLICATE:txn_0001"
p.getBalance() -> 70
p.submitPayment("order-2", 50, "USD") -> "ACCEPTED:txn_0002"
p.getStatus("order-2") -> "ACCEPTED"
p.getBalance() -> 20
```
The retry of `order-1` returns the stored transaction ID and the balance stays at 70. `order-2` is a new key, 50 is at most 70, so it is charged and gets the next ID.
Example 2, a declined payment and its retry:
```text
p = IdempotentPaymentProcessor(50)
p.submitPayment("a-1", 80, "EUR") -> "DECLINED:INSUFFICIENT_FUNDS"
p.submitPayment("a-1", 10, "EUR") -> "DUPLICATE:DECLINED"
p.getBalance() -> 50
p.submitPayment("a-2", 50, "EUR") -> "ACCEPTED:txn_0001"
p.getStatus("a-1") -> "DECLINED"
p.getStatus("a-3") -> "NOT_FOUND"
p.getBalance() -> 0
```
The second call reuses `a-1`, so it replays the stored decline even though 10 would fit in the balance. `a-2` asks for exactly the balance, which is allowed, and gets `txn_0001` because the decline did not use a transaction number.
```hint Order of checks
Decide what to look up before you look at the balance. A retry of a declined payment must not be evaluated again, even when its new amount would fit.
```
### Constraints & Assumptions
- `idempotency_key` has 1 to 100 characters, all letters, digits or hyphens.
- `amount` is a positive integer of at most 1,000,000.
- `currency` is a non-empty string.
- `initial_balance` is between 0 and 1,000,000,000.
- At most 10,000 calls are made in total.
### Clarifying Questions to Ask
- Can two requests with the same key be in flight at the same time, or do calls arrive one after another?
- Do stored keys ever expire, and if so, what should a retry after expiry return?
- Can the account receive deposits or refunds, so that a payment declined under one key could succeed later under a new key?
### What a Strong Answer Covers
- Looking up the idempotency key before any balance check, so retries never reach the payment logic.
- Storing the outcome of every first request, declines included, along with the transaction ID for acceptances.
- The exact output strings, including both `DUPLICATE` forms and the case where the amount equals the balance.
- A transaction counter that advances only on accepted payments, with the zero-padded ID format.
- The cost per call and the memory used by stored keys.
### Follow-up Questions
- Two retries with the same key arrive at the same moment on different threads or servers. How do you make sure only one of them charges the account?
- How would you persist the records so that a crash between charging the account and storing the result cannot lead to a double charge?
- A client reuses a key with a different amount. Should the processor still replay the stored result, and how would you detect the mismatch?
- How long should idempotency keys be kept, and how would you expire them without breaking late retries?
Overview: Design an in-memory payment processor that uses idempotency keys so client retries never charge an account twice, returning the stored outcome for any repeated request. It tests idempotent API design, state handling for accepted and declined payments, and exact output formatting.