• Skip to main content
  • Skip to primary sidebar
BMA

BeMyAficionado

Inspire Affection

Idempotence Is Very Critical In Backend Applications

August 19, 2021 by varunshrivastava Leave a Comment

Wait… before you start reading… get this – it is a very important topic.

Maybe you are someone who already knows about idempotence or someone who is hearing this word for the first time. This article is going to help you all in some way or the other.

I’m a backend engineer. No that’s not right… I’m a full-stack developer specialised in backend development. Yeah, that’s close enough. The point is I’ve always known about the concept of idempotence but I never realised its importance until a few weeks ago when something bad happened.

That’s why I said at the start – Idempotence is very important in backend applications/services.

Table of Contents

  • What is Idempotenct?
  • What do we mean by Idempotent in HTTP methods?
  • Why Is Idempotence Important?
  • Illustration
      • Non idempotent example
      • Example 1
      • Idempotent by minute
      • Example 2 - Idempotent by Minute
  • Take Away

What is Idempotenct?

Let’s start with a simple dictionary definition and from there will see what it means when writing backend services.

From the above definition, any element is unchanged in value even after multiple operations. In mathematics, an idempotent operation is one where f(f(x)) = f(x). For example, the abs() function is idempotent because abs(abs(x)) = abs(x) for all x.

Let me make it functional.

Try the below example (enter 1, 2 or 3 and press discard multiple times)

  • Item 1
  • Item 2
  • Item 3

No matter how many times you press that discard button, the state of the list will change once... on the first operation. The result will be unchanged no matter how many times you press the discard button. This is a very short but complete example of idempotence.

What do we mean by Idempotent in HTTP methods?

I think now definitions will start to make more sense.

Here's one in regards to HTTP methods.

An HTTP method is idempotent if an identical request can be made once or several times in a row with the same effect while leaving the server in the same state. In other words, an idempotent method should not have any side effects (except for keeping statistics). Implemented correctly, the GET, HEAD, PUT, and DELETE methods are idempotent, but not the POST method. All safe methods are also idempotent.

You see I have underlined, bold and colored the word Implemented Correctly in the above definition. Let's say some really smart developers used the DELETE method in a way that it deletes the last entry from the table. Then this is not idempotent... because now... with every delete call, it will delete the last entry from the table. That means every DELETE request is changing the state of the system. Therefore, it's not implemented correctly.

Wait!!! I can quickly demonstrate this behaviour.

Here,

  • Item 1
  • Item 2
  • Item 3

So as long as the methods are implemented safely, the GET, HEAD, PUT, and DELETE methods are idempotent.

In case you want to read some more in detail, here's a good resource,

  • https://developer.mozilla.org/en-US/docs/Glossary/Idempotent

Why Is Idempotence Important?

Coming to the meaty part now. After all, why is this so important that I decided to write this article in the middle of the night. One reason is I cannot sleep and the other is that you might lose sleep (and money) if you are working on critical applications that directly result in revenue for the company.

Let's take a simple real-world example. Suppose you are a customer who took some loan from ME. Now let's say I have a service that talks to some other service that does the interest calculation for ME (because I'm lame with calculations :p). The setup would look somewhat like this,

The key to understanding this example is to think dumb. Seriously!

At this point, if there is some communication delay, I will send the message again and the interest will be accrued twice for the same day. And you will sue me at the end and I will lose a fortune.

In this case, the third person is not idempotent. Because every message is changing the state of the system.

So, to avoid the recalculation or charging YOU twice, I can make this service (another person) idempotent by asking him to check the message before doing the calculation.

Now... this is important. You should be very careful in choosing the right key attribute that will always be the same for the given message.

In this example - I don't want to accrue interest twice for the same person (YOU) more than once for the same day. So the key attributes will be -

  • User Id
  • Date

So, If I'm sending a message with the same user id and on the same date twice then the third person will discard the following messages.

This is the benefit of idempotence.

This can save my company from getting sued over a small issue :p 😀

Illustration

In the below example, think of the number as the interest accrued. And the increment button is ME telling the calculator guy to accrue interest.

In the non-idempotent solution, the number will increment every time you hit that button.

In the idempotent solution, it will only increment once per minute. (Think of minute as a day to relate with the above example)

Non idempotent example

You can hit the increment button N times and every time it will increment the number. But that is not the case with example 2.

Example 1

1

Idempotent by minute

You can only increment the number after each minute.

You can imagine a minute as a day for a reference. So the number will only increment once for a given minute. Wait for another minute. So, no matter how many times you press the button, it will only increment once in a minute.

This is the best I can do in a short time to illustrate idempotence.

Example 2 - Idempotent by Minute

1

Take Away

The one takeaway that I want you from this article is that - Do give a lot of thought before writing a service or REST API that will change the state of your system. Especially if it is going to have a financial consequence.

This is something even experienced developers overlook and sometimes the price is really huge.

I'm one of the lucky few who have seen a considerable impact that could have been avoided if the idempotence key would have been chosen thoughtfully. And this happens.

I hope you enjoyed the article.

If you liked the article then do something through which I would know that you liked it. The easier way is to comment below 🙂

Till next time.

Related

Filed Under: Blogging Tagged With: idempotence, microservices, programming

Primary Sidebar

Subscribe to Blog via Email

Do you enjoy the content? Feel free to leave your email with me to receive new content straight to your inbox. I'm an engineer, you can trust me :)

Join 874 other subscribers

Latest Podcasts

Recent Posts

  • Is The Cosmos a Vast Computation?
  • Building Semantic Search for E-commerce Using Product Embeddings and OpenSearch
  • Leader Election with ZooKeeper: Simplifying Distributed Systems Management
  • AWS Serverless Event Driven Data Ingestion from Multiple and Diverse Sources
  • A Step-by-Step Guide to Deploy a Static Website with CloudFront and S3 Using CDK Behind A Custom Domain

Recent Comments

  • Varun Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Vaibhav Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Varun Shrivastava on Should Girls Wear Short Clothes?
  • D on Should Girls Wear Short Clothes?
  • disqus_X5PikVsRAg on Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java

Categories

  • Blogging
  • Cooking
  • Fashion
  • Finance & Money
  • Programming
  • Reviews
  • Software Quality Assurance
  • Technology
  • Travelling
  • Tutorials
  • Web Hosting
  • Wordpress N SEO

Archives

  • November 2024
  • September 2024
  • July 2024
  • April 2024
  • February 2024
  • November 2023
  • June 2023
  • May 2023
  • April 2023
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • November 2021
  • September 2021
  • August 2021
  • June 2021
  • May 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016

Tags

Affordable Hosting (4) algorithms (4) amazon (3) aoc-2020 (7) believe in yourself (4) best (4) database (4) earn money blogging (5) education (4) elementary sorting algorithms (4) experience (3) fashion (4) finance (6) Financial Freedom (7) food (7) friends (3) goals (5) google (5) india (10) indian cuisine (5) indian education system (4) java (16) life (16) life changing (4) love (4) make money (3) microservices (9) motivation (4) oops (4) podcast (6) poor education system (4) principles of microservices (5) problem-solving (7) programmer (5) programming (28) python (5) reality (3) seo (6) spring (3) success (10) success factor (4) technology (4) top 5 (7) typescript (3) wordpress (7)

Copyright © 2025 · Be My Aficionado · WordPress · Log in

Go to mobile version