How to get the number of keys in Redis
INFO command will show the information and statistics about the redis server. The result includes the total number of keys in redis.
First of all login to the redis server using redis-cli.
Then run the following command to get the complete information.
redis>INFO
# Server
redis_version:6.0.5
redis_git_sha1:0
redis_git_dirty:0
redis_build_id:0
redis_mode:standalone
os:Amazon ElastiCache
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:0.0.0
process_id:1
run_id:d08e21995ecf119a59cf1a898a7b90fd3fd413a4
tcp_port:6379
uptime_in_seconds:3011158
uptime_in_days:34
hz:10
configured_hz:10
lru_clock:492978
executable:-
config_file:-
# Clients
connected_clients:1074
client_recent_max_input_buffer:4
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0
# Memory
used_memory:28184496
used_memory_human:26.88M
used_memory_rss:23384064
used_memory_rss_human:22.30M
used_memory_peak:70034360
used_memory_peak_human:66.79M
used_memory_peak_perc:40.24%
used_memory_overhead:23834596
used_memory_startup:4272192
used_memory_dataset:4349900
used_memory_dataset_perc:18.19%
allocator_allocated:28867656
allocator_active:33927168
allocator_resident:40067072
used_memory_lua:45056
used_memory_lua_human:44.00K
used_memory_scripts:1256
used_memory_scripts_human:1.23K
number_of_cached_scripts:3
maxmemory:2596012032
maxmemory_human:2.42G
maxmemory_policy:volatile-lru
allocator_frag_ratio:1.18
allocator_frag_bytes:5059512
allocator_rss_ratio:1.18
allocator_rss_bytes:6139904
rss_overhead_ratio:0.58
rss_overhead_bytes:-16683008
mem_fragmentation_ratio:0.83
mem_fragmentation_bytes:-4757920
mem_not_counted_for_evict:0
mem_replication_backlog:1048576
mem_clients_slaves:17218
mem_clients_normal:18494130
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0
# Persistence
loading:0
rdb_changes_since_last_save:3159
rdb_bgsave_in_progress:0
rdb_last_save_time:1624871773
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:512000
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0
module_fork_in_progress:0
module_fork_last_cow_size:0
# SSL
ssl_enabled:no
ssl_current_certificate_not_before_date:(null)
ssl_current_certificate_not_after_date:(null)
ssl_current_certificate_serial:(null)
# Stats
total_connections_received:36488938
total_commands_processed:86002332
instantaneous_ops_per_sec:21
total_net_input_bytes:4155870949
total_net_output_bytes:53560937801
instantaneous_input_kbps:1.27
instantaneous_output_kbps:19.00
rejected_connections:0
sync_full:2
sync_partial_ok:0
sync_partial_err:2
expired_keys:885
expired_stale_perc:0.00
expired_time_cap_reached_count:0
expire_cycle_cpu_milliseconds:78903
evicted_keys:0
keyspace_hits:13043
keyspace_misses:23787709
pubsub_channels:2
pubsub_patterns:0
latest_fork_usec:361
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
# Replication
role:master
connected_slaves:1
slave0:ip=10.9.1.222,port=6379,state=online,offset=495714806,lag=1
master_replid:d823298a4d2f31646037675fe7ba9b2f0f917248
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:495714917
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:494666342
repl_backlog_histlen:1048576
# CPU
used_cpu_sys:7023.633215
used_cpu_user:14162.387253
used_cpu_sys_children:0.000000
used_cpu_user_children:0.001985
# Modules
# Cluster
cluster_enabled:0
# Keyspace
db0:keys=11,expires=6,avg_ttl=58350048
That's all….