Part I – What is Idempotence?

This is part 1 of a 4 part series related to the concept of idempotency as it relates to computer science and software engineering.

Idempotence: a term used in mathematics and computer science to describe a function that can be executed indefinitely but results in the same outcome.

Think of your phone. When you click the home button, it will take you to the home screen, regardless of what you are doing on the phone. Pressing the home button multiple times will not change the effect – the result is always the same.

Math

In math idempotence describes an operation which results in the same answer regardless of how many times the operation is executed.

0 * 0 = 0 
1 * 1 = 1
50 = 1 
51 = 5 

In other words:

x * x = x  (where x = 0 or 1)
yx = 1  (where x = 0)
yx = y  (where x = 1)

This is always true where x = 0 or 1, no matter how many times you multiply by x. Stated differently: x * x is idempotent where x is 1 or 0.

x * x = x * x = x * x = x (and so on)
5x = 1x = 1x = 1x = 1 (and so on - where x = 0)
5x = 5x = 5x = 5x = 5 (and so on - where x = 1)

This will not be true for any other number (x * x is not idempotent where x is not 0 or 1)

2 * 2 != 2
3 * 3 != 3
1.5 * 1.5 != 1.5
52 != 1 
52 != 5 

Computer Science

In computer science, idempotence refers to the effect that a function has on a resource. A function that is idempotent will have the same effect on the resource regardless of how many times it is called.

Idempotency is often  associated with RESTful services but this concept has been around for some time and affects other aspects of software engineering.

Database Development

Executing scripts to modify data in the database should be written in a way that will allow them to be executed multiple times if necessary. Idempotence with database scripts will be discussed in greated detail in Part II of this series.

  • Script to fix incorrect data in a database
  • Scripts to update multiple records – such as updating the price on all products in a specific category.
  • Scripts to migrate data with database updates

Imports or transformations in an ETL process

Care should be taken when processing files or performing transformations during an ETL data flow in the event that the entire data flow does not complete successfully, and all or part of the ETL process must be re-run. We will discuss this in more detail in Part III.

  • Processing files and deleting or archiving them
  • Transformation scripts to cleanse data

Processing RESTful web service calls

If you strictly follow REST principles when building your API methods, the following method types will be idempotent. Part IV will be dedicated to idempotence in RESTful services.

  • GET, PUT, DELETE, HEAD, OPTIONS, TRACE are idempotent
  • POST is not idempotent


Categories: Database, Misc, Web

Tags: , , , , ,

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Gregor Dzierzon

Subscribe now to keep reading and get access to the full archive.

Continue reading