Skip to the content.

What is Idempotency?

👉 Idempotency means: Performing the same operation multiple times gives the same result as performing it once.


Idempotency in System Design

It’s super important in distributed systems because failures, retries, and network glitches are common.

Where it helps

  1. API Reliability

    • If a client retries a request (due to timeout or network error), an idempotent API ensures the state isn’t corrupted.
    • Example: Payment APIs → You don’t want to charge a customer twice if they clicked “Pay” twice or system retried.
  2. Fault Tolerance in Distributed Systems

    • Systems like message queues or microservices often retry events.
    • Without idempotency → you may process the same event twice.
    • With idempotency → multiple deliveries are safe.
  3. Database Operations

    • INSERT is usually not idempotent (adding duplicates).
    • UPSERT (insert if not exists, else update) is idempotent.
    • DELETE or UPDATE by unique ID is idempotent.

How to Implement Idempotency


Example

Imagine a payment service:

Result: Only one charge.


Summary


Most systems don’t start with idempotency in place until they scale or hit a real-world problem like duplicate payments, retries, or double bookings.

Let me explain why and how it’s used in practice:


1. Idempotency Keys (in payments & APIs)

âś… Example:

This way, retries are safe.


2. Database UPSERT (ON CONFLICT DO NOTHING / UPDATE)

âś… Example (Postgres):

INSERT INTO orders (order_id, user_id, amount) 
VALUES ('12345', 101, 500) 
ON CONFLICT (order_id) 
DO NOTHING;

3. Why many teams skip it early


4. Real-world example


âś… In short: