Your multi-terabyte simulation output lives in an object store, and every plot starts with the same four-minute wait: a monolithic science file must be partially fetched, parsed, and decoded before a single map can render. The classic fix — slicing datasets into many files — works until you need consistent metadata, compression, and chunk layouts that every language agrees on. Zarr grew directly out of that pain: arrays are stored as chunks with keys like c/2/0/3, the metadata is one small JSON document, and any backend that can put and get bytes can hold an array. Zarr Python 3.x is a ground-up rewrite of that idea: asynchronous I/O at the core, first-class support for both the v2 and v3 format specs, and a composable codec system for compression, checksums, and sharding.
The rewrite introduces a different kind of pain. The public surface looks charmingly synchronous — zarr.open(...) feels like NumPy — but underneath, every read and write is asyncio. Call it from the wrong context and you get a SyncError; wrap it in a thread pool and you may wonder why your executor settings never apply. Between Dask distributed schedulers, Jupyter’s own running loop, and GUI toolkits that own the main thread, “which event loop am I on?” becomes a genuine debugging skill. The only durable answer is to read the bridge code itself.
There is a third pain point: the format looks simple in tutorials, but the real tree is a deliberate machine — eight store implementations, two metadata dialects, and a codec stack where array-to-array, array-to-bytes, and bytes-to-bytes stages chain together. This post walks the actual source of zarr-developers/zarr-python at commit 9b6613bd7555 on the main branch (December 2026), with line-anchored links into GitHub so every claim can be checked against the code.
The four load-bearing layers of zarr-python: a thin synchronous API, the Array/AsyncArray pair, the Store abstraction with its implementations, and the codec stack that turns chunks into bytes.
Reading the overview from left to right:
- AsyncArray, L370 — the async engine, generic over
ArrayV2MetadataandArrayV3Metadata; it owns chunk selection, encoding, and I/O. - Array, L1925 — the synchronous facade; every method is a thin wrapper that blocks on the async core.
- sync, L121 — the bridge itself: submits a coroutine to a dedicated
zarr_ioevent-loop thread and waits for the future. - Store, L60 — the abstract store with async
get/set/deleteplus byte-range request types. - LocalStore, L194 — the filesystem store: one file per key, directories per hierarchy node.
- FsspecStore, L70 — the bridge to fsspec filesystems such as S3, GCS, and HTTP.
- BloscCodec, L141 — the bytes-to-bytes workhorse: blosc compression with shuffle and thread control.
- ShardingCodec, L448 — packs many inner chunks into one shard blob with a
uint64offset index. - Buffer, L135 — the abstract byte buffer; a
BufferPrototype(L584) selects NumPy or CuPy implementations.
The detail view adds the metadata formats, chunk grids and key encodings, all store implementations, the full codec families, the buffer hierarchy, and the sync-async bridge.
One store interface, eight implementations
The whole storage layer hangs off one abstract class. Store, L60 declares async get, set, delete, exists, and listing methods, and defines byte-range requests as three frozen dataclasses: RangeByteRequest, OffsetByteRequest, and SuffixByteRequest (L41–L57). That is the entire contract a backend must satisfy — which is why the same array code runs unchanged against a laptop disk and an S3 bucket. If you have ever pointed a chunk store at self-hosted MinIO instead of AWS to keep a science workflow reproducible, this interface is the reason that swap is a one-line change.
Addressing is layered on top by StorePath in src/zarr/storage/_common.py (L46): a store plus a node path, with async get, set, delete, and JSON helpers. Every array and group lives at a StorePath, so hierarchy navigation is path arithmetic rather than backend-specific logic.
The implementations are genuinely different engines, not thin wrappers over one base: LocalStore, L194 uses real filesystem calls; MemoryStore, L41 is a dict with a GPU variant at L243; FsspecStore, L70 wraps fsspec with read-ahead caching; ZipStore (L86 in _zip.py) reads and writes zarr archives inside a zip file; WrapperStore (L23 in _wrapper.py) decorates any other store; and _obstore.py binds the Rust obstore library for S3, GCS, and Azure with fewer Python hops. Two stores — memory and local — also expose listing, which the consolidated-metadata machinery relies on.
Two spec versions, one metadata model
Zarr v3 replaced the multi-file metadata layout of v2 with a single .zarr.json document per array, and the code models both dialects explicitly. ArrayV2Metadata, L65 carries the classic compressor and filters fields of a .zarray, while ArrayV3Metadata, L512 carries a typed codec list plus a chunk grid — regular (RegularChunkGridMetadata, L271) or rectilinear (RectilinearChunkGridMetadata, L302, a 3.x addition for irregular chunk boundaries).
Chunk keys are equally explicit: ChunkKeyEncoding, L33 is the abstract base, with the v3 default (c/2/0/3 slash style) at L71 and the v2 dot style at L93. Group-level metadata gets its own optimization: ConsolidatedMetadata (L144 in group.py) embeds a snapshot of the whole hierarchy into one document, so opening a deep tree on remote storage costs one GET instead of dozens — a huge win when every round trip is a network flight.
The v2 compatibility layer is not an afterthought. src/zarr/codecs/numcodecs/_codecs.py wraps the entire numcodecs library — Blosc (L205), Shuffle (L233), CRC32 (L317), and roughly twenty more — as typed codec classes, so legacy v2 arrays keep decoding while v3 chains get access to the same algorithms.
From 177 lines of glue to a 5,332-line core: build facts
The numbers in the tree tell the rewrite story precisely:
src/zarr/core/array.pyis 5,332 lines:AsyncArraybegins at L370, the synchronousArrayat L1925, andArray.__getitem__at L2590 — two-thirds of the file is the async half, the reverse of what the import surface suggests.src/zarr/core/group.pyis 3,434 lines, withAsyncGroupat L515 andGroupat L1951;src/zarr/core/indexing.pyadds 1,509 lines of selection-to-chunk math.- The glue is tiny on purpose:
src/zarr/core/sync.pyis 177 lines, and it hard-codes its own housekeeping — a daemon thread literally namedzarr_io(L175) runs the event loop,SyncError(L30) fires if you callsync()from inside a running loop, andSyncMixin(L197) is whatArrayandGroupinherit to become synchronous. - Defaults live in
src/zarr/core/config.py:default_zarr_format: 3,async.concurrency: 10, codec-chainbatch_size: 1, andallow_unsafe_pickle_decode: False— decoding pickle-based codecs can execute arbitrary code, so it is opt-in. Sharding coalescing is tuned too: shards are grown up to 16 MiB with at most a 1 MiB gap (L104–L105). - The shard index is a
NamedTuple(_ShardIndex, L210 insharding.py) holding auint64array of(offset, length)pairs per inner chunk, withMAX_UINT_64as the “missing” sentinel;is_dense()(L228) detects fixed-size shards that tile exactly, unlocking a vectorized whole-shard decode instead of per-chunk loops. - The codec zoo is compact at the leaf level:
gzip.pyis 68 lines,zstd.py78,transpose.py104,blosc.py286 — the complexity concentrates in sharding at 1,637 lines.
That last ratio is the design in miniature: making one hard thing fast (sharding) costs twenty times more code than adding another compression algorithm.
The codec stack: three families, one chain
Every chunk passes through three codec families in a fixed order. Array-to-array codecs transform the multi-dimensional chunk before serialization: TransposeCodec, L30 reorders dimensions so that the fastest-varying axis matches the storage layout. Exactly one array-to-bytes codec serializes: BytesCodec, L49 handles dtype casting and endianness, with VLenUTF8Codec (L26 in vlen_utf8.py) as the variable-length-string sibling. Then any number of bytes-to-bytes codecs compress and checksum: GzipCodec, L32, ZstdCodec (L37 in zstd.py), Crc32cCodec (L21 in crc32c_.py), and BloscCodec, L141.
ShardingCodec, L448 is the outlier: it is an array-to-array codec whose job is storage topology, not values. It takes its own inner codec chain, splits a shard into sub-chunks, and maintains the offset index described above — so one 100 MiB shard object can contain thousands of independently decodable inner chunks, turning “one key per chunk” into “one object per megabyte-scale shard” without giving up partial reads.
The default chain runner processes chunks in mini-batches with concurrent fetch, decode, encode, and store phases; a fused variant that merges stages into a single synchronous pass exists behind a config flag in the library defaults but stays opt-in so behavior is unchanged for existing users. Partial decode is negotiated per chain: it works only when there is a single array-to-bytes codec that supports it and no transform stages break the slice-to-byte-range correspondence (L629–L647 of the chain runner module).
Buffers, threads, and the GPU escape hatch
Nothing in the engine touches a NumPy array directly. Chunks flow as Buffer (1-D bytes) and NDBuffer (n-dimensional, L325 in core.py) instances created through a BufferPrototype — a tiny NamedTuple pairing a buffer and an ndbuffer class. The NumPy-backed prototypes live in cpu.py and are the defaults; CuPy-backed ones in gpu.py (Buffer at L34) activate through zarr.config with enable_gpu(), swapping every allocation site at once. This is the same decoupling philosophy that makes Apache Arrow interchangeable across languages: algorithms are written against an abstract memory interface so the concrete allocation strategy — host, device, or zero-copy views — is a configuration decision, not a rewrite.
Threading follows the same discipline. The zarr_io loop thread is shared process-wide, executor creation respects threading.max_workers from config, and a semaphore bounds per-array concurrency (_with_semaphore, L213 in sync.py). Because Dask and Zarr both schedule work, the two schedulers stay out of each other’s hair: Dask decides which chunks to compute, Zarr decides how each chunk crosses the storage boundary.
Honest Limits
- Line anchors match commit
9b6613bd7555ofmain; this file churns constantly, so re-grep before citing. - Coverage is partial by design:
indexing.py(1,509 lines), the rectilinear grid machinery,_cli/, andexperimental/are skimmed here, not dissected. - The obstore-backed store requires an extra optional dependency, and
ZipStoreis best for archival reads — neither replacesLocalStorefor everyday writes. - v2 and v3 are not symmetric: v2 has one compressor plus filter list, v3 has a codec list and no global compressor field; round-tripping relies on the numcodecs wrappers, and exotic v2 codecs may not map cleanly onto v3 chains.
- The default chain runner’s
batch_size: 1is conservative; throughput tuning (larger batches, the fused variant,async.concurrency) changes behavior and is your responsibility to benchmark. - GPU buffers depend on CuPy and are exercised far less in CI than the NumPy path — treat
enable_gpu()as an early-adopter feature.
Enjoyed this post? Never miss out on future posts by following us