Fastest way to check key exists in redis - php

11.5k views Asked by At

Is there any other faster way than EXISTS, to check if a key exists in redis or not?

My problem is, I have over 1 million records in redis and I need to do a key_exists check. This should happen within 10ms.

Any Ideas around this?

3

There are 3 answers

0
fire On BEST ANSWER

Using the EXISTS command is the fastest way, this should be extremely quick. If you feel that it's too slow its probably the latency between your server and the redis server and nothing to do with the command itself.

0
Mike Miller On

To reduce the time you need to keep indexes of your keys using some pattern logical to your application. This means instead of having to do exists on the all the keys you can do a sismember or zscore on your index set/zset. So for example you have keys related to users, messages and leaderboards etc you keep sets called keys:users , keys:messages etc. I have a library just open sourced to help manage your key names and make this stuff a bit easier https://github.com/imikemiller/Pkeys

2
Amin Shojaei On

The time complexity of the exists is O(1), so it's the fastest possible algorithm.

Your problem is from somewhere else, But you can check the real execution time of the exists using the SLOWLOG command to make sure.