Add a Redis instance
Java Serialized Objects
Use Smaller Keys
Switch to 32 Bits
Upgrade Redis Version
Use Better Serializer
Combine Smaller Strings to Hashes
Switch from Set to Intset for Numeric IDs
Switch to Bloom Filter or HyperLogLog
Shard Big Hash to Small Hash
Convert Hashtable to Ziplist for Hashes
Convert to a List instead of Hash
Compress Field Names
Enable Compression for List
Avoid Dynamic Lua Script
Reclaim Expired Keys Memory Faster
Use Smaller Keys
Redis keys can play a devil in increasing the memory consumption for your Redis instances. In general, you should always prefer descriptive keys but if you have a large dataset having millions of keys then these large keys can eat a lot of your money.
How to Convert to Smaller Keys
In a well written application, switching to shorter keys usually involves updating a few constant strings in the application code.
You will have to identify, all the big keys in your Redis Instance and shorten it by removing extra characters from it. You can achieve this in two ways:
- You can identify the big keys in your Redis Instance by using Rdbtools. This will give you details about all the keys and a way to sort your data based on the length of keys.
- Alternatively, you can run the command
Advantage of using Rdbtools is that it will give you the big keys from the whole dataset whereas the big keys commands run over a certain set of records and return the big keys from that set, hence it is difficult to identify the big keys from the whole dataset using big keys.
Let’s take an example: Suppose you have 100,000,000 keys name like
Now if you shorten the key name like
You save 16 characters by shortening your key i.e. 16 bytes which will let you save 1,000,000,000*16 = 1.6GB of RAM Memory !
Large Keys were more descriptive then shortened keys, hence when reading through your database you may find the keys less relatable, but the memory and cost savings are much efficient as compared to this pain.