Lesson 9 of 14
โฎPrev Nextโฏ

MSD-101 ยท Deep dives: hard problems, solved

Payments without double charges

โฑ 14 min ๐ŸŽฎ Hands-on โ˜ Azure

9of 14

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    A payment must happen exactly once in a world where networks drop and queues deliver at least once. Put the charge on a queue, keep one ledger record per trip (a unique key), send an idempotency key to the payment provider, trust only signed webhooks, and retry with backoff and a limit into a dead-letter queue.

    • At-least-once delivery
    • Idempotency at every hop
    • Signed webhooks
    • Retries that do not storm

    Try it

    Play with the design before you read about it. Everything runs on this page.

    No Azure subscription needed. Read the requirement, see the architecture, build it in a mock portal, send a booking through it, then break it.

    โ˜ Simulate on Azure: Payments without double charges

    Priya's trip ends. Her saved card must be charged RM 32 exactly once, even if the payment provider is slow, a message is delivered twice, or someone sends a fake 'paid' message.

    It must

    • Charge the saved card when a trip completes
    • Mark the trip PAID only when the provider confirms
    • Send the rider a receipt
    • Never store card numbers on our servers

    With these numbers

    Double charges
    0
    Paid within
    p95 < 30 s
    Card data
    Tokens only
    Audit
    Every attempt logged

    ๐ŸŽ“ Education-only simulation. Nothing is created in Azure and no subscription is needed.

    1. The problemA support ticket: โ€œYou charged me RM 64 for a RM 32 ride.โ€ The logs show one trip, two charge calls, 900 ms apart. The first call worked; its reply was lost.
    2. The design ideaEvery hop may repeat. Make every repeat harmless.
    3. In real lifePayment providers document idempotency keys because double charges are the number one payment support ticket. Teams that add them at every hop rarely see the problem again.

    Start with a question

    A support ticket: โ€œYou charged me RM 64 for a RM 32 ride.โ€ The logs show one trip, two charge calls, 900 ms apart. The first call worked; its reply was lost.

    Every hop between the app and the bank can repeat. The design must make repeats harmless.

    Explain it like I'm 10

    When you pay at a shop and the machine freezes, the cashier checks the receipt number before swiping again. The receipt number is the idempotency key.

    The technical explanation

    Queue the work. When a trip completes, the trip service puts a charge command on Azure Service Bus with MessageId = tripId. Turn on duplicate detection (Standard or Premium) so a repeated message in the window is dropped.

    One record per trip. The payment service inserts a PENDING record in Cosmos DB with a unique key on /tripId. A second insert for the same trip fails, so two workers cannot both charge.

    Idempotent provider call. Send an Idempotency-Key (for example the tripId) with the charge. If the reply is lost and you retry, the provider returns the first result. Read the provider key from Key Vault with a managed identity.

    Confirm safely. Mark PAID only when the providerโ€™s signed webhook arrives; API Management verifies the signature. Retry failures with exponential backoff and jitter; after the max delivery count the message goes to the dead-letter queue for a human.

    Repeat sourceGuard
    App retries the trip endIdempotency-Key on the API
    Queue delivers twiceDuplicate detection + unique key
    Worker retries the chargeIdempotency-Key to the provider
    Webhook arrives twiceLedger state check (already PAID)
    Fake webhookSignature verified at the gateway

    โ˜ Simulate on Azure๐Ÿงฉ Patterns

    Real-world scenario

    Payment providers document idempotency keys because double charges are the number one payment support ticket. Teams that add them at every hop rarely see the problem again.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. Service Bus delivers a message twice. What protects the charge?
    2. 2. Which value makes a good idempotency key for a trip charge?
    3. 3. When should the trip be marked PAID?
    4. 4. The provider is down for 3 minutes. What retry policy is right?
    5. 5. Which Service Bus tier supports duplicate detection?

    Pass the quiz to complete this lesson.

    Next: Fares and surge pricing โ†’

    Questions people ask

    How do payment systems avoid double charging?

    Idempotency keys on every call, one ledger record per order (a unique key), duplicate detection on the queue, and confirmation only from signed provider webhooks.

    What is a good idempotency key for a payment?

    A business id such as the trip id, reused on every retry of the same charge. A new random id for each attempt protects nothing, because the provider sees every retry as a new payment.

    Does Azure Service Bus deliver each message exactly once?

    No. Service Bus delivers at least once, so a message can arrive twice. Turn on duplicate detection (Standard or Premium tier) and keep a unique key per trip in the ledger, so a repeat cannot charge again.

    Should a mobile app call the payment provider directly?

    Only for card entry with the providerโ€™s own SDK, which returns a token. Charging happens on your server with the token and an idempotency key.

    Interview questions

    How this topic comes up in system design interviews. Answer out loud first, then open the model answer.

    intermediate Design the charge flow after a ride.

    TripCompleted โ†’ Service Bus (MessageId = tripId, dedup on) โ†’ payment service inserts PENDING (unique /tripId) โ†’ charge with Idempotency-Key โ†’ signed webhook via API Management โ†’ PAID โ†’ receipt push; backoff, max tries, dead-letter queue.

    advanced How do you reconcile with the provider each night?

    Pull the providerโ€™s settlement report, compare with the ledger by tripId, flag missing, extra or mismatched amounts into a review queue, and never auto-refund without a rule or a human.

    Quick revision

    Remember

    • At-least-once delivery
    • Idempotency at every hop
    • Signed webhooks
    • Retries that do not storm
    • Use a business id (tripId) as the idempotency key, not a random id per attempt. A new random id on each retry protects nothing.

    One line to remember

    โ€œEvery hop may repeat. Make every repeat harmless.โ€

    Official documentation