What Is Idempotency? Designing Operations That Are Safe to Run Twice
"The job stopped halfway, so I ran it again." Whether that is safe depends on one property: whether the operation is idempotent . An operation is idempotent if running it many times leaves the system in the same state as running it once . Network failures, timeouts, double clicks, and overlapping schedules all mean the same operation will eventually run twice. This article contrasts idempotent…
Idempotency is a crucial concept when designing operations that can be safely executed multiple times without causing unintended side effects. An operation is considered idempotent if running it more than once yields the same result as running it just once. This property becomes essential under various circumstances, such as network failures, timeouts, double clicks, or overlapping schedules that may result in the same operation being triggered multiple times.
To illustrate this, let's consider a couple of examples. Pressing the "up" button on an elevator is idempotent because pressing it once or several times will still bring the elevator to the same floor. On the other hand, performing the operation "subtract 100 from the balance" is not idempotent; doing it twice would reduce the balance by 200 instead of just 100. Conversely, the operation "set the balance to 900" is idempotent because running it any number of times will always result in the balance being 900.
In this article, we'll explore idempotent and non-idempotent operations through an experiment involving simple SQL statements executed twice. Using an in-memory SQLite database, we create an account with a balance of 1,000 and an empty table. By running each statement twice, we observe the outcome and determine whether the operation is idempotent.
The experiment reveals that operations that change the state relative to the current state (like subtracting 100 from the balance) are not idempotent, while those that set the state to a specific value (like setting the balance to 900) are idempotent.
Why is running an operation twice a concern? There are several scenarios where an operation might need to be retried, such as connection drops, double clicks, overlapping schedules, or restarting a job after a partial failure. In these cases, ensuring the operation is idempotent is crucial for maintaining the consistency and reliability of the system.
To make an operation idempotent, there are several common approaches:
1. Write state, not deltas: Instead of using relative changes (e.g., "subtract 100"), express the operation in terms of the desired end state (e.g., "set to 900"). This approach ensures that running the operation multiple times does not alter the outcome beyond the initial execution.
2. Use unique constraints to reject duplicates: When appending data is unavoidable, use a unique constraint to identify and prevent duplicate entries. In the experiment, the UPSERT operation leverages a unique index on column k to update existing rows instead of inserting duplicates.
3. Employ an idempotency key: For operations without a natural key, generate a unique identifier for each intended operation and include it with the request. The server records this key and returns the earlier result if the same key is encountered again. This approach helps differentiate between intended operations and their results.
4. Leave a "done" marker: For batch and migration jobs, record the progress made and skip completed parts on rerun. This technique ensures that previously processed data is not reprocessed unnecessarily.
It is important to note that idempotency does not necessarily imply that the operation is harmless or without side effects. Deleting a file, for instance, is an idempotent operation since running it again will not cause any harm if the file has already been deleted. However, the first execution of the operation still has irreversible consequences.
When designing idempotent operations, it's essential to consider potential issues that may arise if the operation is retried or executed concurrently. By carefully evaluating how the operation handles these situations, you can ensure that running it multiple times does not lead to unexpected or undesirable results.
In the context of data-format migrations, idempotent operations are particularly valuable. These migrations can be designed to check whether the data is already in the desired format before proceeding. If the data is already in the correct state, the migration can be skipped entirely, preventing unnecessary processing and ensuring a smooth recovery after any interruptions.
In conclusion, idempotency is a vital property of operations that need to be executed reliably in various scenarios. By understanding the concept of idempotency and implementing strategies to make operations idempotent, developers can create systems that remain consistent and robust, even when faced with retries, failures, or overlapping schedules.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to
More from Dev.to on urgent.news
Report a problem with this story
Get the day's news by email. Daily, weekly or monthly. Sign up