Commands on selective data (keys matching a pattern)

760 views
Skip to first unread message

Catalin Ghinea

unread,
Jan 5, 2017, 11:31:10 AM1/5/17
to lettuce-redis-client-users
Hello!

I am working on a technical solution which will use Redis Cluster as cache and I am using Lettuce as the Java client. I implemented straightforward all the basic functionalities (get, put etc) corresponding to specific needs of the product.
I am searching for an optimal solution for the problem of eviction/deletion of selective data (keys matching a pattern) from Redis Cluster. The reasoning behind it is the need for eviction when certain product properties are changing so I should be able to trigger periodically controlled evictions for different parts of data.

What I investigated so far:

1. Following the suggestions of Mark (Iteration) I implemented a Scan method which iterates over the whole cluster and finds the matching keys which are deleted after. Tests: Redis cluster on Windows (3 master, 3 slaves), 80000 keys selected by specific pattern and deleted in 8 - 9 seconds. No tests over this limit due to technical limitations. It is unclear the speed for large quantities of data
2. Because of Redis cluster restrictions on different commands (like Eval of Lua scripts) I tested tags for keys. By using the tagging process you could force all related keys to be resolved in the same hash slot (keys with specific tag would live in a slot but unfortunately others can live there as well). The tests were much faster in speed than for the first solution in similar conditions.
Several downsides: the cluster would be unbalanced and failure of a node containing a lot of populated slots would be quite severe; several tags can fit in the same hash slot and we would have abnormal traffic on just some parts of the cluster
3. We can expire the required keys by setting corresponding TTLs but this would require an iteration on keys for a pattern and we are back to solution 1
4. Add keys that should be deleted together to a hash/list/set and delete them at once, as one object.
5. Flush all the content and regenerate all at once (not really a solution but still)

I would appreciate any feedback about the solutions and how to improve the speed.

Regards,
Catalin

PS. Very interesting article about RC and its current upgrade limitations: Testable

Mark Paluch

unread,
Jan 5, 2017, 3:54:30 PM1/5/17
to lettuce-redis-client-users
Hi Catalin, 

I think you question would be of interest for the community in general, so posting it to StackOverflow/discussing it on IRC/Gitter would be the way to go.

Since you're working with multiple servers, you could scan over the nodes in parallel which might speed up the whole process. 

Data sharding imposes several constraints but it also gives you computation distribution that means you can trigger work on multiple nodes in parallel.

Cheers, 
Mark

mykev...@gmail.com

unread,
Feb 8, 2017, 8:11:03 PM2/8/17
to lettuce-redis-client-users
Hello,

I also need to delete keys with certain pattern from Redis cluster.  I thought of using smember but the size of keys is over 20GB in some case, so getting 20GB of keys to the client using smembers() would not work.  I like to issue a command that runs on the server side.  Can we issue a command like EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 prefix:* or redis-cli KEYS "prefix:*" | xargs redis-cli DEL using lettuce as described in this post?

Or, do you think we can do as in the following code?  Here I am passing the key pattern and the streaming channel will delete the key when onKey is called for each key.  I am assuming onKey() is called as many time as the number of keys.  Though I sill like to find a command that runs in the server side as I have hundreds of millions of keys and I am not comfortable sending a command that many times between client and server.
        keyCommands.keys(new KeyStreamingChannel<K>() {
            public void onKey(K key) {
                keyCommands.del(key);
            }
        }, keyPattern);

By the way, if you already have a posting on this in StackOverflow, please let me know the link.

Thanks,
Kevin

Mark Paluch

unread,
Feb 9, 2017, 2:13:29 AM2/9/17
to lettuce-redis-client-users, mykev...@gmail.com
That's a general Redis question that fits better to StackOverflow. Please also don't cross-post to different forums/platforms.
Reply all
Reply to author
Forward
0 new messages