Skip to content

Compressing Data

Valkey GLIDE supports compressing string values, reducing memory usage and network bandwidth. When enabled, values are transparently compressed before being sent to the server and decompressed on retrieval.

from glide import (
GlideClient,
GlideClientConfiguration,
CompressionConfiguration,
CompressionBackend,
NodeAddress,
)
compression = CompressionConfiguration(
enabled=True,
backend=CompressionBackend.ZSTD,
compression_level=3,
min_compression_size=128,
)
config = GlideClientConfiguration(
addresses=[NodeAddress("localhost", 6379)],
compression=compression,
)
client = await GlideClient.create(config)

Compression works well when you primarily use basic read/write commands (SET/GET and variants) with large string values.

If your application uses server-side string manipulation on the same keys (append, increment, bit operations), don’t compress those keys. The server would operate on compressed bytes and produce garbage. Handle compression at the application level for those cases.

OptionDefaultDescription
enabledfalseToggle compression on/off
backendZSTDCompression algorithm: ZSTD or LZ4
compressionLevelBackend default (ZSTD=3, LZ4=0)Compression level; valid ranges are backend-specific
minCompressionSize64 bytesValues below this threshold are not compressed (minimum: 6 bytes)
maxDecompressedSize512 MBMaximum decompressed size; protects against decompression bombs

The following commands are enabled when compression is configured:

  • SET, GET
  • MGET, MSET
  • GETEX, GETDEL
  • SETEX, PSETEX, SETNX

The following commands are not supported and will return an error while compression is enabled:

  • String manipulation: APPEND, GETRANGE, SETRANGE, STRLEN, LCS
  • Numeric operations: INCR, INCRBY, INCRBYFLOAT, DECR, DECRBY
  • Bit operations: GETBIT, SETBIT, BITCOUNT, BITPOS, BITFIELD, BITFIELD_RO, BITOP

To understand how compression works under the hood, see our section on Data Compression.