iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Install / redis-cli

Installing Redis locally: Docker, native, or managed. Plus a tour of redis-cli and the obvious gotchas.

Redis — install

EXAMPLE
# ===== Option 1: Docker (recommended for dev) =====
docker run -d --name redis -p 6379:6379 \
  -v redis-data:/data \
  redis:7 redis-server --save 60 1 --loglevel warning

# Connect:
redis-cli

# ===== Option 2: native =====
# macOS:
brew install redis
brew services start redis

# Ubuntu / Debian:
sudo apt update && sudo apt install -y redis-server
sudo systemctl enable --now redis-server

# Windows: official Redis Windows builds are deprecated.
# Use WSL or Docker on Windows.

# ===== Option 3: managed =====
# Redis Cloud, Upstash (serverless), AWS ElastiCache, GCP Memorystore.
# Pick when you want HA + backups without ops.

# ===== Verify =====
redis-cli ping
# PONG

redis-cli INFO server | head

# ===== Hello, Redis =====
redis-cli
SET user:1 'Alex'
GET user:1

HSET user:1 name 'Alex' email 'a@x.io'
HGETALL user:1

LPUSH queue 'job1' 'job2'
RPOP queue

SADD tags 'vip' 'beta'
SMEMBERS tags

# Sorted set (leaderboards):
ZADD scores 1500 'alice' 1700 'bob'
ZRANGE scores 0 -1 WITHSCORES

# TTL:
SET session:abc 'user-1' EX 1800
TTL session:abc

# ===== Persistence =====
# RDB snapshots: redis-server --save 60 1   (save every 60s if 1+ keys changed)
# AOF append log: redis-server --appendonly yes
# Pick one or both based on durability needs.

# ===== Drivers =====
# Node:    npm i redis        (official) or ioredis
# Python:  pip install redis
# Go:      github.com/redis/go-redis
# Java:    redis.clients:jedis or lettuce
# Ruby:    gem install redis

# ===== Client tools =====
# redis-cli           canonical CLI
# RedisInsight        free GUI from Redis Inc
# medis (macOS)       GUI

# ===== Patterns to internalise =====
# - Docker for ephemeral dev; managed for prod
# - Always set a TTL on cache + session keys
# - SCAN, never KEYS, against production
# - Choose persistence intentionally (RDB / AOF / none)

# ===== Pitfalls =====
# - Exposing 6379 on a public IP without protected-mode + auth
# - No TTL on cache keys -> memory grows until OOM
# - KEYS * in production -> server stalls
# - 4MB+ values -> slow eviction, memory pressure

Why it matters

Docker for dev, managed for prod, redis-cli for daily work. Set TTLs, prefer SCAN over KEYS, and pick a persistence mode that matches your durability needs. Most Redis incidents come from forgetting one of these three rules.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Ubuntu
sudo apt install redis-server
# macOS
brew install redis
# Connect
redis-cli
PING   # → PONG
Try it Yourself »

Discussion

Loading…