redis-cli: The Redis Client
Get fluent in redis-cli — connect, set and read keys, give them a lifetime with TTL, work with lists, hashes, sets and sorted sets, run command files, and script the client from the shell.
Redis is a key-value store you keep in memory, and redis-cli is the tool that ships with it. It is always there on the server you just SSH'd into, and learning it well is the fastest way to understand what Redis actually is. We start from an empty prompt and build up real fluency one small thing at a time: setting and reading keys, counters that increment atomically, keys that expire on their own, and the data structures — lists, hashes, sets and sorted sets — that make Redis more than a dictionary. Then we step out to the shell to run command files and wire the client into scripts. Writing a command and running it are taught as separate things, so each idea lands on its own.
What you'll build
- Connect to Redis and read the interactive replies
- Set, read and delete keys, and count with atomic increments
- Give keys a lifetime with TTL and expiry
- Work with lists, hashes, sets and sorted sets
- Find keys safely without blocking the server
- Run command files and script redis-cli from the shell
Contents
- Open a session
- Check the connection
- Set and read a key
- Ask whether a key exists
- Read a missing key
- Delete a key
- Switch to another database
- Count keys in a database
- Go back to database 0
- Count atomically
- Set and read many keys at once
- Give a key a lifetime
- Add expiry to an existing key
- Make a key permanent again
- Read the two special TTL replies
- Build a list
- Read a range from a list
- Store a record as a hash
- Read a whole hash
- Collect unique members in a set
- List the members of a set
- Build a leaderboard with a sorted set
- Read the ranking
- Find the type of a key
- Scan for keys the safe way
- Why not just KEYS
- Write a file of commands
- Run the file from the shell
- Run one command from the shell
- Get the bare value for scripts
- Output as CSV
- Target a database from the shell
- Repeat a command
- Ask the server about itself
- Watch every command live
- Make the client yours