Key-value storage and strings
corebeginnerRedis 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.
Think of it as
Think of Redis as one enormous Python dict shared by every process that connects to it, kept in memory for speed. r.set(key, value) and r.get(key) are that dict's [key] = value and [key] — except the value always travels over the network as bytes (or str, once decode_responses=True asks the client to decode it), and the key has to be unique across your whole application, not just one script.
What we're doing: Write and read a string value, use SETNX to avoid overwriting an existing key, and INCR to update a counter without a race condition.
- 2
- r.get returns the string exactly as stored — decode_responses=True on the client turns the raw bytes into a Python str.
- 7
- setnx ("SET if Not eXists") only writes when the key is absent — since user:1001:name already exists, this call does nothing.
- 10
- append adds to the end of the existing string value and creates the key if it was missing — it does not error.
Priya Shah
6
Priya Shah
Priya Shah (verified)Why this works: set/get map directly onto Redis storing one value per key. incrby is atomic on the server — Redis, not your Python process, does the read-add-write, so two clients calling it concurrently never lose an update the way a plain get-then-set would. setnx makes "create if missing" a single round trip instead of a check-then-act race.
Reading, modifying in Python, then writing back instead of using INCR
Wrong
Better
What you see: Under concurrent requests, some increments silently disappear — the counter ends up lower than the number of increments actually issued.
Why: get-then-set is two separate round trips with a gap in between. If two processes both read the same value before either writes, both compute old_value + 1 and the second write clobbers the first — one increment is lost. r.incr runs entirely on the Redis server as a single operation, so there is no gap for another client's write to land in.
- 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'
Core string commands
Together
Remember: Every 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.
See also: ttl and expiration · hashes · atomic operations

