Redis Cheat Sheet

Comprehensive Redis commands for redis-cli, key lifecycle, strings, expiration, and core operational workflows.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Connecting and basic redis-cli use
Connect to local Redis
redis-cli

# Start an interactive session against localhost:6379.

Connect to a remote host
redis-cli -h redis.example.com -p 6379

# Connect to a remote Redis server.

Connect with password
redis-cli -h redis.example.com -p 6379 -a 'your-password'

# Authenticate during connect.

Connect using a Redis URL
redis-cli -u redis://:password@redis.example.com:6379/0

# Use a single connection string, including DB number.

Connect over TLS
redis-cli -h redis.example.com -p 6380 --tls --cacert ca.pem

# Connect securely to a TLS-enabled Redis endpoint.

Ping the server
PING

# Basic liveness check; returns PONG when reachable.

Authenticate after connecting
AUTH your-password

# Authenticate the current connection.

Select logical database
SELECT 1

# Switch to a different logical database index.

Echo a string
ECHO 'hello redis'

# Round-trip a string to test command execution.

Negotiate protocol
HELLO 3

# Switch to RESP3 and get connection metadata.

List clients
CLIENT LIST

# Inspect connected clients.

Show server info
INFO

# Return server statistics and configuration info.

Show memory info
INFO memory

# Inspect memory usage and allocator metrics.

Show replication info
INFO replication

# Check role, replicas, and sync state.

Count keys in current DB
DBSIZE

# Return the number of keys in the selected database.

Get memory statistics
MEMORY STATS

# Inspect memory allocator and overhead statistics.

Inspect command docs
COMMAND DOCS GET SET HGETALL

# Ask Redis for built-in command documentation.

## Keys and expiration
Check if key exists
EXISTS user:1

# Return how many specified keys exist.

Delete keys
DEL cache:page:1 cache:page:2

# Delete one or more keys.

Inspect key type
TYPE session:123

# Return the Redis type stored at a key.

List keys by pattern
KEYS user:*

# Find matching keys. Use only on small datasets.

Iterate keys incrementally
SCAN 0 MATCH user:* COUNT 100

# Cursor-based key iteration for production-safe scanning.

Return a random key
RANDOMKEY

# Pick an arbitrary key from the current DB.

Rename a key
RENAME cache:old cache:new

# Rename a key, overwriting destination if it exists.

Rename only if destination absent
RENAMENX cache:old cache:new

# Rename without overwriting an existing destination key.

Set expiration in seconds
EXPIRE session:123 3600

# Set a TTL in seconds.

Set expiration in milliseconds
PEXPIRE job:1 5000

# Set a TTL in milliseconds.

Expire at a Unix timestamp
EXPIREAT report:1 1767225600

# Expire a key at a specific second timestamp.

Get remaining TTL in seconds
TTL session:123

# Check remaining time to live.

Get remaining TTL in milliseconds
PTTL job:1

# Check remaining time to live in milliseconds.

Remove expiration
PERSIST session:123

# Clear the TTL and keep the key permanently.

Update LRU/LFU idle time
TOUCH user:1 user:2

# Mark keys as accessed without reading values.

Serialize a key
DUMP user:1

# Return serialized value in RDB format.

Restore serialized key
RESTORE user:1-copy 0 '\x00\x01...'

# Create a key from serialized payload.

Copy a key
COPY user:1 user:1:backup

# Duplicate a key inside the current or another DB.

Move a key to another logical DB
MOVE user:1 2

# Move a key to another database index.

## Strings
Set a string value
SET user:1:name 'Alice'

# Write a string value.

Set value with TTL
SET session:token 'abc123' EX 3600

# Store a string and set expiry atomically.

Set only if key does not exist
SET lock:job 'token' NX EX 30

# Common lock-style pattern using NX and EX.

Get a string value
GET user:1:name

# Read a string value.

Get and delete a key
GETDEL temp:token

# Atomically fetch and delete a key.

Get a value and update expiration
GETEX session:token EX 1800

# Read and extend or modify TTL atomically.

Get multiple values
MGET user:1:name user:2:name user:3:name

# Fetch many strings in one round trip.

Set multiple values
MSET site:name 'CheatSheet' site:env 'prod'

# Write many string values at once.

Set many keys only if all are absent
MSETNX a 1 b 2

# All-or-nothing conditional multi-key set.

Append to a string
APPEND log:events '
new event'

# Append bytes to an existing string.

Get string length
STRLEN user:1:name

# Return the byte length of a string.

Increment integer
INCR page:view:123

# Atomically increment a counter.

Increment by a specific value
INCRBY page:view:123 10

# Increase an integer counter by N.

Decrement integer
DECR stock:item:1

# Atomically decrement a counter.

Increment floating-point value
INCRBYFLOAT account:balance 19.95

# Increase a floating-point number.

Overwrite a range within a string
SETRANGE blob 5 'XYZ'

# Replace bytes starting at an offset.

Get a substring
GETRANGE blob 0 9

# Read a portion of a string by byte offsets.

Set one bit
SETBIT feature:flags 7 1

# Flip a single bit within a string.

Read one bit
GETBIT feature:flags 7

# Read a bit at a specific offset.

Count set bits
BITCOUNT feature:flags

# Count the number of bits set to 1.

Recommended next

No recommendations yet.