Monedula

Scenario 12.1.2 · Nisshi on PostgreSQL: one transaction per write

Nisshi diskless Kafka on PostgreSQL: a row lock per write

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

Nisshi 0.8.2 on PostgreSQL · each produce request is one transaction that locks the partition's watermark row; a second writer of the partition waits for the lock, then commits right after.

Run this scenario in the playground

broker-1's transaction holds orders-0's row lock; broker-2's waits. t+0.150s

p1 (on broker-1) and p2 (on broker-2) each send a record for orders-0 at 100, and both batches flush at 150. broker-1's transaction locks orders-0's watermark row; broker-2's transaction waits for that lock. Both producers are idempotent, acks=all.

Open at this step: step 1, broker-1's transaction holds orders-0's row lock; broker-2's waits

What you start with

PostgreSQL is Nisshi's production engine and its Docker Compose default. On it a produce request is one transaction per partition: it locks the partition's watermark row (select ... for no key update), inserts the records, moves the watermark and commits. Nothing goes to object storage. p1 on broker-1 and p2 on broker-2 write orders-0 in the same millisecond; consumer group g1 reads through broker-3. The 15 ms transaction is Inkless's PostgreSQL figure, borrowed (Nisshi's sources give none).

Step by step

  1. broker-1's transaction holds orders-0's row lock; broker-2's waits t+0.150s p1 (on broker-1) and p2 (on broker-2) each send a record for orders-0 at 100, and both batches flush at 150. broker-1's transaction locks orders-0's watermark row; broker-2's transaction waits for that lock. Both producers are idempotent, acks=all. Open at this step: step 1, broker-1's transaction holds orders-0's row lock; broker-2's waits
  2. broker-1 commits row 0 and the watermark; broker-2 takes the lock t+0.165s After 15 ms broker-1's transaction has inserted the row at offset 0, moved the watermark to 1 and committed, which releases the lock. broker-2's transaction takes it at once, with no retry. Open at this step: step 2, broker-1 commits row 0 and the watermark; broker-2 takes the lock
  3. broker-2 commits row 1; both producers are acked t+0.181s broker-2 commits offset 1 at 180. Each ack leaves 1 ms after its commit (a simulator choice): p1's at 166, p2's at 181, 15 ms later for the wait. The idempotent sequence check runs inside each transaction, under the lock. Open at this step: step 3, broker-2 commits row 1; both producers are acked
  4. c1 on broker-3 reads both rows at its first poll t+0.600s c1 talks to broker-3, which wrote nothing. On PostgreSQL a fetch reads the watermark row and the rows together, so every broker sees a commit at once. Open at this step: step 4, c1 on broker-3 reads both rows at its first poll
  5. orders-1 has its own row: no wait t+1.200s p1's next record goes to orders-1 at 700: its transaction locks orders-1's row, commits at 765 with nobody waiting and is acked at 766. c1 reads it at 1200. Open at this step: step 5, orders-1 has its own row: no wait

The takeaway

On PostgreSQL the watermark row lock orders the writers of a partition: the second transaction waited and committed right after the first, so the offsets follow commit order and no write was retried. Records, watermarks, group state and committed offsets all live in PostgreSQL, so any broker reads a commit at once, with no cache to wait for. Writers of different partitions never wait for each other. Retention and compaction run in a maintenance sweep, every 10 minutes by default (maintenance_interval). Simulator choices: the 15 ms transaction borrowed from Inkless's figure, the pinned brokers, and the response 1 ms after the commit.

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
  • maintenance_interval A setting of the Nisshi PostgreSQL engine, 10 minutes by default, for how often its maintenance applies retention and compaction to the stored records.

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.2" title="Kafka Simulator: Nisshi on PostgreSQL: one transaction per write" 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-postgresql-row-lock/">Nisshi diskless Kafka on PostgreSQL: a row lock per write</a></p>

Further reading

Nisshi v0.8.2 source on GitHub

More in Diskless

All scenarios