Skip to content

Redis Memory Optimization

sripathikrishnan edited this page Apr 20, 2012 · 12 revisions

WORK IN PROGRESS

Redis keeps all its data in memory, and so it is important to optimize memory usage. This document explains several strategies to reduce memory overheads. This document builds up on the official memory optimization notes on redis.io, so you should read that document first.

Special Encoding for Small Aggregate Objects

Redis uses a special, memory-efficient encoding if a list, sorted set or hash contains few elements, or if the size of each element is less than a threshold.

The following directives control when this special encoding is used :

hash-max-zipmap-entries 64 (hash-max-ziplist-entries for Redis >= 2.6)
hash-max-zipmap-value 512  (hash-max-ziplist-value for Redis >= 2.6)
list-max-ziplist-entries 512
list-max-ziplist-value 64
zset-max-ziplist-entries 128
zset-max-ziplist-value 64

How do you choose the best values for these parameters? Analyze your data.

TBD : Explain how memory analyzer can help identifying the right values for these parameters.

Avoid multiple databases

Use Integer IDs

Avoid Strings smaller than 100 bytes

Use key expiry judiciously

Optimize for small maps, lists and sorted sets

Integer Sets - 16, 32 or 64 bits

Use lists instead of dictionaries for small, consistent objects

Use Bitmaps to encode data

Create your own data types

Clone this wiki locally