Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete key/value from redis - phantom key not deleted

I'm using a Spring Redis repository and I'm puzzled with the delete operation and the phantom key.

When a delete is performed, the phantom key is not deleted, is it a normal behaviour ? If yes, is it possible to force a deletion of the phantom key when the original key is deleted from the code.

I was expecting that a delete removes the original key AND the associated phantom key.

I planned to use to timeToLive feature to ensure that keys not deleted by my application will expire after a while.

Annotation set on the concerned domain object

@RedisHash(value = "requestContext", timeToLive = 9000)

The delete is performed on this way:

repository.delete(id)

Thank in advance for your help.

like image 846
aure_bobo Avatar asked Sep 28 '17 11:09

aure_bobo


People also ask

How to delete a key value in Redis?

Deleting Keys Deleting a key in Redis is as simple as creating one. We use the DEL command followed by the name of the key we wish to remove. Redis will drop the key and its associated data if the specified key exists in the data store. You can also use the DEL command to remove multiple keys in a single instance.

How do I delete all keys matching a pattern in Redis?

Redis does not offer a way to bulk delete keys. You can however use redis-cli and a little bit of command line magic to bulk delete keys without blocking redis. This command will delete all keys matching users:* If you are in redis 4.0 or above, you can use the unlink command instead to delete keys in the background.

How do I delete a list in Redis?

To delete a large list in Redis: Rename the key to a unique, namespaced key so that the list appears “deleted” to other Redis clients immediately. Incrementally delete elements from the list in small batches until it is empty.

What is Spring Data Redis?

Spring Data Redis, part of the larger Spring Data family, provides easy configuration and access to Redis from Spring applications. It offers both low-level and high-level abstractions for interacting with the store, freeing the user from infrastructural concerns.


1 Answers

Phantom key is not deleted immediately when you delete key/value from Redis. Spring Data Redis manages phantom key to manage secondary indexes along with other functionalities. Spring Data Redis persists a copy of the original hash as phantom hash with a slightly longer TTL (5 minutes). That mean the :phantom TTL in Redis is 5 minutes more than the regular key TTL. When the original hash expires, Spring Data Redis loads the phantom hash to perform cleanups such as removing references from secondary indexes etc. Read more: Redis key Expirations

When the expiration is set to a positive value the according EXPIRE command is executed. Additionally to persisting the original, a phantom copy is persisted in Redis and set to expire 5 minutes after the original one. This is done to enable the Repository support to publish RedisKeyExpiredEvent holding the expired value via Springs ApplicationEventPublisher whenever a key expires even though the original values have already been gone. Expiry events will be received on all connected applications using Spring Data Redis repositories.

like image 54
Binu George Avatar answered Sep 17 '22 11:09

Binu George