0
0
HLDsystem_design~3 mins

Why Idempotency for safe retries in HLD? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if clicking a button twice could never cause a mistake or double charge?

The Scenario

Imagine you are ordering a pizza online. You click the "Place Order" button, but the internet is slow. You wonder if the order went through, so you click the button again. Now, you might get charged twice and receive two pizzas.

The Problem

Without a way to safely retry, repeated requests can cause duplicate actions. This leads to errors like double charges, repeated messages, or corrupted data. Manually checking and fixing these issues is slow and error-prone.

The Solution

Idempotency means making sure that repeating the same request multiple times has the same effect as doing it once. This way, if you retry due to a failure or timeout, the system safely ignores duplicates and keeps data consistent.

Before vs After
Before
processOrder(request)
// No check for duplicates
// Multiple calls create multiple orders
After
if (isDuplicate(request.id)) return previousResult
processOrder(request)
// Only creates order once
What It Enables

Idempotency enables safe retries without fear of duplicate effects, making systems more reliable and user-friendly.

Real Life Example

Payment gateways use idempotency keys so if your payment request times out and you retry, you won't be charged twice.

Key Takeaways

Manual retries can cause duplicate actions and errors.

Idempotency ensures repeated requests have no extra side effects.

This makes systems safe, consistent, and easier to use.