Skip to content

Writing blocks ​

This page describes the current version

The internals on this page can change between v0.x versions. The batch size, the limits and the thread priorities are current values, not fixed ones.

After the node answers VALID, the block is in memory only. A writer thread writes it to the database in the background. This page describes the writer: the batches, the commit, the publish step, what waits for the writer, and what happens on a reorg or an error.

Why a writer thread ​

In earlier versions, the engine loop wrote each block after the forkchoice response, one block in each write transaction. On the desktop, on the mainnet database, a write took 4.2 to 6.6 seconds on average (10.5 to 13.2 seconds p90) and wrote about 70 MB, for a few hundred KB of state change. Each MDBX commit copies every page that it changes, and the B-tree branches above them, and then syncs. The next engine_newPayload waited behind the write. On heavy stretches the node fell behind the chain and could not catch up.

The writer thread fixes this:

  • The next block waits 4 ms on average (7 ms p90) for the writer (150 blocks, 2026-10-02). In a later window (2026-10-05) it was 5 ms. A block that executes during a write is slower (#32). For the cases that wait for the writer, read What waits for the writer.
  • When blocks wait, the writer writes several of them in one transaction. A batch is not free. On the dedicated server, a write of 3 blocks took 1.8 to 2.5 s against about 0.8 s for one block.

When a block is due ​

A canonical block is due for the database when one of these is true:

  • It is --persist-depth blocks or more below the head. The default depth is 0, so every canonical block is due after the forkchoice update that makes it canonical.
  • It is the finalized block or below it.

The node starts the writer thread in engine mode. Then engine_forkchoiceUpdated only marks the due blocks and answers at once.

Batches ​

Between Engine API requests, the engine loop does these steps:

  1. It publishes a batch that the writer finished, if there is one.
  2. If the writer is idle and blocks are due, it gives the writer all due blocks, oldest first, up to 64 blocks.
  3. It marks the blocks of the batch as pinned. The node does not free a pinned block before it publishes the batch.

The hand-over first publishes a batch that the writer finished. This includes a batch that the writer ended just before the hand-over. Before version 0.9.2, the next batch could take the place of such a batch before the node published it. Then the next payload on a linear chain could get ACCEPTED, and the shutdown could wait forever (issue #142).

Blocks that become due while the writer is busy wait for the next batch. So a batch grows when the node is behind. At the head, with one block every 12 seconds, a batch usually holds one block.

One commit per batch ​

The writer does three steps for each batch:

  1. Undo records. For each block, in order, it computes the reverse diff against the state before the block. That state is the database under the diffs of the earlier blocks of the batch. It also collects the code that each block set.
  2. One root replay. It applies the diffs of all blocks of the batch, in order, over a view of the database. It computes the state root and compares it with the state root of the last block. A difference is a corruption error.
  3. One write transaction. It writes the state and trie changes. Then, for each block in order, it writes the header, the history, the code, the undo record and the step of the undo window. Then it sets the database head to the last block. One durable commit ends the transaction.

The tables after a batch are the same, entry by entry, as after one commit for each block. The test runner enginetest checks this when it runs with the --compare-batch option. It writes each EEST Engine API test both ways and compares all 14 tables.

The writer runs at nice 5, as the prewarm. With --threads 2 or more, it has its own pool of 8 threads at nice 5 for the trie reads and the root of a batch. It has its own memory arena. It never changes the block tree. It only reads the blocks of its batch, which do not change after they become valid.

While the writer works, it holds the busy flag of the chain. The mempool prewarm and the transaction validation thread pause during the write.

Publish ​

The engine thread publishes a finished batch. It does this at the start of every chain call (engine_newPayload, engine_forkchoiceUpdated, block building) and in the engine loop between requests. The publish has these steps:

  1. It applies the cache updates of the batch (see below).
  2. It reads the new database head.
  3. It frees the written blocks from memory. Their children now have the database head as their parent. If the trie nodes of a newer block still build in the background, the written blocks stay in memory until that build ends.
  4. If a forkchoice update moved the head off the batch during the write, it puts the head's branch on reverse layers (see Reorgs during a write).
  5. It drops the branches that no longer connect to the database head.

The engine loop then logs a line with the written range, the number of blocks and the time of the batch. Then it trims the memory: it gives free heap pages back to the kernel only when the heap is larger than 1 GB, and each arena keeps up to 512 MB (the scratch arena of the engine thread) or 64 MB (each other arena). For why, read Memory between blocks.

Cache updates at publish ​

The commit of the writer does not change the account and slot entries of the cross-block cache. It gives the updates back to the engine thread, which applies them at publish, when no block executes.

The reason is a block that executes during the commit. That block reads a pinned snapshot of the old database head. It reads the storage roots of the accounts through the cache. With the cache updated at the commit, it read the new storage root of a written account and opened a storage trie that its snapshot did not have. The trie nodes then did not match their hashes. The EEST tests found this error.

The trie nodes that the commit writes go into the node cache at once. A node is valid only for the hash that its parent expects, so an early node is never wrong. For the cache and its epochs, read State database.

What waits for the writer ​

Some work needs the database head to stay where the chain thinks it is. That work first waits for the writer to finish, then publishes the batch:

  • Back-pressure. engine_newPayload waits for the writer when more than 4 canonical blocks deeper than the persist depth are not written yet. Forks do not count. This limit keeps memory bounded and keeps the cost of the roots over unwritten ancestors small.
  • A fork off the batch. A new block whose parent chain does not pass through the last block of the batch in flight. After the publish, the node handles it as a fork below the database head.
  • A forkchoice head off the batch. The same rule for engine_forkchoiceUpdated, with one exception. A head that is a valid block in memory does not wait (see Reorgs during a write). A pending head, a block of the batch below its last block, and a head on a branch with reverse layers still wait.
  • A pending branch. A forkchoice update to a pending block. The execution over the undo records of the blocks above the fork base needs a database head that does not move.
  • A branch on reverse layers. A forkchoice update to a block that executed over the undo records of the blocks above its fork base. If the publish moved the database head, the branch becomes pending again.
  • A view newer than the chain. The writer committed a batch that the engine thread did not publish yet. The block or the build runs again after the publish.
  • Shutdown. The write of all blocks in memory at shutdown.

A block whose parent is not written yet does not wait. It executes over the diffs of its unwritten ancestors and computes its root from their trie nodes. For this path, read Block pipeline.

The back-pressure limit ​

The project measured the limit while the node caught up over P2P, 10 minutes for each setting. At that time each block over unwritten ancestors replayed their diffs for its root.

LimitBlocks written in 10 minutes
16about 120
4197
114

With a limit of 1, each commit held one or two blocks and took 3 to 14 seconds. With 16, each block replayed up to 16 ancestors. Later the root over the trie nodes of the ancestors replaced the replay. With limit 4 the node then wrote 352 blocks in 10 minutes, and 368 with limit 16. With limit 16 the batches stopped at 8 blocks, the step of the P2P import. The limit stays 4: it needs less memory for the same speed.

Reorgs during a write ​

A valid branch can die while the writer writes the other branch. For example, a forkchoice update switches to a sibling of the block in the batch. The node handles this case in these ways:

  • A forkchoice update to a valid block in memory off the batch does not wait for the writer (#145). The block becomes the head at once. The batch and the branch must have no reverse layers, and the branch must start on the base of the batch. When the node publishes the batch, the branch forks off a canonical block of the database. The publish puts the branch on the undo records of the written blocks above its fork base. The writer then writes the switch in one transaction. A crash leaves the database on the old head, on the batch or on the switch. If the publish cannot read the undo records, the head goes back to the database head. The node logs this and gives the change to the pool and to JSON-RPC.
  • Another forkchoice update to a head off the batch waits for the writer. After the publish, the new head is a fork below the database head.
  • A new block whose parent is not on the chain of the batch waits for the writer too. This is the usual mainnet reorg: the next proposer builds on the parent of a late block.
  • While the writer is on, each executed block keeps its full RLP until the writer writes it. When the branch of such a block dies under a batch, the block becomes pending again. A forkchoice update to it then executes it over the undo records, as for any fork below the database head.

Forks below the database head ​

A branch on a fork base below the database head executes over reverse layers: the undo records of the written blocks above the fork base. A block whose parent is 1 or 2 blocks below the database head (for example a sibling of the head) executes at engine_newPayload and gets VALID or INVALID. A deeper fork stays pending (ACCEPTED) until a forkchoice update selects it. Then it executes over the layers.

When a forkchoice update makes the branch the head, the branch keeps these layers. The forkchoice update does not write. The database head stays on the old block, and new blocks on the branch execute over the layers. JSON-RPC reads the branch from memory, so latest is the branch at once (#144).

The writer then writes the branch in one transaction. The transaction removes the rows of the old blocks above the fork base (headers, undo records, history) and writes the new blocks. The state root is a replay of the undo records and the new blocks' diffs over the old database head. The database goes from the old block to the new branch in one commit. It is never on the fork base, and a crash leaves either the old block or the new branch. cinder --serve, which reads the database only, goes from the old block to the new branch in one step and does not see the fork base.

Before version 0.9.2, a sibling of the head got ACCEPTED and executed at the forkchoice update, which then took 170 to 450 ms on mainnet (issue #70). After the sibling executed at engine_newPayload, the forkchoice update still rolled the database back to the fork base, one commit per block, and the writer wrote the new branch later. Most of the time of that forkchoice update was the commit of the rollback. Now a forkchoice update to a branch that the node executed before does not write, and takes much less time. The Changelog gives the measured times. Before version 0.9.5, a forkchoice update that came right after the engine_newPayload of the new block took 7.8 to 8.5 ms: it waited for the trie-node build of that block. Now it does not wait for that build (#233).

This fixed a real case on mainnet: a 1-block reorg during a slow write. Before the fix, the node stayed on the dropped block and answered SYNCING to every later payload.

Errors ​

The writer keeps the error code of a failed batch. From then on, every chain call returns that error. The engine loop logs writing blocks to the database failed and the node stops.

The commit of a batch is one MDBX transaction. A failed batch leaves the database at the last good commit.

If the node stops without the shutdown step (a crash or SIGKILL), the blocks that are not written are lost. After the restart, the database head is the last good commit, and the node must get the missing blocks again. After a restart, the node can answer SYNCING for some minutes. Read SYNCING for a long time after a restart.

Shutdown ​

On SIGINT or SIGTERM, the node stops the JSON-RPC server, the mempool prewarm, the transaction validation thread and the P2P threads. It then waits for the batch in flight and writes all canonical blocks that are still in memory. The log line stopping: writing in-memory canonical blocks marks this step.

Disk write volume ​

The writer writes much more to the disk than reth for the same block (#59). Measured live on mainnet on 2026-10-16, over 250 blocks, against reth 2.7 on the same server. Each one had a disk of its own. Cinder was a test build of 0.9.1.

Per blockreth 2.7Cinder
Written to the device20.4 MB65.2 MB
Write operations on the device4 50115 019

Cinder wrote 3.2 times more bytes. Almost all of them are MDBX copy-on-write copies of pages that already exist. A changed account or slot updates its value and several trie nodes, each in a different page of a large table. The commit copies each such page and the B-tree branches above it. The net growth of the tables is small.

The project tested two changes and kept neither:

  • Commits without fsync. The kernel wrote the same pages in fewer operations, but in larger bursts. The blocks that met a burst were slow: the mean time of a block went up by 5.7 ms.
  • The writer at the idle I/O class. The interval of the median included 0.

Next candidates are a small and frequent sync, one commit for more than one block, and fewer stored trie nodes. For the A/B results, read Performance.

Measured effect ​

One commit per block on the engine loopWriter thread
cinder-replay round trip minus newPayload (the wait for the write)0.4 to 14 s4 ms mean, 7 ms p90 (150 blocks)
After a stopthe node fell 400 blocks behind and kept falling352 blocks in 10 minutes, with the root over the unwritten ancestors' trie nodes (197 with the writer alone)

In a 30-minute desktop window at the head (2026-10-05, 150 blocks), a batch held 1.03 blocks on average and took 8.3 seconds. On the dedicated server, a batch took 0.93 seconds on average (60 writes, 2026-10-10).

With the writer on, only a small subset of the EEST tests ran to the end under the thread sanitizer (TSan) (#31). For the full measurements, read Performance.