What is "Idempotency Messages", "idempotentive", "idempotency"?

Asked

Viewed 314 times

18

I have a question on the subject here, and a article on the subject even in Wikipedia, but I still don’t understand the subject, so the questions are,

  • What is Idempotency Messages?
  • How and where it should be used?
  • Simple example of use?

3 answers

16


An idempotent operation can be understood as something that establishes a value or state rather than modifying it.

A simple example could be the bank balance. Imagine the statement below:

Data             Valor        Operação
01/01/2017      100,00        Abertura de conta + Depósito inicial
02/01/2017      -14,99        Netflix
03/01/2017      -20,00        Refeição
04/01/2017      -30,00        Táxi
01/02/2017        5,01        Saldo Atual
02/02/2017      +14,99        Extorno - Netflix

If you want to know the balance of this account on 06/01/2007 you can not just observe the last processed value because it is a modifier (-30 reais on day 4/01). You need to take into account all modifications until you find the first value defined by a idempotent operation - in this case, the opening of the account with the value of 100 reais.

The message on 01/02 can also be considered an idempotent operation: Many systems use this mechanism to facilitate status assessments. In the above example you don’t need to recalculate all values until the first operation to determine the current balance.

  • 1

    Should such an operation necessarily be an initial value? I mean, if in your example we add a release called saldo atualizado, with the value of the modifications, would also be a idempotente? Example: 04/01/2017 35,00 Saldo atual

  • 2

    @Marcelodeandrade yes, if your operation establishes a state (instead of modifying it) it can be considered as idempotent as well.

6

- What is Idempotency Messages?

First it is necessary to understand the concept of idempotency: an operation or message that can be applied several times without changing the final result.

- How and where it should be used?

It can be applied in asynchronous communications and/or by messages, for example. A message receives an identifier. The recipient should understand that a second message with the same identifier should not change the final status

- Simple example of use?

Imagine a system that gets the message:

{
"Identificador" : "789456",
"Comando" : "CancelarPedido",
"CodigoPedido" : "A258963"
}

Suppose the system receives this message for the first time, it cancels the order. When this system receives a second message as the same Identificador, Comando and CodigoPedido must be accepted as valid and return the same result as the first occurrence of it. The final result of the message, the cancellation of the order, will not be changed by subsequent messages.

There’s a Lambda3 Podcast episode on Queues and Messages discussing scenarios on the subject and how this concept should be applied.

5

Let’s get into the theory about idempotency before applying it in a practical example, even because after understanding what idempotency is, you may OR may not apply it depending on your situation.

What is idempotency?

Idempotency is the property that allows an operation to be applied more than once without changing the result.

Where to apply idempotency becomes interesting?

When there is the possibility of duplicate processing of information, where will cause problems of inconsistency (mainly in business).

In the case of asynchronous communications, the term Idempotency Messages, where together with the message a property is sent (an ID or a token), which will only identify the message.

You can check through this property if the message has already been processed (or not checked), and handle accordingly (just ignore the message or reprocess)

Another example would be when there was a request failure, and you are not sure if your request was actually met by the server, imagine the following scenario:

1) (Saldo total) R$1000,00
2) Transferência de R$100,00 para minha conta (POST /transfers?amount=100)
3) 200 OK | (Saldo total) R$900,00
4) Transferência de R$900,00 para minha conta (POST /transfers?amount=900)
5) CONNECTION TIMEOUT | (Saldo total) ? 
6) Transferência de R$900,00 para minha conta (POST /transfers?amount=900)
7) 200 OK | (Saldo total) -R$900,00

When there was a timeout in the connection, we did not have the answer if the operation was performed or not, in our case the server had already processed and discounted R $ 900,00, leaving our account zero. A new request made another transfer of R $ 900,00, leaving our account negative.

For this example, if there was an idempotency token implemented, the flow would be this way:

Antes de realizar a operação, meu token de idempotência é gerado com o valor 98s0a9F

1) (Saldo total) R$1000,00
2) Transferência de R$100,00 para minha conta (POST /transfers?amount=100&tk=98s0a9F)
3) 200 OK | (Saldo total) R$900,00

Nova operação, meu token de idempotência é gerado com o valor f019a8f

4) Transferência de R$900,00 para minha conta (POST /transfers?amount=900&tk=f019a8f)
5) CONNECTION TIMEOUT | (Saldo total) ? 
6) Transferência de R$900,00 para minha conta (POST /transfers?amount=900&tk=f019a8f)
7) 200 OK | (Saldo total) R$0,00

For this second example, the server will identify that for the token f019a8f an operation has already been carried out, and will only send the result of the operation that occurred successfully, without carrying out a new transfer.

Right and where I can see this idempotency working?

To Stripe has a very interesting mechanism of idempotency to ensure that no operation is performed without actually having the intention to perform it.

It is also important to note that in the case of HTTP, there are methods that are idempotentes by default and do not require this treatment.

  • 1

    I missed the deadline to apply the reward and the system automatically gave half to another new most voted, but I think this one got better elaborated. The examples and description are good. I just wanted to mention so that no one would think that the 25 went in the other by a voluntary choice. However, at least I leave my +1 here

  • 1

    Don’t worry @Bacco :) next time

Browser other questions tagged

You are not signed in. Login or sign up in order to post.