Script Valley
Redis: Complete Course
Core Commands and Data StructuresLesson 2.1

Redis String commands: SET, GET, INCR, APPEND, GETRANGE

SET options NX EX PX, GET, MSET MGET, INCR INCRBY DECR, APPEND, GETRANGE, STRLEN, atomic counter pattern

String commands in depth

Strings are the most versatile Redis type. They power counters, caches, flags, and binary blobs.

SET options

# Only set if key does NOT exist (atomic check-and-set)
SET lock:resource "owner1" NX EX 10

# Set multiple keys atomically
MSET user:1:name "Alice" user:2:name "Bob"
MGET user:1:name user:2:name
# โ†’ ["Alice", "Bob"]

Counters

SET page:views 0
INCR page:views        # โ†’ 1
INCRBY page:views 5    # โ†’ 6
DECR page:views        # โ†’ 5
INCRBYFLOAT price 1.5  # works with floats too

INCR is atomic. Two clients calling INCR simultaneously will each get a unique incremented value โ€” no race condition, no lost update.

String manipulation

SET greeting "Hello"
APPEND greeting " World"   # โ†’ 11 (new length)
GET greeting               # โ†’ "Hello World"
GETRANGE greeting 0 4      # โ†’ "Hello"
STRLEN greeting            # โ†’ 11

GETRANGE is O(N) on the returned substring length. For large strings used as binary buffers, prefer GETRANGE over GET to avoid transferring the full value.

Up next

Redis Hash commands: HSET, HGET, HMGET, HDEL, HGETALL

Sign in to track progress

Redis String commands: SET, GET, INCR, APPEND, GETRANGE โ€” Core Commands and Data Structures โ€” Redis: Complete Course โ€” Script Valley โ€” Script Valley