Code Reference

Strings

Redis · Reference cheat sheet

Strings

Redis · Reference cheat sheet


📋 Overview

Strings are the simplest Redis values: text, integers (for counters), or binary blobs (up to 512 MB theoretically; keep small in practice).

🔧 Core concepts

CommandRole
SET key valueSet string
GET keyGet string
MSET / MGETMulti set/get
SETNXSet if not exists
INCR / DECRInteger increment
APPENDAppend to string
GETRANGE / SETRANGESubstring ops
SET key value EX secondsSet with TTL
OptionMeaning
EX / PXExpire seconds / ms
NX / XXOnly if missing / only if exists
GET (SET GET)Return old value (Redis 6.2+)

💡 Examples

Cache entry:

SET user:42:profile '{"name":"Ada"}' EX 3600
GET user:42:profile

Counter:

INCR page:home:views
GET page:home:views

Lock-style NX:

SET lock:job1 1 NX EX 30

⚠️ Pitfalls

  • INCR fails if the value is not an integer string.
  • Huge strings block peers longer during ops — split or use other structures.
  • KEYS * in production is dangerous — use SCAN.

On this page