Video in progress

What is idempotency, and why does it matter especially in microservices?

An idempotent operation gives the same result whether it runs once or five times. In microservices it's normal for one request to arrive several times because of retries, network failures and broker redelivery, so you have to be ready for it.

📖 Answer

By HTTP method:

Method Idempotent? Note
GET yes changes nothing
PUT yes "make the state like this": the same even if repeated
DELETE yes after the second delete the resource is still gone
POST no each call may create something new

But that's only the contract; your code can break it. For example, if inside PUT /balance you do balance = balance + 100, it's no longer idempotent.

Why it matters: a client sent POST /orders, the server created the order, but the response got lost on the way. The client sees a timeout and sends it again. Without idempotency, you get two orders and the money charged twice.

It's the same in a Kafka consumer: the message was processed, but the service restarted before the offset was committed, so the message comes again. The consumer has to notice and not do it a second time.