191
votes

Is there a way to print the number of keys in Redis?

I am aware of

keys *

But that seems slightly heavy weight. - Given that Redis is a key value store maybe this is the only way to do it. But I would still like to see something along the lines of

count keys *
10
There's a pull request for COUNT, it got denied though. github.com/antirez/redis/pull/32 antirez also commented on KEYSAlex
I wondered if they hadn't supported it as it would be O(n) - guess this confirms it.andy boot

10 Answers

231
votes

You can issue the INFO command, which returns information and statistics about the server. See here for an example output.

As mentioned in the comments by mVChr, you can use info keyspace directly on the redis-cli.

redis> INFO
# Server
redis_version:6.0.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:b63575307aaffe0a
redis_mode:standalone
os:Linux 5.4.0-1017-aws x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:9.3.0
process_id:2854672
run_id:90a5246f10e0aeb6b02cc2765b485d841ffc924e
tcp_port:6379
uptime_in_seconds:2593097
uptime_in_days:30
hz:10
configured_hz:10
lru_clock:4030200
executable:/usr/local/bin/redis-server
195
votes

DBSIZE returns the number of keys and it's easier to parse.

Downside: if a key has expired it may still count.

http://redis.io/commands/dbsize

57
votes

The DBSIZE command returns the number of keys

> DBSIZE
48
votes

WARNING: Do not run this on a production machine.

On a Linux box:

redis-cli KEYS "*" | wc -l

Note: As mentioned in comments below, this is an O(N) operation, so on a large DB with many keys you should not use this. For smaller deployments, it should be fine.

18
votes

Since Redis 2.6, lua is supported, you can get number of wildcard keys like this

eval "return #redis.call('keys', 'prefix-*')" 0

see eval command

5
votes

dbsize() returns the total number of keys.

You can quickly estimate the number of keys matching a given pattern by sampling keys at random, then checking what fraction of them matches the pattern.

Example in python; counting all keys starting with prefix_:

import redis
r = redis.StrictRedis(host = 'localhost', port=6379)
iter=1000
print 'Approximately', r.dbsize() * float(sum([r.randomkey().startswith('prefix_') for i in xrange(iter)])) / iter

Even iter=100 gives a decent estimate in my case, yet is very fast, compared to keys prefix_.

An improvement is to sample 1000 keys on every request, but keep the total count, so that after two requests you'll divide by 2000, after three requests you'll divide by 3000. Thus, if your application is interested in the total number of matching keys fairly often, then every time it will get closer and closer to the true value.

4
votes

Go to redis-cli and use below command

info keyspace

It may help someone

1
votes

After Redis 2.6, the result of INFO command are splitted by sections. In the "keyspace" section, there are "keys" and "expired keys" fields to tell how many keys are there.

0
votes
  1. Displays the db's name along with keys count:
redis-cli info keyspace
# Keyspace
db0:keys=1299555,expires=0,avg_ttl=0
db12:keys=5524396,expires=5,avg_ttl=45201
  1. Display the number of keys in the selected database:
redis-cli dbsize
(integer) 1299557
  1. Displays whole Redis's stats:
redis-cli info
-1
votes
eval "local count = redis.call('scan', 0, 'match', 'key:*:key', 'count', 10000) if count ~= 0 then return #count[2] end " 0

eval "local count = redis.call('sscan', 'key.key:all', 0, 'match', '*', 'count', 1000000) if count ~= 0 then return #count[2] end " 0