---
title: "KafScale 2.1 diskless Kafka: lease handover, no overwrite"
description: "KafScale 2.1: an owner stops mid-write, the new owner writes at the same offset, its S3 PUT fails If-None-Match, and it adopts the winner."
url: https://monedula.dev/kafka-simulator/scenarios/kafscale-2-lease-handover-fencing/
---

# KafScale 2.1 diskless Kafka: lease handover, no overwrite

Scenario 08.2.6 · KafScale: a lease handover without an overwrite. Diskless · Advanced · about 3 min · Apache Kafka 4.4 · KafScale 2.1.

KafScale 2.1 · an owner stopped mid-write releases its lease first; the next owner writes at the same base offset, the old owner's PUT lands first, and the new owner's PUT fails If-None-Match and adopts it: nothing acked is lost.

Run it: https://monedula.dev/kafka-simulator/playground/?scenario=08.2.6

## What you start with

A broker that keeps running after its etcd session expired, for example one paused past the 10 s lease TTL while another broker took the partition, might still write a segment at an offset the new owner has already used and overwrite records that were acked (KafScale issue #324). KafScale guards both writes against that: segment PUTs to S3 carry If-None-Match: *, so S3 never replaces an existing segment, and the etcd end-offset write checks that the writer still holds the lease. A paused broker has its own scenario (08.2.7); here the stale writer comes from a graceful stop, which releases the lease while a write is still in flight: the same fencing, reached another way. broker-2 owns orders, p1's order-2 is uploading there, and broker-2 is stopped gracefully. Then p2 sends order-3. Watch the lease lane, the segment keys in the object storage lane, and both producers' errors.

## Step by step

1. **order-2 is uploading on broker-2** (t+1.100s): orders is a KafScale topic (KafScale 2.1) with one partition, owned by broker-2. order-1 was acked at 790 ms and c1 read it at 1000 ms. p1 sent order-2 at 1000 ms: broker-2 gave it offset 1 and started uploading segment orders/0/segment-00000001 at 1050 ms. [Open at this step](https://monedula.dev/kafka-simulator/playground/?scenario=08.2.6&t=0)
2. **broker-2 stops: its lease goes first** (t+1.250s): broker-2 was stopped gracefully at 1200 ms. In KafScale 2.1 a graceful stop releases every lease at once, then lets requests in flight finish, so orders-0 has no owner now while order-2's upload is still running. [Open at this step](https://monedula.dev/kafka-simulator/playground/?scenario=08.2.6&t=1)
3. **A new owner, the same segment key** (t+1.400s): p2 sent order-3 at 1300 ms. broker-1 took the lease and restored orders-0 at 1350 ms: etcd's end offset is 1 and S3 does not list order-2's segment yet, so order-3 got offset 1, the same base offset and the same key, orders/0/segment-00000001. Segment PUTs carry If-None-Match: *, so only one of the two PUTs can create that key. broker-2's started first, at 1050 ms, so with equal upload times it lands first, at 1650 ms, and broker-1's PUT will fail when it ends. [Open at this step](https://monedula.dev/kafka-simulator/playground/?scenario=08.2.6&t=2)
4. **The first PUT wins, the new owner adopts it** (t+2.050s): broker-2's segment landed at 1650 ms, but its etcd write at 1690 ms failed: broker-2 no longer holds the lease, so p1 got REQUEST_TIMED_OUT (the broker cannot tell whether the records were written). broker-1's PUT ended at 1950 ms and failed its If-None-Match check: p2 got KAFKA_STORAGE_ERROR, which it retries, and broker-1 dropped its partition log (KafScale issue #343, fixed by #353). c1's fetch at 2000 ms made broker-1 restore orders-0: S3 now ends at 2, so broker-1 adopted order-2's segment, wrote end offset 2 to etcd, and c1 read order-2 at offset 1. [Open at this step](https://monedula.dev/kafka-simulator/playground/?scenario=08.2.6&t=3)
5. **Nothing acked is lost, one record twice** (t+5.050s): p1's retry of order-2 reached broker-1 at 2190 ms and became offset 2; p2's retry of order-3 became offset 3. Both were acked, at 2830 and 3090 ms, and c1 read them at 3000 and 4000 ms. S3 never replaced a segment, so nothing acked was overwritten. But order-2 is in the log twice, at offsets 1 and 2: its first write reached S3 and was adopted although p1 got an error, and KafScale has no idempotent producer to drop the retry, so c1 read it twice. [Open at this step](https://monedula.dev/kafka-simulator/playground/?scenario=08.2.6&t=4)

## The takeaway

broker-2 let its lease go before its upload finished. broker-1 took the lease for p2's order-3 and restored the partition, but order-2's segment was not in S3 yet, so order-3 got offset 1, the same base offset and the same segment key. Only one PUT could create that key, and broker-2's, sent first, landed first. Its etcd write then failed, because broker-2 no longer held the lease, so p1 got REQUEST_TIMED_OUT: the broker cannot tell whether the records were written. broker-1's PUT failed its If-None-Match check: p2 got KAFKA_STORAGE_ERROR, and broker-1 dropped its partition log, so its next request restored the partition, adopted order-2's segment at offset 1 and continued after it (KafScale issue #343, fixed by #353). The two retries became offsets 2 and 3 and were acked. Nothing acked was overwritten, but order-2 is in the log twice: its first write reached S3 although p1 got an error, and p1 runs with idempotence off. Since KafScale 2.1.0 an idempotent p1 would get offset 1 back for its retry instead, from the producer state broker-1 rebuilt from S3. The conflict does not count against S3 health (#345), so broker-1 kept serving its other partitions. Simulator choices: the PUT that ends first wins, and with equal upload times that is the one sent first; the losing PUT is not drawn in the bucket while it runs. A broker paused past its lease TTL, the case of issue #324, is shown in scenario 08.2.7.

## Further reading

- [KafScale v2.1.0 source on GitHub](https://github.com/KafScale/platform/blob/v2.1.0/pkg/storage/log.go)

## Troubleshooting

- [TimeoutException in Kafka producers: causes and fixes](https://monedula.dev/kafka/troubleshooting/producer-timeout-exception/)
- [KAFKA_STORAGE_ERROR in Kafka: disk failures and Inkless](https://monedula.dev/kafka/troubleshooting/kafka-storage-exception/)
- [Kafka duplicate messages: causes and fixes](https://monedula.dev/kafka/troubleshooting/duplicate-messages/)
