3

Currently we're using Redis in our application for storing different kinds of data, using prefixes to split each group. We have many "data groups" and they will grow in production environment, both as quantity and size.

We have a requirement that implies the deletion of all keys for a specific kind of data (all keys with a given prefix), and of course we don't want to use the KEYS command (and we will not use multiple DBs because it's a deprecated practice in Redis).

Furthermore, we are planning to use Redis Cluster in production environments and we have to cover this requirement in the same way.

There is a standard way to delete all keys for a given hashtag? (see docs here)

Note: we are not going to split data in different Redis instances, since it could lead to a messy system architecture (we should manage failover for each instance)

pcan
  • 893
  • 11
  • 24

2 Answers2

1

You should consider maintaining a tagging mechanism, for example associating a set of keys to each tag, so you can then invalidate them all at once.

Every time you write a value to a key, you should also insert its key to the corresponding tag set.

> SET {user}:123 john
> SADD :tags:{user} {user}:123

> SET {user}:678 mary
> SADD :tags:{user} {user}:678

And then to invalidate, use SMEMBERS to obtain the keys to invalidate.

> SMEMBERS :tags:{user}
1) "{user}:678"
2) "{user}:123"

> DEL {user}:678 {user}:123
thepirat000
  • 12,362
  • 4
  • 46
  • 72
  • Fine. This seems closer to my needs, but I have another question: will this work also in non-clustered environment? The application should **not** be system-architecture-aware. Thank you for your support. – pcan Dec 18 '15 at 10:46
  • EDIT: ok, I read the SMEMBERS docs, and your solution is very close to the one implemented in Spring Caching for Redis. We are going to adopt this one, since it is not related to any particular system arch choice. – pcan Dec 18 '15 at 10:53
  • This is cluster-compatible as long as you use the {hashtag} to group, since the values and the tag set are forced to be in the same slot. And of course it will work in a non-clustered environment. – thepirat000 Dec 18 '15 at 17:03
0

Use this answer from question How to atomically delete keys matching a pattern using Redis. This is bash script to delete keys by pattern. Execute them for each Redis cluster master node.

Community
  • 1
  • 1
Nick Bondarenko
  • 6,211
  • 4
  • 35
  • 56
  • So as far as we know there is no official supported way to do this with an embedded command? – pcan Dec 17 '15 at 11:29