Compressing strings before putting them in redis - does it make sense?

Viewed 12168

A bit more detail: we're already trying to take the most advantage of zipmaps, ziplists, etc, and I'm wondering whether these representations are already compressed, or are just serialized hashes and lists; does compression significantly reduce memory usage?

Also, does compression overhead at the app server layer get offset by lower network usage? StackOverflow's experience suggests it does, any other opinions?

In brief, does it make sense - for both short and longer strings?

3 Answers

There's a practical way to get good compression, even for very small strings (50 bytes!) -

If your values are somewhat similar to each other - for example, they're JSON representations of a few related classes of objects - you can precompute a compressor/decompressor dictionary based on some example text.

It sounds complicated, but it's simple in practice - and simpler still with the right wrapper code to handle it.

Here's a Python implementation:

https://github.com/internetarchive/openlibrary/blob/master/openlibrary/utils/compress.py

and here's a wrapper for compressing a specific class of strings: (short JSON records)

https://github.com/internetarchive/openlibrary/blob/master/openlibrary/utils/olcompress.py

One catch: to do this efficiently, your compression library must support 'cloning' the internal state. (The Python library does) You can implement something similar by prepending the example text when compressing, but this means paying an extra computation cost.

Thanks to solrize for this awesome trick.

Related