Installing Redis & redis-cli

Redis
Chapter 2 ยท Installing Redis & redis-cli

๐Ÿ› ๏ธ Installing Redis & redis-cli

Chapter 1's 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:

# Docker โ€” fastest path, no system install needed docker run --name redis-dev -p 6379:6379 -d redis # Native install โ€” Debian/Ubuntu sudo apt install redis-server # Native install โ€” macOS (Homebrew) brew install redis

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.

redis-cli 127.0.0.1:6379> PING PONG

PING returning PONG is the simplest possible confirmation the server is up and reachable.

redis-cli Basics

127.0.0.1:6379> SET greeting "hello" OK 127.0.0.1:6379> GET greeting "hello" 127.0.0.1:6379> EXISTS greeting (integer) 1 127.0.0.1:6379> TYPE greeting string 127.0.0.1:6379> DEL greeting (integer) 1
CommandPurpose
PINGConfirms the server is reachable
SET / GETStore and retrieve a value by key
EXISTSChecks whether a key exists (returns 1 or 0)
TYPEReports which data structure a key holds (Chapter 3)
DELDeletes a key
FLUSHALLDeletes 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.

import { createClient } from 'redis'; const client = createClient(); await client.connect(); await client.set('greeting', 'hello'); const value = await client.get('greeting'); console.log(value); // "hello"

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.

โ†’ Solution

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.

โ†’ Solution

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.

โ†’ Solution

โš ๏ธ Gotcha: Never Run KEYS * Against a Production Instance

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.