Domenic Labbate

Idempotency is Important

Idempotency is Important
Published: October 2nd, 2023Last Edited: October 2nd, 2023
59 Views

Introduction

In the context of software engineering, idempotency is an important concept which plays a crucial role in improving the fault tolerance and reliability of an application.

To better understand this concept and its significance, let's start off by illustrating an example.

Example: Why Idempotency Matters

In the following example, we will discuss the impact of not considering idempotency.

Scenario

Let's imagine that we have a client (user) and a server. The user would like to purchase a product from the website.

  1. The user clicks on the "buy" button in the UI.
  2. The server processes the payment request.
  3. Unfortunately there is a network failure, and the user does not know if the first request was successfully processed.
  4. The user becomes frustrated and clicks the "buy" button a second time.

😡 However, later in the day the user notices that they were charged twice on their credit card bill!

User being charged twice because idempotency was not considered
Without Idempotency

What Went Wrong?

Why did this occur? From the server's perspective, it received two different requests and processed both of them successfully. The server had no way of knowing that both requests should not have been handled separately!

The Idempotent Solution

The solution to this problem is idempotency.

Idempotency Definition
Idempotency is the ability to safely retry an operation without unintended side effects.

How to Implement Idempotency

Now that we understand the definition of idempotency, let's discuss how we can actually implement it. One approach is to rely on unique identifiers (such as a UUID). This way, even if a request is sent multiple times, the system can recognize duplicates and handle them accordingly.

In our example, we can refactor the server to accept a transactionId field. If an operation is retried with the same transactionId, the server should only process it once.

That is actually how industry leaders such as Stripe and Square implement idempotency.

User not being charged twice because idempotency was considered
With Idempotency

Key Benefits

Let's review some key aspects that our system will benefit from.

  • Error Resilience: In the face of network failures or server errors, idempotent operations can be safely retried without introducing unwanted side effects.
  • Data Consistency: No matter how many times the operation is retried, the end result will be the same.
  • Improved User Experience: Users can interact with applications confidently, knowing that glitches will not result in unintended actions.

Idempotency is Ubiquitous

Another important detail to mention is that idempotency does not only apply to API endpoints. For example, idempotency is an important point to consider even when designing functions, message consumers, database operations, etc.

Idempotency is language-agnostic and ubiquitous.

Conclusion

  • We can improve the fault tolerance and reliability of an application by considering idempotency.
  • Idempotency can be implemented by leveraging unique identifiers.

References