← Back to Questions
Microservices

What is Idempotency?

Learn What is Idempotency? with simple explanations, real-time examples, interview tips and practical use cases.

Idempotency is a concept in distributed systems, APIs, microservices, and HTTP communication where performing the same operation multiple times produces the same result as performing it once.

In simple words:

No matter how many times the same request is repeated, the final result should remain unchanged.


Simple Real-Time Understanding

Imagine switching on a room light.

  • First press turns ON the light
  • Second press on ON button still keeps light ON
  • Third press also keeps light ON

The result remains the same:

Light Status = ON

This behavior is called idempotency.


Why Idempotency is Important

In distributed systems and microservices, network failures and retries are common.

For example:

  • Client sends payment request
  • Server processes payment successfully
  • Network timeout occurs before response reaches client
  • Client retries same request again

Without idempotency:

  • Money may be deducted twice
  • Duplicate orders may be created
  • Duplicate emails may be sent

Real-Time Example Without Idempotency

Client
   |
   v
Payment Request
   |
   v
Payment Service
   |
   v
₹500 Deducted

Network timeout happens.

Client retries request again.

₹500 Deducted Again

Total deduction:

₹1000

This is a serious issue.


Real-Time Example With Idempotency

Client
   |
   v
Payment Request
   |
   v
Payment Service
   |
   v
Idempotency Key Checked

If request already processed:

  • Duplicate operation is prevented

Final deduction remains:

₹500

Main Goal of Idempotency

  • Prevent duplicate operations
  • Ensure data consistency
  • Handle retries safely
  • Improve reliability
  • Prevent duplicate payments and orders

Mathematical Meaning of Idempotency

If:

f(x) = f(f(x))

then the operation is idempotent.

Applying the operation multiple times gives same result.


Idempotency in HTTP Methods

HTTP Method Idempotent? Reason
GET Yes Only fetches data
PUT Yes Updates same resource repeatedly
DELETE Yes Deleting again changes nothing
POST No Usually creates new resource
PATCH Depends May partially update differently

GET Request Example

GET /users/1

Calling multiple times:

  • Always returns same user

No duplicate changes happen.


PUT Request Example

PUT /users/1

{
   "name": "Naresh"
}

Calling multiple times:

  • User name remains Naresh

Result does not change repeatedly.


DELETE Request Example

DELETE /users/1

First request deletes user.

Second request:

  • User already deleted

Final system state remains same.


POST Request Example

POST /orders

Calling multiple times:

  • Creates multiple orders

This is normally non-idempotent.


Why Idempotency is Critical in Microservices

Microservices often use:

  • Retries
  • Asynchronous messaging
  • Kafka events
  • Distributed transactions

Retries may create duplicate operations if idempotency is not implemented.


Example in E-Commerce Platform

Customer Places Order
        |
        v
Order Service
        |
        v
Payment Service

If Payment Service times out:

  • Retry may happen automatically

Without idempotency:

  • Duplicate payment occurs

Idempotency Key

Idempotency Key is a unique identifier attached to a request.

Example

Idempotency-Key:
ORD-12345

Server stores processed keys.

If same key comes again:

  • Duplicate request is ignored

Idempotency Flow

Client Request
      |
      v
Idempotency Key Sent
      |
      v
Server Checks Key
      |
------------------------------
|                            |
v                            v

Already Processed?         Not Processed?
      |                           |
      v                           v

Return Old Response        Process Request

Real-Time Payment Example

Step 1

POST /payments

Idempotency-Key: PAY123

Step 2

Server processes payment successfully.


Step 3

Client retries same request because of timeout.


Step 4

Server detects:

PAY123 already processed

No duplicate payment occurs.


Database Table Example

idempotency_requests

-----------------------------------------
| id | key      | status    | response |
-----------------------------------------
| 1  | PAY123   | SUCCESS   | Payment Done |
-----------------------------------------

Spring Boot Idempotency Example

Controller

@PostMapping("/payments")
public String processPayment(
    @RequestHeader("Idempotency-Key")
    String key
) {

    if(repository.existsByKey(key)) {

        return "Already Processed";
    }

    repository.save(key);

    return "Payment Success";
}

Idempotency with Kafka

Kafka consumers may receive duplicate events.

Consumers should process messages idempotently.


Kafka Duplicate Event Example

Order Created Event
      |
      v
Kafka Retry
      |
      v
Consumer Receives Event Again

Consumer checks:

  • Was this order already processed?

Idempotent Consumer Pattern

Consumers store processed message IDs.

Example

Processed Event IDs:

EVT101
EVT102
EVT103

Duplicate events are ignored.


Idempotency in Banking Systems

Banking systems heavily depend on idempotency because:

  • Duplicate transactions are dangerous
  • Retries happen frequently
  • Network failures are common

Example

Transfer ₹10,000

Without idempotency:

  • Money may transfer multiple times

Idempotency in Payment Gateways

Payment gateways like Stripe and Razorpay use idempotency keys to prevent duplicate payments.


Advantages of Idempotency

1. Prevents Duplicate Operations

Avoids duplicate payments and orders.


2. Safe Retries

Clients can retry safely after failures.


3. Improves Reliability

Distributed systems become more stable.


4. Data Consistency

Ensures correct final system state.


5. Better Fault Tolerance

Handles retries during failures gracefully.


Challenges of Idempotency

1. Additional Storage

Servers must store processed keys.


2. Increased Complexity

Duplicate detection logic becomes necessary.


3. Expiration Management

Old idempotency keys should be cleaned periodically.


Best Practices for Idempotency

  • Use unique idempotency keys
  • Store processed request IDs
  • Implement idempotent consumers
  • Use retries carefully
  • Handle distributed transactions properly

Idempotency vs Retry

Feature Idempotency Retry
Purpose Prevent duplicates Retry failed requests
Focus Consistency Reliability
Usage Duplicate prevention Temporary failures

Real-Time Company Example

Companies such as Amazon, Netflix, Stripe, PayPal, Razorpay, and banking systems heavily use idempotency to prevent duplicate transactions and duplicate operations.


Interview Ready Answer

Idempotency is a property where performing the same operation multiple times produces the same final result as performing it once. It is very important in distributed systems, microservices, payment systems, and APIs because retries may happen due to network failures or timeouts. Idempotency prevents duplicate operations such as duplicate payments, duplicate orders, or duplicate transactions. This is usually implemented using Idempotency Keys, processed request tracking, unique transaction IDs, or idempotent consumer patterns in Kafka-based systems.


Frequently Asked Questions

Why is idempotency important?

Because retries in distributed systems can create duplicate operations without idempotency.

Which HTTP methods are idempotent?

GET, PUT, and DELETE are idempotent.

Why is POST usually non-idempotent?

Because POST often creates new resources repeatedly.

What is an Idempotency Key?

A unique request identifier used to prevent duplicate processing.

Why is idempotency important in payment systems?

To prevent duplicate money deduction during retries or failures.

Why this Microservices question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.