Idempotency in Scalable Distributed Architectures — Example

Debasis Das
3 min readSep 28, 2023

1. What is Idempotence :

Idempotence is a property that ensures that an operation or action can be performed multiple times without changing the result beyond the initial application. In simpler terms, if the same request is sent to a microservice endpoint multiple times, the result should remain consistent, as if the request was only made once.

2. Application of Idempotency :

  1. Order Processing (Rest API) : For instance, in an e-commerce application, there can be order creation requests (Rest API) that could potentially fail but can be retried automatically. However, this could often lead to cases where your orders can be duplicated in the system. By adopting idempotency, you can guarantee that the order is created only once, regardless of when the request is retried.
  • In the REST API concept, GET, PUT, UPDATE, DELETE, and OPTIONS are idempotent operation since they don’t change anything on the server and returns the same response every time.
  • While POST is not idempotent since it creates a new resource on the server/database every time we execute it.
  • Example — Problem :

. Example — Solution using idempotency :

Use idempotency table to store all idempotency key with status. If idempotency key is not available in db, then process request. Otherwise simple return to user duplicate request (status : “Received”)or request already processed (Status :“Processed”)response.

2. Inventory Management (Distributed system/ Distributed transaction) : When updating inventory levels in microservice after a purchase, idempotency ensures that stock quantities are adjusted correctly regardless of network hiccups or retries, preventing inaccuracies in inventory levels.

Use different different idempotency table to store all idempotency key with status for each microservice. If idempotency key is not available in db, then process request. Otherwise simple return to user duplicate request (status : “Received”) or request already processed (Status :“Processed”)response.

3. Advantages of idempotency in scalable distributed system :

  1. In microservices, where data flows across multiple services, idempotency ensures that updates are uniform across the board. This maintains data integrity and coherence
  2. Network disruptions or service failures are common in distributed systems. Idempotent operations allow for reliable retries without concern for negative outcomes.
  3. The scalability and fault tolerance of microservices hinge on idempotent operations. They permit horizontal scaling and retries without jeopardizing system stability.
  4. Non-idempotent operations risk unintentional consequences when repeated. Idempotency guarantees that executing the same operation multiple times doesn’t introduce unexpected side effects.

Summary : That’s it! In this post, we understand about idempotency use in post rest API and in scalable distributed system.

Thank you for reading my blogs ……..

--

--

Debasis Das

Senior software developer | Typescript | Node.js | Micro-service | Large scale distributed system | LLD | HLD | Reactjs