In an earlier experiment, the clearest result wasn't about the model at all. When the same support ticket was delivered twice, the agent made two audited follow-up calls, unless the application deduplicated them.

The deduplication in my demo was a Set in memory. That's fine for a demo. In production it has two problems:

  • It forgets on restart. If the consumer creates the follow-up and dies before acknowledging the message, Kafka delivers it again to a fresh process with an empty set. The follow-up call happens again.
  • It drops real work. It was keyed on the ticket, so a customer's second, genuine callback request on the same ticket was silently ignored.

So I added an opt-in mode that stores the follow-up as a database row, and then tried to break it by killing the consumer at the worst possible moment. This post is about that second part: how to kill a consumer at an exact point, and how to make sure the test isn't lying to you.

The design, in one paragraph

Each customer message gets an id from the ticket system, reused on any resend. The follow-up is one PostgreSQL row with the primary key (ticket_id, message_id), created with INSERT … ON CONFLICT DO NOTHING. If the row already exists with the same data, no second row is created. If it exists with different data, that's an identity conflict, and it goes to (simulated) human review instead of being merged. The row is committed before the Kafka record is acknowledged. If the database stays unreachable after a few retries, the consumer stops instead of acknowledging work it couldn't record.

The dangerous window is between the commit and the acknowledgement:

consumer receives offset 0 │ ├─ INSERT row ........................ (crash here: no row, replay creates it) ├─ COMMIT ........................... row is durable │ ◀── kill the JVM here ──▶ the interesting case ├─ triage, completion ├─ ack() ............................. acknowledged inside the app └─ periodic offset commit ............ the consumer group's offset moves 

If the process dies between COMMIT and the ack, Kafka has every right to deliver offset 0 again. That's correct Kafka behavior, not a bug. The question is what my code does with it.

Killing a JVM at an exact line

"Kill it at some point" is easy. Kill it exactly after the commit and before the ack is harder, because a few milliseconds later the result means something else.

So the test builds a separate, test-only version of the agent with a checkpoint hook. In production, the hook does nothing. In the test build, every checkpoint reports over a loopback socket to the test, and at the chosen point the handler stops and waits to be killed. It never resumes. If it loses the connection to the test, it halts the JVM, and the test counts that as an invalid run, not as a crash.

The test (the parent) then:

  1. sends one ticket to a fresh topic, and starts agent JVM #1 in a fresh consumer group
  2. waits for that process to report PAUSED AFTER_CREATED for exactly that topic, partition and offset
  3. checks the database on its own connection: is the row there?
  4. checks Kafka: what offset has the consumer group committed?
  5. kills JVM #1, and checks both again before starting anything else
  6. starts JVM #2 in the same consumer group
  7. requires JVM #2 to receive the same topic, partition and offset
  8. waits for the group to commit past it, and counts rows and outputs

Everything runs against real Kafka (apache/kafka:4.2.1) and PostgreSQL (postgres:17.11-alpine) in Testcontainers, with a scripted model, so no API calls.

The result

This is the actual trace from the published run:

Point Rows for the ticket Committed next offset
JVM #1 paused after COMMIT msg-1 none
After the kill, before restart msg-1 none
JVM #2 received offset 0 again, finished msg-1 1

Kafka delivered offset 0 to a second process (pid 18556 first, then 10780). The group had no committed offset and starts from the earliest record, so the replacement began at offset 0. It attempted the follow-up, found the row, and added none. Two attempts, one row.

How this test could have lied to me

This is the part I'd want someone to check if they reviewed my test. Several versions of it would pass for the wrong reason:

  • "The committed offset was empty, so the record wasn't acknowledged." Not enough. With SmallRye's throttled strategy, acknowledgements are tracked locally and the next offset is committed periodically. An empty committed offset doesn't prove the handler never called ack(). What proves it is the barrier: the pause is reported from a line that runs before the ack, by that process id, and that handler never continues.
  • "There's one row at the end." Also not enough on its own. The row could have been created by the replacement. That's why the test reads the database at the pause and after the kill, before any restart. The row exists before JVM #2 ever starts.
  • "Something got replayed." It has to be the same topic, partition and offset, delivered to the replacement's process id, not a different record that happens to look similar.
  • "The JVM died at the right moment." A process that halted itself (lost connection, timeout) is a failed run, not an injected crash. The test requires the process to be alive before the kill and rejects the fixture's own halt codes afterwards.

The other cases

The same harness covers the windows around it:

Case What I did Result
Crash inside the transaction killed after INSERT, before COMMIT 0 rows after the kill; the replay created 1
Database unreachable cut the agent's DB access with a proxy after bounded retries the tickets channel stopped; the JVM stayed up and the committed offset stayed at 1. After access was restored, a replacement processed offset 1 and committed 2, with 1 row
Old in-memory version killed after the first audited call was visible, before the ack two audited calls in total after the replay

The case that keeps the fix honest

It's easy to "fix" duplicates by throwing away anything that looks similar. So the last test sends two different messages with identical text on the same ticket, and kills the consumer after the first one is committed.

After recovery: two rows, msg-1 and msg-2. The crashed message was attempted twice and recorded once; the second was recorded once. A fix keyed on the ticket, or on a hash of the text, would have quietly dropped a real customer request.

What this doesn't prove

  • It's not exactly-once. The audit log shows two createFollowUp attempts; the database has one effect. Other side outputs can repeat on a replay.
  • It covers a database row. If the follow-up called an external API, you'd need idempotency on that side too, or reconciliation.
  • Not tested: rebalances, broker failures, share groups, and a crash after the ack but before the periodic offset commit.
  • A real model may decide differently on the replay than it did the first time. The scripted model decides the same way every time.

Try it

The results list every case against the expectations I committed before writing any code, plus the raw traces. The design decision explains the alternatives I rejected: keying on Kafka coordinates misses a resend at a new offset, and Kafka transactions don't include this PostgreSQL write. With Java 21, Maven and Docker, the crash cases run in a few minutes:

mvn -pl crash-fixture -am verify -Dtest=none -Dsurefire.failIfNoSpecifiedTests=false -Dit.test=CrashRecoveryIT -Dfailsafe.failIfNoSpecifiedTests=false 

If you've tested a consumer like this: what could you observe from outside the process, and where did you need a hook inside it?