简体   繁体   English

Redis:何时使用哈希与 RedisJSON?

[英]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 .我可以在 Redis 中将资源存储为hashRedisJSON 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. 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.在队列中,对象的字段(例如user1_statususer2_status )将被更新几次。

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. 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?哪种 Redis 数据类型(哈希或 RedisJSON)更适合我的任务? 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. RedisJSON 可能是正确的选择,因为您必须先展平任何 object,然后才能将其存储为 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.正如您已经提到的,平面对象与嵌套对象是使用 RedisJSON 的主要原因之一。 Another reason is if you care about JSON-specific types such as numbers vs strings, since Redis hash values are always strings.另一个原因是如果您关心 JSON 特定的类型,例如数字与字符串,因为 Redis hash 值始终是字符串。

Finally, if you need to query objects on specific criteria, RedisJSON supports JSONPath for that which can be handy.最后,如果您需要根据特定条件查询对象,RedisJSON 支持 JSONPath,这很方便。

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.至于性能:虽然 RedisJSON 非常高效,但与普通的 Redis 哈希相比,它确实有一些开销,因为需要存储 JSON 密钥和序列化/反序列化。

In summary: If your data is structured or nested and has a specific schema, you can make good use of RedisJSON.总结:如果你的数据是结构化的或嵌套的并且有特定的模式,你可以很好地利用 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.如果它只是简单的键/值对并且您不关心值的类型,那么简单的 Redis 哈希就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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