Skip to content

Redis

🔴 Redis CLI Cheatsheet

🔑 Key Management

Basic operations for handling keys, regardless of their data type.

Command Description
SET key value Set the string value of a key
GET key Get the value of a key
DEL key Delete a key
EXISTS key Check if a key exists
EXPIRE key 60 Set a timeout on a key (in seconds)
TTL key Get the remaining time to live for a key
KEYS * List all keys (Warning: Slow on large datasets)
FLUSHALL Delete all keys from all databases

📝 Data Structures

Used for queues or stacks. * LPUSH list value – Add item to the start. * RPUSH list value – Add item to the end. * LPOP list – Remove and get the first item. * LRANGE list 0 -1 – Get all items in the list.

Perfect for storing user profiles or objects.
* `HSET user:1 name "Alice"` – Set a field in a hash.
* `HGET user:1 name` – Get a specific field.
* `HGETALL user:1` – Get all fields and values in the hash.

Unordered collections of unique elements. * SADD myset "value" – Add a member to a set. * SMEMBERS myset – List all members. * SISMEMBER myset "value" – Check if a value exists in the set.

Sets ordered by a "score." Great for leaderboards. - ZADD players 100 "user1" – Add member with a score. - ZRANGE players 0 -1 WITHSCORES – Get all members by rank.


📡 Pub/Sub & Persistence

Redis isn't just for storage; it's a powerful message broker.

Command Description
PUBLISH channel message Send a message to a channel
SUBSCRIBE channel Listen for messages on a channel
SAVE Synchronously save the dataset to disk
BGSAVE Asynchronously save the dataset to disk
INFO Get server statistics and information

🛠️ Connection & Troubleshooting

# Connect to a local instance
redis-cli

# Connect to a remote host
redis-cli -h <host> -p <port> -a <password>

# Test if the server is alive
127.0.0.1:6379> PING
# Response: PONG

import redis
r = redis.Redis(host='localhost', port=6379)
r.set('foo', 'bar')
const redis = require('redis');
const client = redis.createClient();
await client.set('foo', 'bar');