Search code examples
redisjacksonobjectmapper

RedisJSON vs Jackson ObjectMapper


There are two ways to handle JSON in Redis:

  1. Use Jackson ObjectMapper to serialize the data before storing it in Redis, and then deserialize it when retrieving the data.

  2. Use the RedisJSON module to store and query JSON data directly, without the need for serialization and deserialization.


The difference between the two is that using the RedisJSON module allows processing to be handled on the Redis server, whereas using Jackson ObjectMapper requires the application to handle the processing.

Considering factors such as performance, load, and bottlenecks, I'm curious about which method is better suited for different scenarios.


Solution

  • A somewhat broad question, but let me try to give a very brief summary, focusing on the major difference. Depending on the use case - of course - these considerations might or might not be important.

    Implementations using a hash for backing

    • require that the whole JSON text be fetched even if only a portion of it needs to be read, e.g. an element of an array
    • require that the whole JSON text be stored (transmitted over the wire) when it needs to be modified

    Implementations using the RedisJSON module

    • allows for reading and modifying fine grained sections of the JSON text, by specifying a JSON Path to the elements
    • allows for modifications that consider the data type, e.g. incrementing a number stored in an array or toggling a boolean value (without having to first read the data)
    • allows inspecting the structure of the JSON text without having to read it, e.g. read array length, identify data types, etc.

    All of the latter would ultimately result in performance improvements as they would both decrease the amount of data sent over the wire, but also the need to parse the data in some cases. Consider incrementing a number stored in an array in both cases:

    Case 1 - the whole JSON text needs to be fetched (if it is larger than the array in question - including all irrelevant data); then the path would have to be applied to get to the array n question; then the value needs to be deserialized, so it is incremented; then data needs to be serialized back and returned to the server;

    Case 2 - a single command to the server indicating the path to the element to increment needs to be sent