October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Make Kafka Consumers Idempotent Across Postgres and Redis

A Kafka consumer can write to a destination and crash before committing its offset. Use a stable event identity and a PostgreSQL unique constraint to make that database transaction safe to replay, and treat Redis as a separate system with its own verified failure guarantees.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To make a Kafka consumer idempotent, give each event a stable identity and enforce deduplication in the same durable transaction as its PostgreSQL business effect. Commit the Kafka offset only after that database transaction commits. If the consumer crashes between those commits, Kafka can redeliver the record, but PostgreSQL’s unique key can prevent the transaction’s effect from being applied again. A separate Redis marker does not make Redis and PostgreSQL one atomic system.

Why at-least-once processing can apply an effect twice

A consumer commonly processes a record, writes its result, and then saves its Kafka offset. If it crashes after the destination write but before the offset is saved, Kafka can deliver the record again. The first effect may already be committed even though Kafka still considers the record eligible for processing.

As an Amazon Associate I earn from qualifying purchases.

Apache Kafka’s design documentation describes this process-then-save ordering as at-least-once delivery: records are not intentionally skipped by the ordering, but a record may be processed again after a failure. Kafka also notes that when a message has a primary key, applying the same update can be idempotent: “receiving the same message twice just overwrites a record with another copy of itself.” That works only when the operation really has those overwrite semantics; a charge, increment, or email send is not automatically safe to repeat. Apache Kafka: Message Delivery Semantics

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the correctness boundary for each effect

PostgreSQL effect: use a durable event identity

For a business mutation in PostgreSQL, put a stable event identity under a UNIQUE constraint and record it in the same transaction as the mutation. PostgreSQL enforces unique constraints at the database boundary, while INSERT ... ON CONFLICT provides a way to handle a duplicate key conflict. PostgreSQL: Constraints PostgreSQL 18: INSERT

Kafka output: use Kafka’s transaction scope where applicable

Kafka producer idempotence addresses certain duplicate writes caused by producer retries. It does not make an arbitrary external consumer side effect safe to repeat. Kafka documents producer idempotence and transactional producer configuration separately from the consumer’s offset and output coordination. Apache Kafka 3.9: Producer Configs

Kafka Streams can atomically coordinate input offsets, state changes, and output records written to Kafka topics. That guarantee is scoped to those Kafka-managed operations; it should not be read as a universal exactly-once guarantee for writes to PostgreSQL or Redis. Apache Kafka 4.1: Processing Guarantees

Implement replay protection in one PostgreSQL transaction

The following is a recommended pattern, not tested code. Adapt the schema and mutation to the application. The essential rule is that the deduplication row and the business change either both commit or neither does.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Define the event identity. Use an identifier that remains the same across retries and is unique in the scope where duplicates must be detected. A producer-generated event_id is one option. If the business identity is a Kafka record rather than a logical event, a combination such as source, topic, partition, and offset may be appropriate. Choose deliberately; these identities represent different notions of “the same event.”
  2. Enforce uniqueness in PostgreSQL. Create a processed-event table with a unique key on the chosen identity. Retain that key for at least as long as a replay must be harmless; deleting it sooner can allow an old event to apply again.
  3. Insert the identity and apply the mutation only for a new row. Run both operations in the same database transaction. If the insert returns no row because the identity already exists, do not apply the business mutation.
  4. Commit PostgreSQL before saving the Kafka offset. If offset saving fails after the database commit, redelivery encounters the existing identity and skips the mutation. If the database transaction fails, do not commit the offset; the record can be retried.
BEGIN;

INSERT INTO processed_events (event_id)
VALUES (:event_id)
ON CONFLICT (event_id) DO NOTHING
RETURNING event_id;

-- Only if the INSERT returned a row:
UPDATE accounts
SET balance = balance + :delta
WHERE account_id = :account_id;

COMMIT;

-- After the database commit succeeds, commit the Kafka offset.

The conditional is part of the design: the business mutation must run only when this transaction inserted the event identity. A database client can inspect whether RETURNING produced a row and execute the mutation conditionally, while keeping both statements in the same transaction. On duplicate delivery, the unique key arbitrates which attempt records the identity; only the successful new insertion should proceed to the effect.

This protects effects inside that PostgreSQL transaction for the selected event identity. It does not make an external call, a Redis write, or another database’s write part of the same atomic commit. If a transaction fails to commit, the consumer must treat the outcome according to the database client’s error handling and retry safely; the unique identity is what makes a later replay safe once a commit has succeeded.

Where Redis fits—and what must be verified

A Redis marker and a PostgreSQL transaction are separate writes. If PostgreSQL commits and the consumer fails before writing the Redis marker, the two stores disagree. If Redis records the marker first and the consumer fails before PostgreSQL commits, a retry could encounter the marker even though the business effect never committed. Neither ordering creates a shared atomic transaction.

For a PostgreSQL business effect, use PostgreSQL’s unique key as the durable correctness boundary. Redis may be useful as an optimization only if its role cannot cause a legitimate event to be skipped and the application still has a correct authoritative path. Do not let a Redis “seen” result alone decide that the PostgreSQL effect is complete unless the deployment’s guarantees and failure model have been explicitly established.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Before relying on Redis as an authority for deduplication, verify the behavior relevant to your specific deployment: persistence across restart, eviction policy, replication and failover, key retention, and the atomicity of the commands used. Those properties can depend on configuration and version; no universal Redis guarantee follows from using a marker. This article therefore does not prescribe a Redis command, TTL, or persistence setting.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Failure cases to design for

  • Crash before the PostgreSQL commit: the transaction’s identity and mutation should both roll back. Do not commit the Kafka offset; retry the record.
  • Crash after PostgreSQL commit but before offset commit: Kafka may redeliver. The unique identity already exists, so the repeat transaction must not reapply the business mutation.
  • Concurrent delivery of the same event: the unique constraint is the database arbiter. Ensure the application applies the mutation only for the transaction that inserted the new identity.
  • Identity removed before the replay window ends: a later replay can be treated as new. Set retention based on how long old events can realistically be replayed, including operational recovery scenarios.
  • Several external effects for one event: a PostgreSQL transaction cannot atomically commit unrelated Redis writes or remote service calls. Give each effect an appropriate correctness strategy instead of assuming one deduplication marker coordinates all destinations.

Keep the guarantees precise

Say “the PostgreSQL effect is protected against replay for this event identity,” not “the message is processed exactly once” without defining the scope. Kafka-to-Kafka transactions, a PostgreSQL transaction, and a Redis operation have different commit boundaries. Idempotency is achieved by aligning the deduplication identity with the effect and enforcing it where that effect is committed; offsets determine whether Kafka may send the record again.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.