APPENDIX · P14 · REDIS · PERFORMANCE · FOR SLIDES 14 AND 17
← Back to Slide 14
P14. A Single Command Stream and Large Keys
# большой ключ: размер, затем порциями
DEL lesson03:big:hash
HSET lesson03:big:hash f1 a f2 b f3 c f4 d
HLEN lesson03:big:hash
HSCAN lesson03:big:hash 0 COUNT 2
ZCARD leaderboard:season:7
ZRANGE leaderboard:season:7 0 1 REV WITHSCORES
UNLINK lesson03:big:hash
What the result should be
(integer) 0
(integer) 4
(integer) 4 // size — to be read
1) "0" 2) f1 a … f4 d
// small hash: everything at once,
// COUNT — just a hint
(integer) 3
1) "player:2001" 2) "1720" …
(integer) 1 // removed in the background
In the lab: MEMORY USAGE key,
redis-cli --bigkeys, SLOWLOG GET 10
A single stream
In portions
UNLINK
Commands are executed one at a time: HGETALL with one million fields blocks all clients.
First HLEN / ZCARD, then HSCAN / SSCAN / ZSCAN or a ZRANGE value between 0 and 99 instead of reading the entire range.
Frees a large amount of memory in the background thread; DEL does this immediately and blocks.
It's not just KEYS that are dangerous: any O(N) operation on a large key. Break up large keys: use one key per player for inventory, rather than a single hash for everyone; use a separate key for each season for the seasonal ranking.