简体   繁体   中英

Redis: When to use hashes vs RedisJSON?

I'm at a point where I could store a resource in Redis as either hash or RedisJSON . My particular data in this instance is are temporary data objects consisting of several string and numeric fields each. A user will invoke a process which creates a structure like:

{
    'item_id': 'k0f8h3n5m6n1w9d0k0k1m1n4b6c8f8r7'
    'amount': 3.00042
    'timestamp': 1590440708,
    'user1_status': 'pending',
    'user_2_status': 'completed'
}

This is effectively a client-user-processed queue (The queuing is handled by a separate Redis stream ) wherein each object will remain used (As a hash or RedisJSON object) for an average of about 1 hour. At any given time, there will be tens of thousands of these objects in the queue. While in the queue, the object's fields (Such as user1_status and user2_status ) will be updated several times.

Once each object is done being processed, I could either leave it in Redis or move each object to a cold storage database for log-keeping and delete from Redis. I'm not sure if I should do that or just leave it.

Which Redis data type (Hash or RedisJSON) would be more appropriate for my task? What are some of the general considerations in deciding between these two types?

Note: I realize that if I want to do something like this:

{
    'item_id': 'k0f8h3n5m6n1w9d0k0k1m1n4b6c8f8r7'
    'amount': 3.00042
    'timestamp': 1590440708,
    'user1_status': 'pending',
    'user_2_status': 'completed'
    'parent': {
        'item1': 1,
        'item2': [1, 2, 3, 4]
        'item3': {
            'one': 1,
            'two': 2
        }
    }
}

RedisJSON would probably be the right choice because you have to flatten any object before storing it as a hash. For the sake of this question, assume I don't need to do that since I already know about it.

As you already mentioned, flat vs nested objects is one major reason for using RedisJSON. Another reason is if you care about JSON-specific types such as numbers vs strings, since Redis hash values are always strings.

Finally, if you need to query objects on specific criteria, RedisJSON supports JSONPath for that which can be handy.

As for performance: While RedisJSON is quite efficient, it does have some overhead as compared to plain Redis hashes due to the need to store the JSON keys and serializing/deserializing.

In summary: If your data is structured or nested and has a specific schema, you can make good use of RedisJSON. If it's just plain key/value pairs and you don't care about the types of the values, plain Redis hashes are fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM