Redis Strings
6. Redis Strings
Section titled “6. Redis Strings”What is a String?
Section titled “What is a String?”A String in Redis is the most basic type. Despite the name, it can hold:
- Plain text:
"Hello, World!" - Integers:
42 - Floats:
3.14 - Binary data: images, serialized objects (up to 512 MB)
Analogy: A String is like a labeled jar — the label is the key, and whatever you put inside is the value.
How Strings Work
Section titled “How Strings Work”flowchart LR subgraph SET[SET key value] K1["Key: username"] --> V1["Value: 'prathamesh'"] end
subgraph GET[GET key] K2["Key: username"] --> V2["Returns: 'prathamesh'"] end
subgraph Counter[INCR key — Atomic Counter] K3["Key: page:views"] --> V3["0 → INCR → 1 → INCR → 2"] end
subgraph TTL[SET key value EX seconds] K4["Key: session:token"] --> V4["Stored with 3600s expiry Auto-deletes after 1 hour ⏰"] end
style SET fill:#7c3aed,color:#fff style GET fill:#3b82f6,color:#fff style Counter fill:#059669,color:#fff style TTL fill:#f59e0b,color:#fffCore Commands
Section titled “Core Commands”# Set a keySET username "prathamesh"
# Get a keyGET username# Output: "prathamesh"
# Set with expiry (in seconds)SET session:user123 "token_abc" EX 3600
# Set multiple keys at onceMSET name "Alice" age "30" city "Pune"
# Get multiple keys at onceMGET name age city# Output: 1) "Alice" 2) "30" 3) "Pune"
# Check if a key existsEXISTS username# Output: 1 (exists) or 0 (not found)
# Delete a keyDEL username
# Append to existing valueSET greeting "Hello"APPEND greeting ", World!"GET greeting# Output: "Hello, World!"Counters — Increment & Decrement
Section titled “Counters — Increment & Decrement”Redis Strings can store integers and perform atomic increment/decrement operations.
# Initialize a counterSET page:views:homepage 0
# Increment by 1 (atomic operation)INCR page:views:homepage# Output: (integer) 1
INCR page:views:homepage# Output: (integer) 2
# Increment by a specific amountINCRBY page:views:homepage 100# Output: (integer) 102
# DecrementDECR page:views:homepage# Output: (integer) 101
DECRBY page:views:homepage 50# Output: (integer) 51Why is INCR useful? Because
INCRis atomic. Even if 1,000 concurrent clients callINCRat the same time, each increment is applied correctly. No race conditions.
Real-World Examples
Section titled “Real-World Examples”Example 1: Page Visit Counter
# Every time a user visits the homepageINCR visits:homepage
# Get total visit countGET visits:homepageExample 2: User Session Storage
# Store session token with 30-minute expirySET session:user:42 "eyJhbGciOiJIUzI1NiJ9..." EX 1800
# Validate sessionGET session:user:42# Returns token if valid, nil if expiredExample 3: Rate Limiting
# Track API calls per minute per userSET ratelimit:user:42 0 EX 60INCR ratelimit:user:42
# If value > 100, reject the requestGET ratelimit:user:42Advantages of Redis Strings
Section titled “Advantages of Redis Strings”- Simple and fast
- Atomic counter operations
- Supports TTL natively
- Binary-safe (can store any data)
Limitations
Section titled “Limitations”- Maximum value size is 512 MB
- Not suitable for structured or relational data