Home/Engineer/Databases/Redis Fundamentals in Practi…
DatabasesBeginnerUbuntuDebianCentOSRedisNoSQL

Redis Fundamentals in Practice — Caching, Sessions, and Message Queues

A practitioner-focused guide covering Redis from installation through String, Hash, List, and Sorted Set data structures, plus caching strategies and TTL.

Contents
  1. Installation
  2. Basic Commands
  3. Hash — Storing Objects
  4. List — Queues and Stacks
  5. Set and Sorted Set
  6. Cache-aside Pattern
  7. Monitoring

Installation

Bash
# Ubuntu
apt install redis-server

#
redis-cli ping  # PONG

We 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 100

Hash — 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 / 0

List — Queues and Stacks

Bash
# FIFO
RPUSH tasks "job1" "job2" "job3"
LPOP tasks # "job1"
LLEN tasks #

#
BLPOP tasks 30 # 30

Set 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 user

Monitoring

Bash
# 
redis-cli info memory
redis-cli info stats

# 
redis-cli --bigkeys

# 
redis-cli slowlog get 10
#Redis#NoSQL
Editorial 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.