---
title: "Discussion on \"System Design Drill Day 9: Idempotency Keys for Payments and Retries\" | Hashnode"
description: "Discussion on \"System Design Drill Day 9: Idempotency Keys for Payments and Retries\". Why retries make duplicates inevitable, and how idempotency keys stop double charges and double stock decrements."
url: "https://hashnode.com/posts/system-design-drill-day-9-idempotency-keys-for-payments-and-retries/6ae01711d3f4c6020cdfe5b7"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [System Design Drill Day 9: Idempotency Keys for Payments and Retries](https://blog.anishansari.com.np/system-design-drill-day-9-idempotency-keys-for-payments-and-retries?utm_source=hashnode&utm_medium=feed)

Why retries make duplicates inevitable, and how idempotency keys stop double charges and double stock decrements.

Oct 27 · 7 min read · blog.anishansari.com.np  
Read the article

-   [#interview-preparation](https://hashnode.com/tag/interview-preparation)
-   [#system-design](https://hashnode.com/tag/system-design)
-   [#backend](https://hashnode.com/tag/backend)
-   [#laravel](https://hashnode.com/tag/laravel)
-   [#distributed-systems](https://hashnode.com/tag/distributed-systems)

## Written by

[Anish Ansari](https://hashnode.com/@anish78)

@anish78 · kathmandu, Nepal

  
[View profile](https://hashnode.com/@anish78)

Food,Code and Cloud Computing. You can support here :https://www.buymeacoffee.com/anishansari

## Responses 4

Markdown supported · ⌘↵ to post

[Crownforge](https://hashnode.com/@thecrownforge)

[Oct 27](https://hashnode.com/posts/system-design-drill-day-9-idempotency-keys-for-payments-and-retries/6ae01711d3f4c6020cdfe5b7/comment/6ae078b2375e3174907ab22e)

I would add a crash test after the order transaction commits but before the key is marked completed. On retry, how do you find the existing order and avoid decrementing stock again? The provider key covers the charge, but that local step needs a guard too

[Anish Ansari](https://hashnode.com/@anish78)

[Oct 27](https://hashnode.com/posts/system-design-drill-day-9-idempotency-keys-for-payments-and-retries/6ae01711d3f4c6020cdfe5b7/comment/6ae078b2375e3174907ab22e#reply-6ae07c3b87f3dfa3c62971ba)

Good catch, that gap is real. I'd write the idempotency key onto the order row with a unique index inside the same transaction as the stock decrement, so a retry that takes over the key finds the existing order, skips the decrement and just marks the key completed.

[Abhinav Sharma](https://hashnode.com/@abhinavs1920)

[Oct 27](https://hashnode.com/posts/system-design-drill-day-9-idempotency-keys-for-payments-and-retries/6ae01711d3f4c6020cdfe5b7/comment/6ae0467ff19b693ef98ba12c)

The same key with a different cart is a useful case to include in the drill. The happy-path retry test could pass while that mix-up still returns a result for the wrong request.

[Anish Ansari](https://hashnode.com/@anish78)

[Oct 27](https://hashnode.com/posts/system-design-drill-day-9-idempotency-keys-for-payments-and-retries/6ae01711d3f4c6020cdfe5b7/comment/6ae0467ff19b693ef98ba12c#reply-6ae050e128e89b36c825759d)

Agreed Abhinav, that's the test that actually proves the request hash check works. Same key with a different cart should come back as a 422, not a replay of the old response.
