1

Using the following model:

@RedisHash("positions")
public class Position {

    @Id
    private String id;

    @GeoIndexed
    private Point coordinates;

    @TimeToLive(unit = TimeUnit.MINUTES)
    protected int ttl;

    //...
}

I noticed that some data remains persisted after the Time To Live expires. Notice the difference between keys * command before and after the expire event:

Before

127.0.0.1:6379> keys *
1) "positions:336514e6-3e52-487a-a88b-98b110ec1c28"
2) "positions:coordinates"
3) "positions:336514e6-3e52-487a-a88b-98b110ec1c28:idx"
4) "positions"
5) "positions:336514e6-3e52-487a-a88b-98b110ec1c28:phantom"

After

127.0.0.1:6379> keys *
1) "positions:coordinates"
2) "positions:336514e6-3e52-487a-a88b-98b110ec1c28:idx"
3) "positions"
4) "positions:336514e6-3e52-487a-a88b-98b110ec1c28:phantom"

Only the positions:336514e6-3e52-487a-a88b-98b110ec1c28 item was deleted.

I also notice that, after some more time, the *:phantom item also is deleted, but not the rest. Is this a bug or it is required to configure/implement something more?

mp911de
  • 17,546
  • 2
  • 55
  • 95
y.luis.rojo
  • 1,794
  • 4
  • 22
  • 41
  • Did you use the system during your test ? Anything modified the fields ?After the expire command on redis, if any other command changes the key value, the expire command is terminated. So, please check if anything can set the keys *:idx, *:phantom or any other remained. – anL Jan 10 '18 at 20:28
  • These keys are not generated explicitly by me but by Spring Data Redis, so, do not know the internal behavior. – y.luis.rojo Jan 11 '18 at 15:22

1 Answers1

2

Your application needs to stay active. Redis Repositories use keyspace events to get notified about expiration so Spring Data Redis can cleanup index structures. Redis supports expiry on top-level keys only, it does not support expiry on list/set elements.

The :phantom key has a slightly longer expiration, that's why it expires after the original key has expired. It's used to provide the expired hash values for index cleanup and such.

mp911de
  • 17,546
  • 2
  • 55
  • 95
  • The application remains active for long time. Since Spring Data Redis is in charge of creating items in Redis I am not sure which(if any) is a list/set element. Does that mean that `positions:coordinates`, `positions:336514e6-3e52-487a-a88b-98b110ec1c28:idx` and `positions` items in the example, will remain forever?! – y.luis.rojo Jan 11 '18 at 15:46
  • Taking a closer look I think I understand better. Redis does not supports expiry on lists/sets but Spring Data Redis could remove (after receiving the expired event and using the id): a member from `positions` set using `SREM`, a member from `positions:coordinates` sorted set using `ZREM` and definitely remove `positions:id:idx` item at all. Am I right? – y.luis.rojo Jan 11 '18 at 16:47
  • Yes, that’s correct. Make sure Redis is configured to send key expiration keyspace events, see @EnableRedisRepositories annotation javadoc for more details. – mp911de Jan 11 '18 at 18:21
  • 2
    Great! That's what was missing! I set `enableKeyspaceEvents = EnableKeyspaceEvents.ON_STARTUP` on `@EnableRedisRepositories` annotation and now every item is deleted! Thanks. – y.luis.rojo Jan 11 '18 at 18:42
  • Are the `:idx` entries cleaned up together with the `:phantom`? Are you extending the standard CRUD repository to write to Redis? Which versions are you using? I am asking, because I am facing the [same issue](https://stackoverflow.com/questions/48379229/redis-expired-indexes-are-not-deleted) and I can't get it to work the same way... – NanSil Feb 02 '18 at 06:51
  • Where I can take a look about :phantom key expiration ttl? Why not put this info to spring-redis-data documentation? This is really surprise first time you see this behavior with nulls on expired values... – Dzmitry Prakapenka Feb 18 '19 at 12:16
  • See https://docs.spring.io/spring-data/redis/docs/current/reference/html/#redis.repositories.expirations. Feel free to submit a pull request to improve the documentation if you feel there's something we can do about it. – mp911de Feb 18 '19 at 12:22