Skip to main content

Question 41 of 50

What is Idempotency?

Mid-levelSenior

Question

"What is Idempotency, in the context of a Kafka consumer?"

What the interviewer wants to assess

Whether you understand idempotency as a structural requirement (not a "quality extra") whenever the delivery guarantee is At Least Once — the one most used in practice.

Resposta rápida

Idempotency is the ability to process the same event multiple times without producing duplicated side effects — the balance is credited once, the email is sent once, even if the message is delivered two or more times.

Resposta nível Sênior

At Least Once (Chapter 10) guarantees no message is lost, accepting as the price that it may be delivered more than once — after a rebalance, a commit retry, or a failure between the end of processing and the offset commit. A consumer that doesn't handle this duplicates effects in production. The most robust implementation uses a unique event identifier (eventId) and a database uniqueness constraint, checked and applied in the same transaction that executes the business effect — not in a separate upfront check, which would introduce a race condition under concurrency.

In-depth explanation

See the full chapter — Idempotency.

Exemplo financeiro

The saldo-service keeps a processed_events table with a uniqueness constraint on eventId. Before crediting a PIX, it tries to insert the eventId; if it fails due to a uniqueness violation, the event has already been processed and the operation is safely skipped — all within the same database transaction.

"I check whether the eventId already exists before processing"

Checking and processing as separate steps introduces a race condition under concurrency. The check and the effect need to be in the same transaction, with the uniqueness constraint as the real guarantee.

Pode vir a seguir

Likely follow-ups: "how would you avoid duplicate processing?" and "what is Replication Factor?".

Related chapters