Connecting & Server
# Connect to local Redis
redis-cli
# Connect to remote with auth
redis-cli -h myhost.example.com -p 6379 -a password
# Connect with TLS
redis-cli --tls --cert ./client.crt --key ./client.key
# Server info
INFO # Full server info
INFO memory # Memory usage
INFO replication # Replication status
DBSIZE # Key count in current DB
CONFIG GET maxmemory # Read config at runtime
CONFIG SET maxmemory 2gb # Set config at runtime
Core GET / SET
SET / GET
SET user:1:name "Ada Lovelace"
GET user:1:name
"Ada Lovelace"
Store and retrieve a string value by key.
SET with Options
# Set with 300s TTL
SET session:abc "data" EX 300
# Set only if key does NOT exist
SET lock:order 1 NX EX 30
EX (seconds), PX (ms), NX (if-not-exists), XX (if-exists).
MSET / MGET
MSET k1 "v1" k2 "v2"
MGET k1 k2
1) "v1"
2) "v2"
Batch get/set — atomic and faster than N round-trips.
Counters
INCR page:views
(integer) 1
INCRBY page:views 10
(integer) 11
DECR page:views
INCRBYFLOAT price 3.14
Atomic increment/decrement — no race conditions.
Key Management
EXISTS mykey # 1 if exists, 0 if not
DEL mykey # Delete (blocking)
UNLINK mykey # Delete (async, non-blocking)
TYPE mykey # Returns: string, list, set, zset, hash, stream
RENAME old new # Rename a key
KEYS user:* # Pattern match (NEVER in production!)
SCAN 0 MATCH user:* COUNT 100 # Cursor-based iteration (safe)
OBJECT ENCODING mykey # Internal encoding (listpack, hashtable, etc.)
Data Types at a Glance
| Type | Use Case | Max Size | Key Commands |
|---|---|---|---|
| String | Cache, counters, flags | 512 MB | GET SET INCR |
| Hash | Objects, user profiles | 4B fields | HGET HSET HGETALL |
| List | Queues, feeds, stacks | 4B elements | LPUSH RPOP LRANGE |
| Set | Tags, unique items | 4B members | SADD SMEMBERS SINTER |
| Sorted Set | Leaderboards, ranges | 4B members | ZADD ZRANGE ZRANK |
| Stream | Event log, messaging | Configurable | XADD XREAD XACK |