What is the most efficient way to create a medium or large list / set / zset / hash in redis?

Using redis , there are many commands for retrieving entire data structures ( LRANGE for lists, SMEMBERS for sets, ZRANGE for sorted sets, and HGETALL for hashes).

Only the hash has a method ( HMSET ) to insert multiple elements with a single command.

All the examples I saw showed just adding one item at a time to a list (via RPUSH or LPUSH ) or a set (via SADD / ZADD ).

A more specific problem that I want to solve is the creation of lists and sorted sets containing database identifiers, these lists are unique for each user and contain from several hundred to several thousand identifiers.

Usually they are collected from a database query, massaged a bit in memory, and then stored in redis for pagination (lists) or set-based operations to extract subsets (sets and sorted sets).

I am currently repeating the list and calling the appropriate add method for each item. This has the disadvantages of having to perform multiple cable requests and repeat the key each time.

redis> RPUSH employee:ids 1000 (integer) 1 redis> RPUSH employee:ids 1001 (integer) 2 redis> RPUSH employee:ids 1002 (integer) 3 redis> RPUSH employee:ids 1003 (integer) 4 redis> del employee:ids (integer) 1 

I think that using transaction with MULTI and EXEC may help turn it into a single request, but that will not help with the rekey.

 redis> MULTI OK redis> RPUSH employee:ids 1000 QUEUED redis> RPUSH employee:ids 1001 QUEUED redis> RPUSH employee:ids 1002 QUEUED redis> RPUSH employee:ids 1003 QUEUED redis> RPUSH employee:ids 1004 QUEUED redis> EXEC 1. (integer) 1 2. (integer) 2 3. (integer) 3 4. (integer) 4 5. (integer) 5 

Is there something that I am missing that will allow me to add items to lists / sets in the same command or without repeating the key every time?

I also use the jedis client library if that matters (or if there is another library that I can use from the JVM that would be better).

+4
source share
1 answer

Setting up a transaction here will not help. The purpose of transactions is to ensure that commands are executed at the same time, so you never have half the list on the server. They are still sent in turn.

Are multiple teams really the cause of performance issues? 1000 elements are actually not many, and as long as you do not do something like opening a new connection for each element, there should not be any delay problems.

+4
source

Source: https://habr.com/ru/post/1335201/


All Articles