Redis cli - KEYS * not showing all keys
Asked Answered
M

1

5

I am connecting to an AWS redis cluster using following command

redis-cli -c -h host.amazonaws.com -p 6379

I pushed two key "X1" and "X2" into redis cache from a springboot application (API methods are not annotated with @Cacheable) and now when I run KEYS * from cli terminal it would list either "X1" or "X2" but not both. GET for both keys works fine though.

info keyspace return following;

Keyspace

db0:keys=11,expires=1,avg_ttl=1975400

What am I missing here?

Maxwell answered 31/8, 2022 at 13:46 Comment(0)
C
8

You probably have cluster mode enabled. In cluster mode, the data you store is partitioned by key. One of the advantages of this is that you can now have a larger dataset than would reasonably fit on one machine (hundreds of terabytes, if you want) since every shard has some fraction of the entire data set.

A downside is that multi-key commands no long work like you would expect if the keys end up in different hash slots. The KEYS command is such a multi-key command.

To make a long story short:

  • KEYS is apparently giving you only the keys on the cluster node you're hitting. It would perhaps have been nicer to give you an error, instead, but it doesn't.
  • GET is unaffected: redis-cli, with the -c flag, knows how to find the right cluster node (perhaps after hitting the wrong one and being told the key has MOVED).

If you ask every individual primary node in your cluster for KEYS *, and add up all the results, you should get all the keys. This question has some examples of using the redis-cli to do this.

Crine answered 31/8, 2022 at 14:27 Comment(1)
this answer was helpful.Epiphenomenon

© 2022 - 2024 — McMap. All rights reserved.