Boost Your Redis-Powered Caching: Unleashing the Magic of Efficient Data Serialization
Tags: redis, cache, backend, webdev
Published: 2023-10-10 13:13:10


In the quest to optimize application performance, caching is often the go-to strategy. However, many popular standalone caching solutions like **Redis** and **Memcached** primarily deal with string data. Consequently, data originating from the application must be converted into string format before it's pushed into the cache. This process essentially requires data serialization for storage.

Even with various caching strategies like Cache-Aside, Write-Through, Write-Behind, or Read-Through, the fundamental requirement remains the same: reading and writing data to and from the cache. When retrieving data from the cache, the application is faced with the task of deserialization, converting the stored string back into its original data format. It's important to note that this serialization and deserialization process can consume a significant amount of time, with the time required directly linked to the size of the data.

Thankfully, there are alternative methods for serializing data that don't involve converting it to JSON. One such approach is the use of **Messagepack standards**. Messagepack offers the advantage of efficient data storage and significantly faster data serialization. Find more about [messagepack](https://msgpack.org/index.html).

A simple representation of how messagepack stores the data.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/der79tuke6fyryu8r3y8.png)



## Advantages of messagepack:
**Efficiency**: MessagePack is a binary serialization format, whereas JSON is a text-based format. This means MessagePack typically requires less space to represent the same data, resulting in smaller message sizes. Smaller payloads can lead to reduced network and storage usage, which can be especially important in resource-constrained environments.

**Speed**: Due to its binary nature, MessagePack is faster to encode (serialize) and decode (deserialize) compared to JSON. This speed advantage is particularly beneficial in applications where rapid data serialization and deserialization are crucial for performance.

**Compatibility**: MessagePack is designed to be language-agnostic, and there are libraries available for a wide range of programming languages. This makes it easier to work with MessagePack in a multi-language or multi-platform environment, where different components of an application may be written in different languages.

**Native Data Types**: MessagePack includes native support for a wide range of data types, including integers, floats, strings, arrays, and maps. This can lead to more efficient and accurate serialization and deserialization of data, especially when dealing with complex or nested data structures.

**Cross-Platform**: MessagePack's binary format makes it suitable for cross-platform data exchange. It can be used to transmit data between different systems and architectures without compatibility issues.

**Backward and Forward Compatibility**: MessagePack provides a level of backward and forward compatibility. New fields or data types can be added to your MessagePack messages without breaking compatibility with older versions of your software. This flexibility is valuable when working with evolving data schemas.

**Reduced Overhead**: JSON includes metadata and human-readable keys, which add overhead to the data. MessagePack, being a binary format, eliminates much of this overhead, resulting in more compact data representation.

**Streaming Support**: MessagePack is well-suited for streaming data, as you can serialize and deserialize data incrementally. This is particularly useful for scenarios like real-time data processing.

**Binary Data Handling**: MessagePack is excellent for handling binary data, such as images or serialized objects, as it doesn't require escaping or encoding of binary characters.

**Ecosystem**: MessagePack has a growing ecosystem of libraries and tools that support it, making it easier to integrate into your existing applications and infrastructure.

One of the noteworthy feature of Messagepack is its compatibility with a wide range of programming languages. In this post, we will explore Messagepack's performance benefits by using Python to serialize a Python dictionary with Messagepack tools and compare this performance to JSON. We conducted benchmark tests with a 70KB JSON data loaded as a dictionary.

With Messagepack tools:

- Serialization is approximately **4 times faster** compared to JSON.

- Deserialization is around **1.2 times faster** than with JSON.


```{python}
import json
import msgpack

# Replace your data in 'dict_70_kb'
nonserialized_dict_70kb = dict_70_kb

# Serializer methods
def json_serializer(data):
    json_serialized_dict_70kb = json.dumps(data)
    return json_serialized_dict_70kb
def msgpack_serializer(data):
    msgpack_serialized_dict_70kb = msgpack.dumps(data)
    return msgpack_serialized_dict_70kb
def msgpack_pack_serializer(data):
    msgpack_serialized_dict_70kb = msgpack.packb(data)
    return msgpack_serialized_dict_70kb

# Deserializer methods
def json_deserializer(data):
    deserialized_dict_70kb = json.loads(data)
    return deserialized_dict_70kb
def msgpack_deserializer(data):
    deserialized_dict_70kb = msgpack.loads(data)
    return deserialized_dict_70kb
def msgpack_pack_deserializer(data):
    deserialized_dict_70kb = msgpack.unpackb(data)
    return deserialized_dict_70kb

print("JSON serialization: ", end='')
%timeit -n 10000 -r 15 json_serializer(nonserialized_dict_70kb)
print("Msgpack serialization: ", end='')
%timeit -n 10000 -r 15 msgpack_serializer(nonserialized_dict_70kb)
print("Msgpack pack serialization: ", end='')
%timeit -n 10000 -r 15 msgpack_pack_serializer(nonserialized_dict_70kb)

json_serialized_dict_70kb = json_serializer(nonserialized_dict_70kb)
msgpack_serialized_dict_70kb = msgpack_serializer(nonserialized_dict_70kb)
msgpack_pack_serialized_dict_70kb = msgpack_pack_serializer(nonserialized_dict_70kb)

print("JSON deserialization: ", end='')
%timeit -n 10000 -r 15 json_deserializer(json_serialized_dict_70kb)
print("Msgpack deserialization: ", end='')
%timeit -n 10000 -r 15 msgpack_deserializer(msgpack_serialized_dict_70kb)
print("Msgpack pack deserialization: ", end='')
%timeit -n 10000 -r 15 msgpack_pack_deserializer(msgpack_pack_serialized_dict_70kb) 
```

Here's the python's time benchmarks, with huge number of iterations.

- JSON Serialization - Conversion to JSON
- Messagepack Serialization and Messagepack pack Serialization (both are same) - Conversion to Binary Messagepack

same info applies to the deserialization as well.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4g20d3hjjz343aqig3l.png)

A simple scatterplot to display the difference between different strategies.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwkvp6zovpcys9jdnghb.png)


Try using messagepack in your application and check the performance benchmarks. Let me know if you have any questions or difficulties with messagepack.