Get Your CertSystem Design & Architecture Interview QuestionsPlayCompete
‹ All System Design & Architecture Interview Questions questions

System Design & Architecture Interview Question 9: Idempotent Payment Consumer

System Design & Architecture Interview Questions · Test 1 · question 9 of 10

Retries & Idempotencyhard

A checkout service puts a "charge order" message on a queue that guarantees at-least-once delivery. A worker takes each message, calls the payment processor, and then acknowledges the message. A worker occasionally crashes after the processor has charged the card but before the acknowledgement, so the queue redelivers the message and the customer is charged twice.

Every order must be charged exactly once, even if a worker crashes at any point, and the queue cannot offer exactly-once delivery. Which design meets the requirement?

Select one answer
Show answer and explanation

Answer:

  • B. Create one idempotency key per order, send it with every charge attempt, and have the payment side return the stored result for a repeated key

With at-least-once delivery, duplicates cannot be prevented. They have to be made harmless. The consumer's side effect must be idempotent. A caller-supplied unique request identifier lets the receiving side recognise a retry. It records the key together with the outcome of the first successful request, and any later request with the same key gets the same result instead of a new charge. The key has to be created once per order (not once per attempt), so every redelivery carries the same key. The combination of at-least-once delivery and idempotent processing is what gives the "effectively exactly once" behaviour the stem asks for.

Option A makes delivery more reliable but does nothing about duplicates. Retrying the call is exactly how a second charge happens when the first one actually succeeded.

Option C turns the pipeline into at-most-once. If the worker crashes after acknowledging but before charging, the message is gone and the order is never charged. That breaks "every order must be charged."

Option D assumes redelivery means the charge succeeded. A message is also redelivered when the worker crashed *before* calling the processor, and skipping it would leave the order unpaid.

References: Making retries safe with idempotent APIs - Amazon Builders' Library; Competing Consumers pattern - Azure Architecture Center

Practice Test 1 ›

Study guides for System Design & Architecture Interview Questions