Dùng appropriate data type: Hash thay vì nhiều String key riêng lẻ cho object nhỏ (Redis tự động dùng listpack encoding khi Hash nhỏ hơn hash-max-listpack-entries entries và mỗi value nhỏ hơn hash-max-listpack-value bytes — memory efficient hơn hashtable).
- Lưu ý: Redis 7.0 đổi tên ziplist thành listpack; config hiện tại dùng
hash-max-listpack-entriesvàhash-max-listpack-value. - Compressed encoding: Redis tự optimize encoding dựa trên size (listpack, intset, skiplist).
- Dùng
OBJECT ENCODING keyđể xem encoding. - Giảm key size:
usr:1001:prfthay vìuser:1001:profiletiết kiệm memory nhân với hàng triệu key. - Set TTL cho tất cả cache key tránh memory leak.
- Dùng
MEMORY USAGE keyđể đo memory một key,MEMORY DOCTORđể chẩn đoán vấn đề memory. - Nén value ở application layer (gzip JSON trước khi lưu) giảm 70-80% với JSON lớn.
- Monitor
used_memory,mem_fragmentation_ratio(>1.5 là vấn đề fragmentation — có thể bậtactivedefrag yeshoặc dùngMEMORY PURGEđể defragment).