RDB Compression

Difficulty: Intermediate

Question

Should you keep RDB compression enabled in Redis? What does it compress, what does it cost, and when might you turn it off?

Answer

Yes, in almost all cases. rdbcompression yes is the default and makes Redis compress string data in RDB snapshots with LZF, a very fast compression algorithm. It usually shrinks snapshot files noticeably, which speeds up disk writes, backups and full resyncs to replicas, at the cost of a little CPU in the background process that writes the snapshot.

What actually gets compressed

  • Redis applies LZF to string values in the RDB file when they are longer than about 20 bytes and compression actually saves space.
  • Compact encodings such as listpacks and intsets are written as blobs, so they benefit too.
  • Keys, small integers and short strings are written as-is.

How much you save depends on the data. Repetitive JSON, HTML fragments and text compress well; already compressed or random data (images, encrypted tokens, protobuf) barely shrinks.

Where the CPU cost lands

Snapshots are produced by BGSAVE, which forks a child process. The child walks the dataset and does the compression, so the main thread serving clients is not blocked by it. The costs are:

  • a longer-running child, which means the copy-on-write window stays open longer while the parent keeps modifying pages
  • extra CPU usage on the host during snapshots
  • slightly more CPU when loading the RDB at startup

On a host with spare cores this is negligible compared to the I/O saved.

When turning it off can make sense

  • The host is CPU-starved during snapshots and disk or network is plentiful.
  • The data is incompressible, so the effort buys nothing.
  • You compress the file afterwards anyway with a stronger tool such as zstd before shipping it to object storage.

Measure before changing it: compare snapshot size and rdb_last_bgsave_time_sec from INFO persistence with the setting on and off.

Related settings

SettingDefaultPurpose
rdbcompressionyesLZF-compress strings in RDB
rdbchecksumyesAppend a CRC64 checksum to detect corruption, small cost on save and load
savesnapshot rules such as 3600 1 300 100 60 10000When to run BGSAVE automatically
stop-writes-on-bgsave-erroryesRefuse writes if snapshots are failing

The same RDB format is used for full replication syncs, including diskless replication (repl-diskless-sync), so compression also reduces the bytes sent to a new replica.

Code examples

Checking and measuring compression

redis-cli CONFIG GET rdbcompression
redis-cli CONFIG GET rdbchecksum

# Take a snapshot and note its size and duration
redis-cli BGSAVE
redis-cli INFO persistence | grep -E 'rdb_last_bgsave_status|rdb_last_bgsave_time_sec'
ls -lh /var/lib/redis/dump.rdb

# Try without compression and compare
redis-cli CONFIG SET rdbcompression no
redis-cli BGSAVE

Compare the file size and rdb_last_bgsave_time_sec between the two runs. Remember to set rdbcompression back to yes, and persist the choice in redis.conf.

Key points

  • rdbcompression yes is the default and uses LZF on string data
  • Compression runs in the forked BGSAVE child, not the main thread
  • Gains depend on data: text and JSON compress well, random bytes do not
  • Smaller RDB files also speed up full resyncs to replicas
  • rdbchecksum adds a CRC64 for corruption detection

Common follow-up questions

  • How does BGSAVE use fork and copy-on-write?
  • Why can a large RDB snapshot cause memory spikes on the host?
  • RDB vs AOF: which gives better restart time?
  • How would you back up Redis to S3 safely?

Interview tips

  • Say yes with reasons, then show you know when the trade-off flips
  • Mention that compression runs in the forked child, so clients are not blocked
  • Suggest measuring with INFO persistence instead of guessing

Concepts covered

RDB snapshot, rdbcompression, LZF, rdbchecksum, Fork and BGSAVE

More Redis & Caching questions