Monedula

Scenario 12.1.3 · Nisshi: two brokers, one partition

Nisshi diskless Kafka: two brokers write one partition

Diskless · Advanced · about 3 min · Apache Kafka 4.4 · Nisshi 0.8.2

Nisshi 0.8.2 on S3 · two brokers write one partition at once: the conditional write of watermark.json picks the order, the loser gets 412, reads the document and writes again.

Run this scenario in the playground

broker-1 creates watermark.json; broker-2's create fails with 412. t+0.200s

Both brokers try to create the document. broker-1's lands first: version 1, offset 0 is p1's, and its PUT starts. broker-2's create fails because the document now exists (PutMode::Create in the code), so broker-2 has taken nothing yet.

Open at this step: step 2, broker-1 creates watermark.json; broker-2's create fails with 412

What you start with

On Nisshi's S3 engine no broker leads a partition: any broker can write any partition. What orders two writers is the partition's watermark.json, written with a version check. The broker that loses the race gets 412 (precondition failed), reads the document again and writes again at once. p1 talks to broker-1 and p2 to broker-2; both are plain producers (acks=all) writing orders-0, and consumer group g1 reads through broker-1.

Step by step

  1. Two brokers send a request for orders-0 in the same millisecond t+0.150s p1 on broker-1 and p2 on broker-2 send a record for orders-0 at 100, and both requests leave at 150. Neither broker holds a copy of orders-0's watermark.json yet. Open at this step: step 1, Two brokers send a request for orders-0 in the same millisecond
  2. broker-1 creates watermark.json; broker-2's create fails with 412 t+0.200s Both brokers try to create the document. broker-1's lands first: version 1, offset 0 is p1's, and its PUT starts. broker-2's create fails because the document now exists (PutMode::Create in the code), so broker-2 has taken nothing yet. Open at this step: step 2, broker-1 creates watermark.json; broker-2's create fails with 412
  3. broker-2 reads version 1 and takes offset 1 t+0.300s broker-2 reads the document (version 1, high watermark 1) at 250 and writes again against version 1 at 300: version 2, offset 1 is p2's. Open at this step: step 3, broker-2 reads version 1 and takes offset 1
  4. Both batches are written; p2 is acked one round later t+0.601s broker-1's PUT ends at 500 and p1 is acked at 501; broker-2's ends at 600 and p2 is acked at 601. The lost race cost p2 one read and one write, 100 ms. Offsets 0 and 1 follow the order of the watermark writes. Open at this step: step 4, Both batches are written; p2 is acked one round later
  5. broker-1's copy is stale: its write gets 412, then offset 3 t+1.100s Both send again at 900. broker-2 holds version 2, the current one, and writes version 3 (offset 2) at 1000. broker-1 still holds version 1, so its write fails with 412; it reads version 3 at 1050 and writes version 4 at 1100: offset 3. Open at this step: step 5, broker-1's copy is stale: its write gets 412, then offset 3
  6. Offsets 0 to 3, no gap, read in order t+1.800s Both batches are written (1300, 1400) and acked (1301, 1401). c1 reads through broker-1: offset 0 at 600, offset 1 at 1200, and offsets 2 and 3 at 1800. Open at this step: step 6, Offsets 0 to 3, no gap, read in order

The takeaway

Every write to orders-0 went through one document, watermark.json, written with a version check. When two brokers raced, one won, and the other read the new version and wrote again, so the offsets stayed contiguous (0 to 3) in the order of the watermark writes and the loser was acked one round, 100 ms here, later. A broker writing after another broker's write always pays that round, since its copy is stale; the code retries with no cap and no backoff. Simulator choices: the pinned brokers, the 50 ms conditional write and read, the 300 ms PUT, and the response 1 ms after the PUT.

Configs in this scenario

  • acks How many replicas must have a write before the producer counts it as sent: 0 (none), 1 (the leader only) or all (every in-sync replica, the default, and required for idempotence). How acks works

Embed this scenario

Paste this into a blog post, a course page or your team's docs. The scenario plays inside your page, and the link under it points readers to this walkthrough.

<iframe src="https://monedula.dev/kafka-simulator/embed/?scenario=12.1.3" title="Kafka Simulator: Nisshi: two brokers, one partition" width="100%" height="520" style="border:1px solid #d0d0d0;border-radius:8px" loading="lazy" referrerpolicy="no-referrer-when-downgrade"></iframe>
<p><a href="https://monedula.dev/kafka-simulator/scenarios/nisshi-two-brokers-one-partition/">Nisshi diskless Kafka: two brokers write one partition</a></p>

Further reading

Nisshi v0.8.2 source on GitHub

More in Diskless

All scenarios