1.Key-value storage and strings
Redis stores every value under a unique text key, like a giant dictionary that lives outside your process. r.set("user:1001:name", "Priya Shah") writes it; r.get("user:1001:name") reads it back — a string is the simplest value a key can hold.
Key features
- r.set(key, value) writes a string value; r.get(key) reads it back, or returns None if the key does not exist
- r.incr(key) / r.incrby(key, n) atomically add to a value Redis parses as an integer — no read-modify-write race
- r.setnx(key, value) only sets the value if the key does not already exist yet — a cheap "claim this key once" check
- r.exists(key) returns 1 if the key exists and 0 if it does not — not a Python bool
- A colon-separated key like "user:1001:name" is a naming convention, not special syntax — Redis treats the whole string as one opaque key
Core string commands
- r.set("user:1001:name", "Priya Shah") — key → value, written in memory
- Redis server — holds every key across every connected client
- r.get("user:1001:name") — returns 'Priya Shah'
Command
import redis
r = redis.Redis(host="localhost", port=6379, decode_responses=True)
r.set("session:42", "active")
r.get("session:42") # 'active'
r.get("no_such_key") # NoneEvery Redis value lives under a unique string key; r.set/r.get read and write it, and r.incr/r.incrby update a numeric one atomically without a read-modify-write race.