Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Using redis, there are a number of commands to retrieve 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 items with a single command.

All of the examples I've seen have shown just adding a single item at a time to a list (through RPUSH or LPUSH) or a set (through SADD/ZADD).

The more specific problem that I want to solve is to create lists and sorted sets containing database ids, these lists are unique to each user and contain a few hundred to a few thousand ids.

They are normally gathered from a database query, massaged a little bit in memory and then stored in redis for either paginating through (lists) or doing set based operations on to retrieve subsets (sets and sorted sets).

Currently, I'm iterating through the list and calling the appropriate add method for each element. This has the drawbacks of making multiple requests over the wire and repeating the key every 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'm thinking that using a transaction with MULTI and EXEC could help to turn it into a single request, but that wouldn't help with the repeated key.

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'm missing that would let me add elements to lists/sets in a single command, or without repeating the key every time?

I'm also using the jedis client library if that matters (or if there's another library that I can use from the JVM that'd be better).

like image 368
Ted Naleid Avatar asked Oct 13 '22 18:10

Ted Naleid


1 Answers

Setting up a transaction won't help here. The purpose of transactions is to ensure the commands get executed at the same time so you never have half a list on the server. They still get sent one at a time.

Are the multiple commands really causing performance issues? 1000 items isn't actually that many, and as long as you aren't doing something like opening a new connection for each item there shouldn't be any latency issues.

like image 96
Tom Clarkson Avatar answered Nov 15 '22 14:11

Tom Clarkson