I have a Flask server that will fetch keys from Redis. How would I maintain a list of already deleted keys? Every time I delete a key, it needs to be added to this list within Redis. Every time I try to check for a key in the cache, if it cannot be found I want to check if it is already deleted. This would allow me to distinguish in my API between requests for unknown keys and requests for deleted keys.
I am using the standard redis python library.
2
Answers
Probably the easiest way is to have 2 redis databases (1 and 2) let’s say, where in 1 you keep active keys and in 2 you keep deleted keys. The lookup will be very fast and you can set an expiry period on the keys from the deleted database so it does not grow out of control.
See this documentation for switching between databases in Redis: https://redis.io/commands/select
Instead of deleting the key, you can set the to-be-deleted key with a special value, say, empty string.
When you get key-value pair from Redis, you can check if the value equals to the special value. If it does, then the key has been deleted. Otherwise, serve the client with the value.
Also, as @Mihai mentioned, you might also need to set a TTL with those deleted keys to avoid Out-Of-Memory problem.