🐳 1. Launch and Management via Docker
Using Docker is the cleanest way to launch a Redis instance in development or production.
Basic Launch (Ephemeral)
Ideal for quick tests or temporary caching. Data is lost when the container stops.
Bash
docker run --name my-redis -p 6379:6379 -d redis
Launch with Data Persistence
Uses appendonly mode to save data to your host disk and survive restarts.
Bash
docker run --name my-persistent-redis \
-p 6379:6379 \
-v /path/to/local/folder:/data \
-d redis redis-server --appendonly yes
Launch with Password (Secured)
Bash
docker run --name my-secure-redis -p 6379:6379 -d redis redis-server --requirepass "MySuperStrongPassword"
Connect to the Redis CLI (from the container)
Once your container is running, use this command to open the Redis prompt (redis-cli) and type the commands below:
Bash
docker exec -it my-redis redis-cli
# If you set a password:
docker exec -it my-redis redis-cli -a "MySuperStrongPassword"
🔑 2. Basic Commands (Keys & Strings)
Strings are the simplest data type in Redis (ideal for page or session caching).
| Command | Usage Example | Description |
|---|---|---|
| SET | SET user:1 "Eric" | Stores the value “Eric” in the key “user:1”. |
| GET | GET user:1 | Retrieves the value of the key “user:1”. |
| DEL | DEL user:1 | Deletes the key and its value. |
| EXISTS | EXISTS user:1 | Returns 1 if the key exists, 0 otherwise. |
| EXPIRE | EXPIRE session:99 3600 | Sets a time-to-live of 3600 seconds (1h) for the key. |
| TTL | TTL session:99 | Shows the remaining time (in seconds) before expiration. (-2 = no longer exists, -1 = never expires). |
| INCR / DECR | INCR views:article:42 | Increments (or decrements) an integer by 1. Highly performant for counters. |
📦 3. Advanced Data Structures
Redis excels thanks to its complex data structures, which can be manipulated directly in memory.
Lists
Arrays of strings, sorted by insertion order. Perfect for queues or news feeds.
| Command | Usage Example | Description |
|---|---|---|
| LPUSH / RPUSH | LPUSH tasks "email_welcome" | Adds a value to the left (start) or right (end) of the list. |
| LPOP / RPOP | LPOP tasks | Removes and returns the first (or last) element of the list. |
| LRANGE | LRANGE tasks 0 -1 | Shows all elements in the list (from index 0 to the last index -1). |
| LLEN | LLEN tasks | Returns the size (number of elements) of the list. |
Hashes
Ideal for storing structured objects (like a database row or a user profile).
| Command | Usage Example | Description |
|---|---|---|
| HSET | HSET user:2 name "Doe" age 35 | Assigns fields and values to the hash “user:2”. |
| HGET | HGET user:2 name | Retrieves the value of the “name” field for this hash. |
| HGETALL | HGETALL user:2 | Retrieves all fields and values of this hash. |
| HDEL | HDEL user:2 age | Deletes a specific field in the hash. |
Sets
Collections of unique, unordered elements. Perfect for managing tags, banned IPs, or checking membership.
| Command | Usage Example | Description |
|---|---|---|
| SADD | SADD banned_ips "192.168.1.1" | Adds an element to the set (if it already exists, it is ignored). |
| SMEMBERS | SMEMBERS banned_ips | Lists all elements in the set. |
| SISMEMBER | SISMEMBER banned_ips "10.0.0.1" | Returns 1 if the element is part of the set, 0 otherwise. |
⚙️ 4. Administration and Utilities
Useful commands to monitor and clean your Redis instance.
| Command | Description |
|---|---|
| PING | Connection test. Redis replies PONG if the server is online. |
| INFO | Shows detailed server statistics (memory used, clients, version…). |
| KEYS * | Lists all existing keys. ⚠️ Warning: Never use in production on a large database, it blocks the server (use SCAN instead). |
| FLUSHDB | Deletes all keys from the currently selected database. |
| FLUSHALL | Deletes absolutely all keys from all databases in the instance. |
| MONITOR | Shows all commands received by the server in real time. Handy for debugging in development. |
Sur Tellaw.org :