Redis Cheatsheet

View saved

Redis is an in-memory key/value store used for caches, sessions, counters, and lightweight queues.

Name keys with prefixes, set TTLs on disposable data, and avoid KEYS * on large production instances.

CLI & keys

redis-cli / PING

Connect and health-check the server.

redis-cli
PING

EXISTS / TYPE / DEL

Inspect and remove keys.

EXISTS cache:home
TYPE cache:home
DEL cache:home

SCAN

Iterate keys safely; prefer over KEYS in production.

SCAN 0 MATCH cache:* COUNT 20

Naming

Use prefixes such as cache:, session:, lock:.

SET session:abc123 "..." EX 3600

Strings & TTL

SET / GET

Store a string or blob value.

SET greeting "hello"
GET greeting

SET options

NX = only if missing; EX = seconds TTL.

SET lock:job1 "worker-a" NX EX 30

INCR

Atomic counters for tallies and rate limits.

INCR visits:home
INCRBY visits:home 5

EXPIRE / TTL

Attach or inspect lifetimes.

EXPIRE cache:home 60
TTL cache:home
PERSIST cache:home

Lists, hashes, sets

Lists

Queues and recent feeds with push/pop and ranges.

LPUSH jobs task1
LRANGE jobs 0 -1
BRPOP jobs 5

Hashes

Field maps under one key—good for objects.

HSET user:1 name "Ada" email "[email protected]"
HGETALL user:1

Sets

Unique membership and set algebra.

SADD tags redis cache
SISMEMBER tags redis
SINTER a b

Sorted sets peek

Unique members with scores—leaderboards.

ZADD scores 100 alice 95 bob
ZREVRANGE scores 0 2 WITHSCORES

Use cases & clients

Cache-aside

GET cache → on miss load DB → SET with TTL.

SET cache:product:42 "{\"name\":\"Tee\"}" EX 300
DEL cache:product:42

Sessions

Store session payloads with idle expiry.

SET session:S3cr3t "{\"userId\":7}" EX 86400

Node client peek

Official redis package for Node.js.

import { createClient } from "redis";
const c = createClient({ url: process.env.REDIS_URL });
await c.connect();

Python client peek

redis-py from PyPI.

import redis, os
r = redis.from_url(os.environ["REDIS_URL"])
r.set("k", "v", ex=60)

Comments

One comment per signed-in account. Comments are saved with this page’s URL.