Installing Redis & redis-cli
๐ ๏ธ Installing Redis & redis-cli
SET/GET preview assumed a running Redis instance. This chapter gets one actually running, connects to it with the redis-cli shell, and connects to it from real application code โ the same three-step arc mongodb1-2 followed for MongoDB.
Installing Redis
Three common paths, in order of how likely you are to reach for each:
A managed cloud Redis offering (many cloud providers offer one) is the production-equivalent path โ same protocol, no server to maintain โ but local development almost always starts with Docker or a native install.
The redis-cli Shell
redis-cli is Redis's own interactive shell โ conceptually identical to mongosh for MongoDB or the mysql client for MySQL.
PING returning PONG is the simplest possible confirmation the server is up and reachable.
redis-cli Basics
| Command | Purpose |
|---|---|
PING | Confirms the server is reachable |
SET / GET | Store and retrieve a value by key |
EXISTS | Checks whether a key exists (returns 1 or 0) |
TYPE | Reports which data structure a key holds (Chapter 3) |
DEL | Deletes a key |
FLUSHALL | Deletes every key in every database โ use with real caution |
Connecting From an Application
Real applications don't shell out to redis-cli โ they use a client library, the same pattern mongodb1-8 established for MongoDB's Node.js driver. The most common Node.js options are node-redis (the official client) and ioredis.
redis-cli vs. a Client Library
redis-cli
An interactive shell for exploring data and running one-off commands by hand โ the same role mysql/mongosh play for their engines.
Client Library (node-redis, ioredis)
What real application code actually uses โ the same underlying protocol, wrapped in an API a program calls programmatically.
๐ป Coding Challenges
Challenge 1: Confirm a Running Instance
Using redis-cli, write the command to confirm a Redis server is reachable, and explain what response confirms success.
Goal: Practice the most basic connectivity check before attempting any real commands.
Challenge 2: Set, Check, and Remove a Key
Write the sequence of redis-cli commands to: set a key session:42 to the value "active", confirm it exists, check its type, and then delete it.
Goal: Practice chaining the basic commands from this chapter's reference table in a realistic sequence.
Challenge 3: CLI or Client Library?
For each, say whether redis-cli or a client library (like node-redis) is the right tool: (a) quickly checking what's currently cached during debugging, (b) a running Express application reading a cached value on every request.
Goal: Practice applying this chapter's CLI-vs-library distinction to realistic scenarios.
Redis is fundamentally single-threaded for command execution โ one command runs to completion before the next one starts, which is part of why individual commands are so fast and predictable. KEYS * (or any broad KEYS pattern) scans every key in the database in one blocking operation โ on a database with millions of keys, this can freeze the entire server for every other client for seconds, all at once. The safe alternative, covered properly once it becomes relevant, is SCAN, which walks the keyspace incrementally without blocking. For now: reach for KEYS only in a small local development database, never against anything resembling production.
๐ฏ What's Next
The next chapter covers Redis's core data structures in depth: Strings, Lists, and Hashes โ SET/GET/INCR, LPUSH/RPUSH/LRANGE, and HSET/HGET/HGETALL.