skills/zarr-python/references/chunking_and_compression.md
How to size chunks for an access pattern, sharding, the available codecs and Blosc compressors, and recommended settings for numeric scientific data, for speed, and for compression ratio.
Chunking is critical for performance. Choose chunk sizes and shapes based on access patterns.
# Configure chunk size (aim for ~1MB per chunk)
# For float32 data: 1MB = 262,144 elements = 512×512 array
z = zarr.zeros(
shape=(10000, 10000),
chunks=(512, 512), # ~1MB chunks
dtype='f4'
)
Critical: Chunk shape dramatically affects performance based on how data is accessed.
# If accessing rows frequently (first dimension)
z = zarr.zeros((10000, 10000), chunks=(10, 10000)) # Chunk spans columns
# If accessing columns frequently (second dimension)
z = zarr.zeros((10000, 10000), chunks=(10000, 10)) # Chunk spans rows
# For mixed access patterns (balanced approach)
z = zarr.zeros((10000, 10000), chunks=(1000, 1000)) # Square chunks
Performance example: For a (200, 200, 200) array, reading along the first dimension:
Zarr 3.2 supports rectilinear chunks for uneven grids. Pass nested chunk lengths when a dimension has variable tile sizes:
z = zarr.create_array(
store="rectilinear.zarr",
shape=(60, 100),
chunks=([10, 20, 30], [50, 50]),
dtype="f4",
)
When arrays have millions of small chunks, use sharding to group chunks into larger storage objects:
# Create array with sharding
z = zarr.create_array(
store='data.zarr',
shape=(100000, 100000),
chunks=(100, 100), # Small chunks for access
shards=(1000, 1000), # Groups 100 chunks per shard
dtype='f4'
)
Benefits:
Important: Entire shards must fit in memory before writing.
Zarr applies compression per chunk to reduce storage while maintaining fast access.
from zarr.codecs import BloscCodec, BloscShuffle, GzipCodec
# Default: Blosc with Zstandard
z = zarr.zeros((1000, 1000), chunks=(100, 100)) # Uses default compression
# Configure Blosc compression
z = zarr.create_array(
store='data.zarr',
shape=(1000, 1000),
chunks=(100, 100),
dtype='f4',
compressors=BloscCodec(cname='zstd', clevel=5, shuffle=BloscShuffle.bitshuffle)
)
# Available Blosc compressors: 'blosclz', 'lz4', 'lz4hc', 'snappy', 'zlib', 'zstd'
# Use Gzip compression
z = zarr.create_array(
store='data.zarr',
shape=(1000, 1000),
chunks=(100, 100),
dtype='f4',
compressors=GzipCodec(level=6)
)
# Disable compression
z = zarr.create_array(
store='data.zarr',
shape=(1000, 1000),
chunks=(100, 100),
dtype='f4',
compressors=None
)
# Optimal for numeric scientific data
compressors=BloscCodec(cname='zstd', clevel=5, shuffle=BloscShuffle.bitshuffle)
# Optimal for speed
compressors=BloscCodec(cname='lz4', clevel=1)
# Optimal for compression ratio
compressors=GzipCodec(level=9)