← All bricks

redis-cli · Beginner

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

  1. Open a session
  2. Check the connection
  3. Set and read a key
  4. Ask whether a key exists
  5. Read a missing key
  6. Delete a key
  7. Switch to another database
  8. Count keys in a database
  9. Go back to database 0
  10. Count atomically
  11. Set and read many keys at once
  12. Give a key a lifetime
  13. Add expiry to an existing key
  14. Make a key permanent again
  15. Read the two special TTL replies
  16. Build a list
  17. Read a range from a list
  18. Store a record as a hash
  19. Read a whole hash
  20. Collect unique members in a set
  21. List the members of a set
  22. Build a leaderboard with a sorted set
  23. Read the ranking
  24. Find the type of a key
  25. Scan for keys the safe way
  26. Why not just KEYS
  27. Write a file of commands
  28. Run the file from the shell
  29. Run one command from the shell
  30. Get the bare value for scripts
  31. Output as CSV
  32. Target a database from the shell
  33. Repeat a command
  34. Ask the server about itself
  35. Watch every command live
  36. Make the client yours