Redis

Redis Max Key Length and What Happens if Exceeded

Redis is a well-known in-memory data structure store. It stores data using key-value

This guide will look at the key length and its impact on Redis.

Redis Key Length

Redis keys are binary-safe. This means any binary sequence can serve as a key. It can be anything – a string, contents of a JPEG file, and more. An empty string is also a valid key.

However, there’s a limit to the max key size (512MB). The same goes for the value size (512MB of any data type).

The proper key length is dependent on the context. In most cases, however, too long or short keys are better avoided.

Cons of Long Keys

If you’re setting a very long key, it comes with performance costs. Looking up the key in the Redis database may require several costly key comparisons. In memory efficiency, long keys are pretty memory inefficient.

If you must use long keys, consider hashing them with good algorithms, such as SHA1, SHA256, etc. In exchange for a bit more processing power, the load on memory is significantly reduced, not to mention the performance uplift.

For example, a 1024 bytes key will result in less performance and higher memory consumption. If it’s hashed using SHA256, the size is reduced to 256 bits (32 bytes) while maintaining a unique position in the Redis server.

Cons of Short Keys

The other side of the spectrum is using concise keys. From the previous example, shortening key length is preferable for performance benefits and resource efficiency. However, sacrificing clarity for the slight performance uplift is often not worth it.

Let’s break it down with an example. Consider the key u5000f as a shortened form of user:5000:followers. While u5000f is the better option performance-wise, it makes a terrible option when implemented in code.

For starters, the key name is quite unintuitive. It also doesn’t have a clear indication of what its purpose is. It’s the perfect seed for disaster down the line. As the code base grows bigger, confusing codes become harder to maintain. Instead, using user:5000:followers offers a great balance between performance and convenience.

What Happens if Redis Runs Out of Memory

Redis can handle a large number of keys. It’s designed to handle up to 2^32 keys! According to the Redis FAQ, the software is stress-tested with 250 million keys. In other words, you’re more likely to run out of system memory before hitting the Redis limit.

However, what happens when Redis reaches the maximum memory capacity? Redis comes with built-in protections, such as limiting max memory usage with the help of the maxmemory directive in the Redis configuration file.

$ sudo nano /etc/redis/redis.conf

If Redis reaches the memory limit, it returns an error to new write commands. However, it will still respond normally to read commands.

Conclusion

In this guide, we discussed the max key length Redis accepts. We also discussed the management policy when Redis exceeds the assigned memory limit. With this knowledge, you can decide how to implement Redis in your applications and scripts.

Speaking of scripts, learn more about incorporating Redis with LUA, Golang, Ruby, etc. Need to customize Redis behavior? Learn more about the Redis configuration file.

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.