What Is Idempotence?
Think About the “Pay Now” Button the Last Time You Bought Something Online. Now, Let’s Say You’re Ready to Purchase Your Shopping Cart Items, So You Press “Pay...
Think about the “Pay now” button the last time you bought something online. Now, let’s say you’re ready to purchase your shopping cart items, so you press “Pay now”. What happens if you press it multiple times? Will your credit card be charged for each time you hit the button?
This is where idempotence comes in: if the function in the “Pay now” button is idempotent, the user can tap the “Pay Now” button many times—yet their card is charged only once and they only make one purchase on their cart’s contents. If, alternatively, the function of the “Pay Now” button weren’t idempotent and the user anxiously pressed the key several times, the user would end up with multiple charges on and receive several of the same items in their mailbox.
So, idempotence is an important design for many functions in technology. Let’s take a look.
Idempotence: a technical definition
Idempotence is any function that can be executed several times without changing the final result beyond its first iteration. Idempotence is a technical word, used in mathematics and computer science, that classifies a function’s behavior. There are certain identity functions, such as a = a, that can be called idempotent.
As a function, it can be expressed as:
f(f(x)) = f(x)
That means that an operation can be performed on x to return y. Then, if that same function were performed on y, the result would still equal y.
Must Read
When to use idempotence
Idempotence is a safe practice for DevOps teams when developing applications. Idempotence ensures a safe, quality experience for both users and software teams. No one has to go in afterwards and clean up a mess.
For example, idempotence is the design standard for Ansible, a DevOps tool for system administrators to manage their servers. Ansible can automate the server setup process, consistently create the same servers, and automatically deploy them. There is a lot of orchestration that occurs in automating this process—and it is idempotent behaviors that ensure only one directory gets created on a server, and only an exact number of servers get created.
The idempotence function can be used in a variety of ways. Engineers want the destination to be the same, but the route that an orchestrator wishes to take can be different. If one function is called and the route it takes fails, another route can be tried. If one route lags, and a new one is fired, then, if both tasks make it to the endpoint, the result at the destination can still be the same.
Idempotent behavior is ever more crucial in cloud computing and edge devices. When multiple accounts can be logged in at once, accessed from different locations and different devices, updated from multiple user accounts, and data sent from one place to another, idempotent design helps manage the application’s state more efficiently and without error.