Chunk Storage & Streaming
What of a world is stored and what is regenerated - the world tier, chunk bases, player edits and baking - and how a chunk is materialized from the world tier and streamed to the clients.
A world is mostly not stored. This page says which parts of it live where and for how long, and how a chunk is built from the world tier when a client asks for it. How the zone keeps chunks in memory once they are built is on Chunk Caching.
Storage: voxels, chunks, player edits and regeneration
Four things exist, at three different lifetimes. Conflating them is the most common way to misdescribe this system, so this section is deliberately explicit about which is which.
1. The world tier (rasters, vector features, the chronicle) is a pure function of (seed, WorldConfig).
Never stored anywhere — regenerating it costs milliseconds to low single-digit seconds depending on world
size (see Benchmarks), so persisting it would only be persisting a cache. Held in memory for
the life of the server process.
2. A generated chunk base is also a pure function — of the world tier plus a chunk coordinate — so it is
also never durably stored, only cached: an in-process LRU of decoded chunks (ChunkCache, 128 in
zone-server) with the encoded wire payloads behind it, so a chunk that falls out of the LRU is decoded again
rather than generated again — Chunk Caching walks through it with examples.
ChunkCache’s key folds in (seed, pipelineVersion, chunkCoordinate), so retuning any stage changes every key
at once — nothing stale can ever be served, and no explicit invalidation pass is needed.
3. Player edits are a sparse delta on top of the base, kept in memory while the server runs and saved to
the zone’s database (see Saving edits below). ChunkDelta holds, per touched voxel, how much of it is left —
never a block id, because it’s derivable: unchanged material is whatever the generator put there, and carved
to nothing is AIR. Three consequences follow directly from there being no building system, only removal:
- No block id is stored. The voxel that still has material keeps the generator’s material.
- Occupancy only ever falls, enforced by the one party (
ChunkStore) holding the base to compare a removal against — a removal that would raise occupancy is a placement system arriving by accident and is refused outright. - A voxel appears once, keyed on position rather than logged, so mining the same spot across many swings costs one entry, not one per swing.
Player-built walls and floors stay out of the voxels on purpose. They live in a cell overlay beside them, see Structure Collision.
Every read goes through one path: ChunkStore.merged(chunk) returns the baked blob if the chunk has one,
otherwise the cached/generated base with any delta applied. There is deliberately no API that hands out an
unmerged base to a caller that might be authoritative over anything — line of sight, projectile collision and
movement validation all need the server’s own merged view, and a client-authoritative alternative is the
single most exploited bug class in multiplayer voxel games.
Baking: once a chunk’s delta reaches roughly the RLE-encoded size of its own base — which, because a removal costs about 3 bytes and a mostly-solid base compresses far better than that, happens around 3% of the chunk edited, well before the 30%-coverage backstop that exists only for chunks whose base is unusually large to begin with — the merged result is written back as the new base and the delta is dropped. Heavily mined or built-up ground gets cheaper to serve from then on, not more expensive, because nearly-air or newly-uniform ground run-length-encodes to almost nothing. Baking is also the only safe migration path across a pipeline version change: bake every outstanding delta first (which pins the current terrain as the new, frozen base), then ship the new generator; every untouched chunk simply regenerates against the new version.
Saving edits: every edited chunk is one chunk_edit row, stored the way ChunkStore holds it — the
removals while it is a delta, the deflated RLE blob once it is baked — together with its revision and the
shapeVersion and pipelineVersion it was dug under. ChunkEditJournal writes the chunks edited since the
last write every chunk-stream.edit-flush-seconds (10 s by default) and once more at shutdown; the write is a
DB job, so the tick never waits for it. At boot, ChunkEditBootRunner puts the rows back before any player
can log in, with their revisions, so a client that cached a chunk is told it is current. A row whose versions
disagree with the live world is dropped: removals only mean something on the base they were taken from. A
crash loses at most the last flush interval of digging.
4. The world’s identity — name, seed, dimensions, wrapX/wrapY, and the three-part version vector below
— is persisted as a real JPA row (PersistedWorld). A hash over every field
that decides terrain shape (shapeVersion) is stored alongside it and recomputed from the row on every boot,
specifically so a field that matters to generation and was never given a database column shows up as a
mismatch rather than as a world that silently regenerates slightly wrong forever (this caught a real bug once:
wrapX/wrapY decided the coastline and had no column for as long as the row existed).
Three independent numbers gate whether a client may generate its own chunk bases at all —
pipelineVersion (did the stage graph or its parameters change?), blockPaletteVersion (a hash over the
block id→name mapping — same terrain, different rock), and chunkFormatVersion (the RLE wire format). Three
separate numbers rather than one, because each breaks a payload a different way and the fix a client needs to
hear is different for each. chunkFormatVersion mirrors RleCodec.VERSION, the one version number in the
module that is not part of the pre-release reset above — it is a literal name
(CHUNK_ENCODING_RLE_V2) baked into chunk.proto, a real statement between two build artefacts rather than
bookkeeping with no counterparty yet.
// The only two calls a caller needs — read is always merged, write is always a batch.
val chunk = chunkStore.merged(ChunkPos(3, 4, 0))
val outcome = chunkStore.carve(ChunkPos(3, 4, 0), removals) // removals: packed (voxelIndex, remainingOccupancy)
// outcome.changed == 0 means nothing actually changed - don't announce a patch or bump a revision
// outcome.baked == true means this carve just replaced the delta with a new, frozen base
Chunk materialization and streaming
At request time (core/ChunkHeightSampler.kt → voxel/ChunkMaterializer.kt): sample the base raster, apply
every vector feature touching the chunk in priority order (river channels, trough cross-sections, settlement
grading, town structures), then stratify into voxels using rock hardness and soil depth. A voxel carries a
material and an occupancy fraction (0–255), not just a material — a surface at 40.3 m is genuinely 30% of
the voxel spanning 40–41 m, and the client’s Surface Nets mesher (see
World & Terrain) reconstructs that to a fraction of a centimetre rather than
snapping to the nearest whole voxel. Carving (removal, whether by a player mining or by generation cutting a
mine shaft through its own masonry) is written last, in a second pass over the same buffer — additions have
to exist before a hole can be cut through them.
world/stream/ on the zone-server side owns the running world’s ChunkStore, saves its edits through
ChunkEditJournal, and streams merged RLE chunks
to clients over dedicated bnet-messages (see Networking): a ChunkManifestSMSG
announces (position, revision) pairs for a player’s view volume, the client asks only for what it doesn’t
already hold, and a held chunk is withdrawn only once it is one chunk past the view
(chunk-stream.release-margin-chunks), so walking back and forth over the view edge does not re-send a
row each time. Edits travel afterward as small ChunkPatchSMSG diffs fanned out to that chunk’s
subscribers as retained buffer duplicates — thirty players near a ten-voxel edit cost about 1.5 kB between
them, not thirty re-sent chunks. Base-plus-delta transmission (sending a hash plus the edit list for an
untouched chunk, instead of the full terrain) is designed for but deliberately not turned on: it requires the
client to regenerate bit-identical base terrain, and the Godot client is C# — a different floating-point path
from the server’s Kotlin/JVM one — which is exactly the case that makes shipping this without real bandwidth
numbers to justify it dangerous.
Generating a chunk takes milliseconds, so the tick thread does not do it. An unedited chunk a player asks
for is materialised, RLE-encoded and deflated on a zone-chunk-worker thread (ChunkWorkers,
chunk-stream.encode-workers, 2 by default), at most chunk-stream.encodes-per-tick (16) started per tick
across all players, and each chunk only once however many players want it. The finished payload is handed
back to the tick and sent on a later tick; until then the chunk waits in the player’s send queue. The
payload then stays in the encoded cache, for the next player and for the hot cache to rebuild from (see
Chunk Caching). Two
chunks are still encoded at once on the tick: the one the player stands on, because arriving on nothing is
worse than a hitch, and any chunk somebody has edited, whose base is already hot. A carve does not encode
the new revision just to decide between a patch and a snapshot; it compares against the payload size the
holders already have. The ground-block lookup that trampling and fire use builds its columns on the same
workers. Deflate stays at level 9: measured on 121 surface chunks, level 6 is 11% more bytes, and the 80 µs
level 9 costs per chunk is paid on a worker.
A player’s desired set is considered every tick, but only recomputed when something it depends on moved:
the player’s chunk, or ChunkService.slabEpoch, which counts columns whose slabs were first computed or
grew because somebody dug. A player standing still on a fully costed view costs one map lookup per tick.
Alongside the voxels, derived/ maintains cheap, incrementally-updated structures so hot paths never touch
raw voxels directly: WalkableTile (per-column walkable spans for a given agent’s step/slope profile) and
OpacityGrid (a downsampled, occupancy-weighted line-of-sight grid). Both are kept fresh on every edit
through a per-tick rebuild budget, so an edit never stalls the tick that caused it — a query in the meantime
returns the stale structure, which is the right trade: an NPC walking through a doorway that closed two
hundred milliseconds ago is an artefact nobody files a bug about, and a tick-thread hitch every time somebody
places a fence is one everybody does. WalkableTile is the one of the two with a real reader today —
zone-server’s local pathfinding queries it directly.
Last updated 04 Dec 2026, 01:08 +0100 .