Redis

Enable Compression with Redis

Remote Dictionary Server, or Redis for short, is a lightning-fast in-memory database that stores values in key-value pairs. It is mainly used as a caching mechanism for databases such as SQL and Document databases.

Since Redis is an in-memory database, the space used is critical and needs to be heavily monitored. One strategy to improve and optimize memory performance for Redis is to use compression.

By default, Redis does not provide compression for any data stored. Hence, compression techniques are implemented on the application.

Let us discuss a few techniques you can use to optimize memory performance in Redis.

Implement a Compression Algorithm

Since Redis does not compress the stored values, you must do so before storing them. There are several compression algorithms to compress strings before storing them.

Such algorithms include:

  1. LZO Compression – very fast and provides higher decompression speeds.
  2. LZ4 – efficient in speed and very easy to integrate into applications.
  3. Snappy – high compression/decompression rates.

Use Shorter Key Names

Although developers should favor more descriptive names over short ones, memory usage can quickly skyrocket if you have an extensive collection of keys in the database.

Always consider using short key names for your key-value data to avoid this.

Example:

SET this_is_a_very_large_key_name value

Instead, you can use the key name:

SET l_key_name value

This reduces Redis’s number of characters to store for your database.

Compress Field Names

The same case above can be said about the field names. And again, using a shorter field name can save a few bytes or kilobytes of your memory.

Hence, consider using short field names for your Redis data.

An example is as shown:

127.0.0.1:6379> HSET user_info id 1 firstname Moes lastname K country "United States of America"

Here, we can save some memory by refactoring the field names as:

HSET user_info id 1 fname Moes lname country US

This compresses the field names and the values.

Use List Instead of a Hash

A hash is comprised of field names and corresponding values. Although this is not a significant problem, it can be problematic when thousands of hash types come into play.

To solve this, you can opt for a list as shown:

HSET user_info id 1 fname Moes lname country US

You can convert the above hash to a list as:

LPUSH ["fname", "Moes", "lname", "K", "country", "US"]

Avoid Dynamic Lua Scripts

To save even more memory, avoid using dynamic LUA scripts that cause the cache to grow. The more scripts you load, the more you consume a lot of memory.

Enable List Compression

As mentioned, Redis does not compress any values stored in it. This includes elements inside a list. For short list values, this is hardly a problem. However, on long lists, it can be beneficial to enable compression.

In the Redis.conf file, locate the line:

sudo cat /etc/redis/redis.conf | grep list-compress
list-compress-depth 0 // change this value

Change the value of list-compress-depth to either:

  1. 1 – compresses every list node except head and tail.
  2. 2 – never compress head or head-> or tail or tail->prev
  3. 3 – start compression after head->next and tail->-prev

Upgrade your Redis Version

Another step you can take to improve memory usage in your Redis server is to upgrade your Redis version.

As of writing this tutorial, version 4.0 (latest) comes with the following features.

Closing

This guide discusses various methods and techniques you can use to optimize memory usage in your Redis cluster. However, keep in mind that not all forms are 100% guaranteed.

Thanks for reading, see you in the next one!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list