Installation
Bash
# Ubuntu
apt install redis-server
#
redis-cli ping # PONGWe recommend setting requirepass yourpassword in /etc/redis/redis.conf.
Basic Commands
Bash
redis-cli
# String
SET user:1:name "Alice"
GET user:1:name
INCR counter
INCRBY counter 10
# TTL (expiry)
SET session:abc "token" EX 3600 # 3600 after
TTL session:abc # when
PERSIST session:abc # TTL
# ( SCAN )
SCAN 0 MATCH user:* COUNT 100Hash — Storing Objects
Bash
HSET user:1 name "Alice" email "alice@example.com" age 30
HGET user:1 name
HGETALL user:1
HDEL user:1 age
HEXISTS user:1 email # 1 / 0List — Queues and Stacks
Bash
# FIFO
RPUSH tasks "job1" "job2" "job3"
LPOP tasks # "job1"
LLEN tasks #
#
BLPOP tasks 30 # 30Set and Sorted Set
Bash
# Set
SADD tags "redis" "nosql" "cache"
SMEMBERS tags
SISMEMBER tags "redis" # 1
# Sorted Set
ZADD leaderboard 1500 "alice"
ZADD leaderboard 2300 "bob"
ZRANGE leaderboard 0 -1 WITHSCORES #
ZREVRANGE leaderboard 0 2 # Top 3
ZINCRBY leaderboard 100 "alice" #Cache-aside Pattern
Python
import redis, json
r = redis.Redis(host='localhost', port=6379)
def get_user(user_id):
cache_key = f"user:{user_id}"
cached = r.get(cache_key)
if cached:
return json.loads(cached)
user = db.query(f"SELECT * FROM users WHERE id = {user_id}")
r.setex(cache_key, 3600, json.dumps(user))
return userMonitoring
Bash
#
redis-cli info memory
redis-cli info stats
#
redis-cli --bigkeys
#
redis-cli slowlog get 10Editorial note
AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us
Topic-related official docs (auto-linked · not per-claim sources)Redis documentation ↗
Questions & answers
Ask a question about this guide. We'll review and reply.