Back to all questions

What Is an Idempotency Key?

Devs Hive

Devs Hive

Seen on interview: 4 users

Quite often in technical interviews, when the conversation turns to distributed systems, you will hear a question about a pattern called Idempotency. In short, an Idempotency Key is a unique identifier of an operation that lets a system recognize that it has already seen this operation and not execute it again.

I have already covered idempotency in the context of HTTP requests, so I recommend reading about it as well. But in an interview, a definition alone is usually not enough. It is much better to show what real problem this pattern solves.

Let's walk through a simple payment example.

A system without Idempotency

Imagine a user buying a product on a website. The browser sends a request to our microservice. The microservice accepts it and publishes a message to a broker, for example RabbitMQ.

Suppose the message delivery model is at-least-once delivery. This means the broker guarantees that a message is delivered at least once, but under certain circumstances the same message may be delivered again.

What Is an Idempotency Key?

In the diagram above, RabbitMQ passes the message to one of our workers. The worker receives it and performs the business operation, which in our case is charging the user's card.

After successful processing, the worker must send the broker an ACK, a confirmation that the message was processed successfully.

Now imagine the following situation:

RabbitMQ → Worker

Worker:
1. отримав повідомлення
2. списав 100$
3. ❌ не встиг відправити ACK

The cause can be anything: the worker crashed, a network timeout occurred, the process restarted, and so on. And here is the important point.

RabbitMQ does not know that the money has already been charged. All it knows is that the message was sent but no ACK was received. Since at-least-once delivery is used, the broker will redeliver this message after some time, possibly even to a different worker.

RabbitMQ → Worker 1 → списав 100$ → ACK загубився

RabbitMQ → Worker 2 → списав ще 100$ 💥

As a result, a single logical operation may be executed multiple times. For payments, this is obviously unacceptable.

This is where the Idempotency Key comes in

Now let's look at the same system, but this time using an Idempotency Key.

What Is an Idempotency Key?

The architecture barely changes. The main difference is that every logical operation now has a unique idempotencyKey.

For example: payment:order-123

Before executing the business logic, the worker tries to register this key in a fast store, for example Redis.

The logic goes roughly like this:

Worker отримав message
↓
перевіряє idempotencyKey
↓
чи існує payment:order-123?
↓
/ \
НІ ТАК
↓ ↓
обробити duplicate
message ↓
↓ ignore
зберегти
key

What Idempotency gives you in the end

It is important to understand that Idempotency does not guarantee that RabbitMQ will not deliver a message twice.

The goal of Idempotency is to ensure that redelivery of the same message does not result in the business operation being executed again.

In our example, RabbitMQ may deliver the payment message several times, but thanks to the Idempotency Key, the system recognizes the duplicates and does not charge the money again.

Seen on interview?

Related Questions

Comments (1)

Sign in to leave a comment

7P
Давид
November 2, 2026 at 05:06 PM

Дякую автору за детальне пояснення!