How to Boost JavaScript Objects Storage on Redis
It’s our duty to give users the services they want
As software engineers, one of our duties is to create services that satisfy users’ needs. In today’s world, users demand faster services, able to retrieve their information using the least amount of time.
To achieve that, we need, of course, the right architecture, and most of the time Redis is a part of it.
But… can we go a step further, improving the way we integrate it and the general performances?
YES! And I will show you how using some basic examples.
But first… what is Redis?
Redis is an open-source, in-memory data structure store that is widely used for its exceptional performance and versatility. With its simple key-value data model, Redis enables developers to implement various use cases such as real-time analytics, pub/sub messaging, job queues and caching.
Today we will focus on this last point, and how can we use it to achieve our goals.
Great! What do I need?
To follow all the steps, I highly suggest you clone the code publicly available in the dedicated GitHub repository. To execute the code you clone, this is what you’re going to need:
- Node.js - version 18 or above, I suggest NVM to manage it;
- Docker and Docker compose, to create a local Redis instance. If you already have it, feel free to skip this;
- RedisInsight, a GUI for Redis. If you already have some experience with the Redis CLI, you can skip this;
- A minimal knowledge of Fastify, the web framework we will run on each example;
- The basic parameters to invoke autocannon, the HTTP/1.1 benchmarking tool we will use during our comparisons.
Let’s start coding!
Firstly, we are going to write a reusable function to create a new Fastify instance.
src/fastify.mjs
As you can see, we are:
- Registering the @fastify/redis plugin, which instantiates a Redis client under the hood using the popular library ioredis;
- Using ioredis to clean all Redis’ data before starting with our tests.
Secondly, we are going to implement a basic postHandler
, just to explore the simplest Redis functionality: saving and retrieving data through plain strings .
src/step_1.mjs
Using this handler, we search for the key step_1
in Redis. If it’s there, we directly return what has been extracted, otherwise we populate it with the value success!
before returning it.
Finally, to test what we made, we must run the following scripts:
npm run redis:up
: this script creates a Redis server in a container, you need to run it only once;npm run step_1
: this script instantiates a Fastify server which will be invoked once by autocannon.
If everything is OK, you should see something like this inside RedisInsight:
Saving non-nested JavaScript objects
I know, I know, it doesn’t make any sense to store a static value inside a cache… but hey, we are just warming up ????
It’s time to write another handler, and this time we will store a different entity: a non-nested JavaScript object!
As it’s not a plain string, we must also change the structure that will host our data: we can opt for hashes , which represents record types modeled as collections of field-value pairs.
src/step_2.mjs
The structure of the handler is similar to the previous one, but this time we are using the hset
method, where the initial h
stands for hashes
.
To test this, we can run the step_2
script and see the result in RedisInsight, as we did before:
Saving nested JavaScript objects
Now things get a bit more complicated ????
Unfortunately hashes
are not the right structure for this duty, since they save the .toString()
object’s representation for nested fields: it means that we are losing our precious data!
One possible solution is to use a library like flat in order to avoid any kind of nesting.
src/step_3_flatten.mjs
But since we are deeply mutating the structure of each object, we will notice a slowdown in the API performance: we will measure it using autocannon
.
Let’s try it running step_3:flatten
script:
Thanks to RedisInsight, we can look at how data is now stored:
We must always aim for the best performance so, alternatively, we can choose to follow one of these paths:
- Using the JSON data type;
- Stringifying our object, and store it as a string.
As ioredis requires us to stringify even for the JSON data type , I will follow the latter approach as it’s simpler.
src/step_3.mjs
Running the step_3
script:
We notice that even if we achieve the same result, the stringify/parse
combination is faster, allowing us to serve more requests per second.
Looking at RedisInsight, we can see the magic happens ✨
We’re finally able to store nested objects, with dynamic shape ????…
… almost ????
Nested objects…but with some limitations
Let’s suppose for a second that you have an object which contains the results of a huge number of calculations.
It’s true that with this technique you can store objects with a dynamic shape, but in the JavaScript engine used by Node.JS and Chromium, v8, there’s a limit of ~512MB for each string size .
Imagine having a 515 MB JSON (in the code repository, it is automatically generated once you install the dependencies), and trying to load its entire content as a string: it will throw an error like this ????
It means that we can’t simply stringify every kind of object. ????
Nested objects: the solution ????
It seems to be an unbearable obstacle… ????
Well, even if we can’t configure this limit yet, we can still represent data using a different format… like buffers! 1️⃣0️⃣1️⃣0️⃣0️⃣1️⃣
Do you know how to serialize objects to buffers without stringifying them first? Let me introduce you to MessagePack : an efficient binary serialization format .
Feel free to consult the entire specification , but I advise you use the msgpackr library which implements it with an additional native acceleration to boost performance.
src/step_4.mjs
We can now run our last step through the step_4
script:
For small objects, we have the same performances of JSON.stringify
, without its drawbacks and with less use of space on Redis.
In our simple example, we are consuming ~78% less memory to store the same data! ????
Benchmark time ????
Now that we know how to proceed, it’s time to better compare MessagePack and JSON formats on arrays of different items. Will we reach the same results using heavier objects?
I’m measuring 3 different things in these tests:
- Number of items in the array. To make things harder, each object has a different content, based on the following example:
The time needed to create each array is not considered;
- The amount of memory consumed in Redis for both JSON and MessagePack;
- The number of requests/sec served using both JSON and MessagePack.
All the tests have been executed on a 16-inch 2019 MacBook Pro running both Fastify and autocannon using them concurrently as we did in the previous steps. Its specs are:
- CPU: 2.6 GHz 6-Core Intel Core i7
- Memory: 16 GB 2667 Mhz DDR4
- OS: macOS Ventura 13.4.1
And here is the outcome: Requests/sec
# items in the array | JSON | MessagePack |
---|---|---|
1 | 101k | 101k |
10 | 87k | 96k |
100 | 52k | 60k |
1000 | 8k | 16k |
10000 | 850 | 1k |
100000 | 65 | 138 |
1000000 | 20 (with 6 timeouts) | 25 |
As you can see, we reach the best scenario for 1000 items, where we double our throughput ????. Memory consumption on Redis
# items in the array | JSON | MessagePack |
---|---|---|
1 | 128 B | 72 B |
10 | 816 B | 208 B |
100 | 7 KB | 2 KB |
1000 | 64 KB | 16 KB |
10000 | 768 KB | 160 KB |
100000 | 7 MB | 2 MB |
1000000 | 80 MB | 20 MB |
In this case, the best scenario is reached with 10000 items where we use 380% less space ????.
In all the presented scenarios, MessagePack is better than JSON. Things could change a bit on the requests/sec metric if we start using libraries like fast-json-stringify , but the size on memory will always be the same.
Summary
By leveraging the power of MessagePack and Redis, developers can significantly reduce memory consumption in their applications and speed up their services. MessagePack offers a compact binary format that is not only efficient in terms of storage but also in terms of memory usage.
With its wide language support and easy integration, MessagePack is a powerful tool for optimizing memory usage in various applications: you can also use it to store data inside IndexedDB, for example.
Will you give it a chance? ????
Insight, imagination and expertly engineered solutions to accelerate and sustain progress.
Contact