↑ ↓ select, Enter open, Esc close

Count Redis keys by pattern without blocking the server

Adjust the values, the command updates live
user@server
redis-cli -n 0 --scan --pattern '*transient*' | wc -l

Walks through the database step by step with SCAN and prints all matching keys, which wc -l then counts. The command KEYS * would return the same, but it blocks Redis for its entire runtime because Redis processes commands one after another. On a production server with many keys, that can take seconds during which every website waits for the cache.

Note: Always put the pattern in single quotes, otherwise the shell expands the asterisk. Without | wc -l, the keys themselves are shown.

Also searched as

  • count redis keys with prefix
  • how many transients are in redis
  • redis keys star alternative

Related one-liners

All in Redis and Memcached

Read first, then run.

The commands on myline.de act directly on servers, files and databases. A wrong path or placeholder can delete data irreversibly or make a server unreachable.

  • All commands are provided without warranty and are not tested on every system.
  • Understand what a command does before running it, and check every placeholder.
  • Make a backup first and, if possible, try it on a test system.
  • You run commands at your own risk. Liability for damages is excluded to the extent permitted by law.