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.
- The user clicks on the "buy" button in the UI.
- The server processes the payment request.
- Unfortunately there is a network failure, and the user does not know if the first request was successfully processed.
- 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!
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.
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.
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.
Conclusion
- We can improve the fault tolerance and reliability of an application by considering idempotency.
- Idempotency can be implemented by leveraging unique identifiers.