How does the internal listpack encoding for small hashes affect memory usage, and at what point does Redis switch to a different, less compact encoding?
Short answer
Redis stores small hashes using a highly compact listpack encoding by default, which uses significantly less memory than the alternative, but automatically switches to a full hash table encoding once the hash exceeds a configurable number of fields or a configurable maximum…
Advertisement
AModel answer
Redis stores small hashes using a highly compact listpack encoding by default, which uses significantly less memory than the alternative, but automatically switches to a full hash table encoding once the hash exceeds a configurable number of fields or a configurable maximum field value size, trading some memory efficiency for better performance on larger hashes.
Example
CONFIG GET hash-max-listpack-entries CONFIG GET hash-max-listpack-value OBJECT ENCODING user:100
Real-world example
A team storing millions of small user preference hashes carefully tunes the listpack thresholds to keep as many hashes as possible in the compact encoding, achieving significant memory savings across their entire dataset.
Likely follow-ups
- What are reasonable threshold values for a typical application's hash sizes?
- What performance difference exists between listpack and hash table encoding?
Related topics