A Kafka share group runs 40 thumbnail workers on a 6-partition topic, and one corrupt image keeps crashing workers; how do delivery counting and locks contain it, and what would you change?
answer
- every acquisition is an attempt
- lock expiry brings it back
- archived at the limit, not copied
- batch-mates pay too
- REJECT on known permanent errors
basics
~20 sEach crash leaves the image locked until the lock expires or the broker frees it, then it is redelivered; at the delivery count limit (5 by default) it is archived. Catching the error and acknowledging REJECT archives it at once.
solid answer
~40 sIn a share group every acquisition counts as a delivery attempt. The crashing worker never acknowledges, so once the acquisition lock (30 s by default) expires the image becomes available, another worker takes it and crashes, and so on until the record reaches `group.share.delivery.count.limit` (default 5; per group `share.delivery.count.limit` from 4.3) and is archived — not copied to any dead-letter topic. Healthy records in the same batches lose attempts too. I would switch to explicit acknowledgement and `REJECT` known permanent decode errors, isolate the decoder so it cannot kill the worker, fetch fewer records per poll with `share.acquire.mode=record_limit`, log or copy the payload before rejecting, and make the thumbnail write idempotent for redeliveries.
go deeper
Remember that a share group counts delivery attempts and stops delivering a record after the limit, five by default.
Explain the loop: crash, lock expires, redelivery, attempt counted, archive at the limit, and why REJECT short-circuits it.
Show the batch-mate collateral, the lock-expiry duplicate, and a concrete fix list: explicit acks, decoder isolation, record_limit, idempotent writes.
Set the policy for unprocessable records across teams: what gets rejected, where its payload is kept, and who reviews it, since Kafka keeps no trace.
## The setup An image-thumbnail service reads jobs from a Kafka topic with 6 partitions. A consumer group could keep only 6 workers busy, so the team runs 40 `KafkaShareConsumer` workers in one **share group**, where members share partitions and settle each record individually. One job points at a corrupt image, and the decoder crashes the worker process that takes it. ## What the broker does on its own Nothing in a share group needs the application to cooperate for the poison record to be contained eventually: 1. A worker's `poll()` acquires a batch that includes the corrupt image. Acquisition counts as **delivery attempt 1** for every record in the batch. 2. The worker crashes before acknowledging. Its records stay acquired until the **acquisition lock** expires (30 s by default) or the broker releases a departed member's records; then they become available again. 3. Another worker acquires the image (attempt 2) and crashes in turn. The cycle repeats. 4. When a record that has reached the **delivery count limit** would become available again, the broker archives it instead. The broker default is `group.share.delivery.count.limit=5`; from Kafka 4.3 a share group can override it with the group config `share.delivery.count.limit`, which the broker bounds between `group.share.min.delivery.count.limit` (2) and `group.share.max.delivery.count.limit` (10). 5. An archived record is never delivered to that share group again. It is not copied anywhere: it stays in the topic's log until retention removes it, and the share group simply moves past it. So, unattended, the image costs five crashes and up to five lock periods before it stops — and five workers restarting in a 40-worker fleet is a visible outage of capacity. ## The collateral damage: batch-mates Delivery counts rise per record, and a crash ends the attempt for **every** record the worker held. Healthy thumbnails fetched in the same batch as the corrupt one also burn attempts each time and can be archived alongside it — silent loss of good work. The 4.3 broker source mitigates this: as a record's delivery count climbs past a threshold it shrinks the batches it hands out, and it delivers a record on its final attempt alone so that one bad record cannot spend its batch-mates' last attempts. The application should still not depend on that alone. ## What to change - **Catch the failure and say so.** Switch to `share.acknowledgement.mode=explicit` and wrap decoding: on a known permanent error acknowledge with `AcknowledgeType.REJECT`, which archives the record on attempt 1 with no crash and no lock wait. Keep `RELEASE` for transient errors such as a storage timeout. - **Isolate the decoder.** If a native library can kill the process, run it in a child process or sandbox so the worker survives and can still acknowledge. - **Shrink what a crash takes down.** For expensive per-record work use `share.acquire.mode=record_limit` with a small `max.poll.records`, so a worker holds a handful of records rather than a whole stored batch. - **Tune the limit to the failure mode.** Lowering `share.delivery.count.limit` for this group archives poison records sooner, at the price of fewer retries for genuinely transient errors. - **Keep what you reject.** Since Kafka keeps no record of why a record was archived, log the key and offset, or produce the payload to a topic of your own, before calling `REJECT` if someone must inspect it later. ## The other duplicate path: a lock that expires mid-processing The same mechanism causes duplicates with no poison involved. If thumbnailing a huge image takes longer than the lock, the record is delivered to a second worker while the first is still busy, the first worker's late acknowledgement is refused (`InvalidRecordStateException`), and the work is done twice. The fixes are a lock duration that fits normal processing (group config `share.record.lock.duration.ms`, bounded by 15 s and 60 s by default), acknowledging with `AcknowledgeType.RENEW` while long work continues, and idempotent output — writing the thumbnail to a deterministic path makes a second run harmless. ## How to see it - `kafka-share-groups.sh --describe --group thumbnails` prints each partition's `START-OFFSET` and `LAG`, and `--members` lists the members and their assignments; a start offset that stops moving while lag grows points at stuck processing. - Worker restarts that coincide with one key or offset are the poison signature; log the record's offset and delivery attempt on every failure so the pattern is visible before the limit hides it.
- Why might healthy thumbnails disappear from a Kafka share group along with the corrupt one?A crash ends the delivery attempt for every record the worker had acquired, so batch-mates of the poison record lose an attempt each time and can reach the delivery limit and be archived. In 4.3 the broker shrinks batches for records with high delivery counts and delivers a last-attempt record alone, and `share.acquire.mode=record_limit` with a small `max.poll.records` narrows the blast radius further.
- After a corrupt record is archived by a Kafka share group, how do you get it back for inspection?The share group will not deliver it again and Kafka does not copy it anywhere, but the record is still in the topic's log until retention deletes it. Read it by partition and offset with an ordinary consumer, which is why the worker should log the partition and offset before rejecting. Better still, produce the payload to a topic of your own before acknowledging `REJECT`.
- Would raising the delivery count limit to its maximum make the thumbnail service more reliable?Only for transient failures. A higher limit (bounded by `group.share.max.delivery.count.limit`, 10 by default) gives a flaky storage call more retries, but a deterministic poison record then causes up to ten worker crashes, each attempt also costing a lock period. Classify errors instead: `RELEASE` transient ones, `REJECT` permanent ones, and keep the limit modest.
- A share group redelivers a failing record forever until someone fixes it.
- Kafka moves archived share-group records to a dead-letter topic automatically.
- An archived record is deleted from the topic straight away.
- Only the poison record's delivery count rises when a worker crashes.
- A crash counts nothing against the record because no acknowledgement was sent.
- Raising the lock duration is the fix for a record that always fails.
Checked against Apache Kafka 4.3.1; the per-group share.delivery.count.limit config was added in 4.3, and earlier releases set the limit only broker-wide.