tags:

views:

57

answers:

2

A basic question. Maybe I'm just doing this wrong.

In my Redis DB I have a number of "prefix:" hashes.

Sometimes I want to purge them all atomically.

How to do this without using some distributed locking mechanism?

A: 

Execute in bash:

redis-cli "KEYS prefix:*" | xargs redis-cli DEL

UPDATE

Ok, i understood. What about this way: store current additional incremental prefix and add it to all your keys. For example:

You have values like this:

prefix_prefix_actuall = 2
prefix:2:1 = 4
prefix:2:2 = 10

When you need to purge data, you change prefix_actuall first (for example set prefix_prefix_actuall = 3), so your application will write new data to keys prefix:3:1 and prefix:3:2. Then you can safely take old values from prefix:2:1 and prefix:2:2 and purge old keys.

Casey
Sorry, but this is not atomic deletion. Someone may add new keys between KEYS and DEL. I do not want to delete those.
Alexander Gladysh
Keys, that will be created after KEYS command will not be deleted.
Casey
OK, you're right, sorry, wrong reason (5 AM here). The correct one: If the value changes between KEYS and DEL, I do not want to delete it. Furthermore, if value is changed between KEYS and DEL, I want the change to be of... "insert" kind, not "update" (if you get what I mean) -- important for hsets for example.
Alexander Gladysh
Posted some new idea
Casey
IMHO, it is better to post a new answer in such cases -- so old answer's "karma" would not harm new one.
Alexander Gladysh
Anyway, a good solution, thanks. But it requires a single read for each write I do -- a bit of overhead. Is it possible to do what I want without that overhead?
Alexander Gladysh
Thank you, i will create new answer next time. About your question - i think no. GET is extremely cheap operation in Redis, it will not be a bottleneck.
Casey
+1  A: 

I think what might help you is the MULTI/EXEC/DISCARD. While not 100% equivalent of transactions, you should be able to isolate the deletes from other updates.

alexpopescu
But I can't figure out how to use them here. DEL is atomic by itself (or so I think). And I can't get values from KEYS until I do EXEC, so I can't use KEYS and DEL in the same MULTI.
Alexander Gladysh