Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null Elements Returned By CrudRepository.findAll() for Expired Keys [DATAREDIS-570] #1145

Open
spring-projects-issues opened this issue Nov 7, 2016 · 17 comments
Assignees
Labels
in: repository Repositories abstraction type: bug A general bug

Comments

@spring-projects-issues
Copy link

Brian Marcey opened DATAREDIS-570 and commented

The Redis documentation states that "if a key expires while the application is down the expiry event will not be processed which may lead to secondary indexes containing still references to the expired object."

I would expect this behavior and have indeed seen it while testing my code.

However, I have also noticed that if I restart my application after the above scenario occurs,
CrudRepostitory.findAll() will return a null value for each of the orphaned Ids. I didn't expect that and I can work around it, but it would be nice if SDR could detect this condition and not even include them in the returned Iterable. Note that CrudRepository.count() also returns an incorrect value under this scenario.

I can include more information if the above is not clear. Thank you!


Affects: 1.7.4 (Hopper SR4)

6 votes, 8 watchers

@spring-projects-issues
Copy link
Author

arpan2501 commented

Team any updates on this, as we are still facing this issue.

We are retrieving null values with keys expiry time set to 5 minutes but data retrieved is days old which is null

@spring-projects-issues
Copy link
Author

kubav182 commented

Can you pls fix this bug?

@spring-projects-issues
Copy link
Author

Enoobong Ibanga commented

Hi Christoph Strobl any updates on this please?

@spring-projects-issues
Copy link
Author

Eugene Zrazhevsky commented

Still being critical

@bebers22
Copy link

bebers22 commented Jan 2, 2021

We are also facing that.
Any news for the fix?

@bergerst
Copy link
Contributor

bergerst commented Sep 9, 2021

This bug not only affects findAll(), all the other query methods will also return incorrect data.

For example, findAll(example, PageRequest.ofSize(1)) will often return an empty Page with getTotal() returning a number > 0. count() will just measure the size of the Redis Set, including the expired ids.

I was able to implement a workaround to get rid of the garbage.

First, use @EnableRedisRepositories(enableKeyspaceEvents = ON_STARTUP) on a @Configuration bean so the expiration listener is active.

Then do something like this:

@Component
@RequiredArgsConstructor
public class RedisCleanupRunner implements ApplicationRunner {
  private final RedisTemplate<String, String> template;

  @Override
  public void run(ApplicationArguments args) throws Exception {
    SetOperations<String, String> setOps = template.opsForSet();
    HashOperations<String, Object, Object> hashOps = template.opsForHash();
    private final String redisKey = "yourKeySpace";

    Set<String> ids = setOps.members(redisKey);
    for (String id : ids) {
      String key = redisKey + ":" + id;

      if (!TRUE.equals(template.hasKey(key))) {
          hashOps.put(key, "foo", "bar");
          template.expire(key, Duration.ofMillis(1));
      }
    }
  }
}

The expiration of the newly created entries will trigger the MappingExpirationListener to delete all the phantoms and indexes and remove the ID from the Set.

If you have multiple RedisHash classes, run the above code in a loop for each one.

@german1608
Copy link

Hello. Is this fixed? I'm dealing with this problem :(

@Orahpajo
Copy link

Orahpajo commented May 3, 2023

I'm joining the 'would be great if this would be fixed' crowd. But so far the workaround of @bergerst is working.

@cgpoh
Copy link

cgpoh commented Aug 4, 2023

I tried @bergerst workaround but it is not working for me. The issue is very random.

@bergerst
Copy link
Contributor

bergerst commented Aug 4, 2023

@cgpoh Did you also use my workaround in #2200 ?

@cgpoh
Copy link

cgpoh commented Aug 7, 2023

@bergerst , thanks! I tried your workaround in #2200 by setting notify-keyspace-events Ex in the redis.conf file but I still get the null pointer exception when doing the query.

@Berkaybabatas
Copy link

Has a solution been found in the new versions?

@Sumned
Copy link

Sumned commented Dec 13, 2023

omg, so this bug has not been fixed in 7 years

@sureshkmit
Copy link

Please fix this issue.

@gbiz123
Copy link

gbiz123 commented Aug 1, 2024

Definitely a weird bug but glad to hear i am not alone. I was perplexed when a customer did not receive credits after paying for my service. On checking the logs, i found that findAll() had an unexpected null value in it where the user's data hash should have been,

@abilash-sethu
Copy link

abilash-sethu commented Sep 10, 2024

+1 Please do something , or suggest a proper workaround, Thanks

@cdelannoy-leroymerlin
Copy link

Is there any news about this ? The workaround provided by @bergerst might be working, it should not be required to have such code in production for every project using Spring Data Redis...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: repository Repositories abstraction type: bug A general bug
Projects
None yet
Development

No branches or pull requests