Redis Strings

The String type is the simplest building block in Redis. A String value can hold text, a number, or raw binary data — up to 512 megabytes. Despite the name "String," this type handles much more than just words.

The Labeled Box Analogy

Picture a row of labeled boxes on a shelf. Each box has a name on the front (the key) and holds exactly one thing inside (the value). You open the box by name, see what is inside, swap the contents, or throw the box away.

Shelf (Redis Memory)
┌────────────────┐  ┌────────────────┐  ┌────────────────┐
│  Key:          │  │  Key:          │  │  Key:          │
│  "site:title"  │  │  "visitors"    │  │  "promo:code"  │
│                │  │                │  │                │
│  Value:        │  │  Value:        │  │  Value:        │
│  "eStudy247"   │  │  "18042"       │  │  "SAVE20"      │
└────────────────┘  └────────────────┘  └────────────────┘

SET and GET – The Core Commands

127.0.0.1:6379> SET site:title "eStudy247"
OK
127.0.0.1:6379> GET site:title
"eStudy247"

SET overwrites any previous value for the same key without warning. If you need to set a key only when it does not already exist, use SETNX (SET if Not eXists):

127.0.0.1:6379> SETNX promo:code "SAVE20"
(integer) 1    ← created (key did not exist)

127.0.0.1:6379> SETNX promo:code "FLAT10"
(integer) 0    ← rejected (key already exists, value unchanged)

Storing Numbers and Using Atomic Counters

Redis stores numbers as strings internally but provides special commands that treat them as integers. This makes counters safe — even when two server processes try to increment at the same moment, Redis handles both correctly one after another.

127.0.0.1:6379> SET visitors 100
OK
127.0.0.1:6379> INCR visitors
(integer) 101
127.0.0.1:6379> INCR visitors
(integer) 102
127.0.0.1:6379> INCRBY visitors 50
(integer) 152
127.0.0.1:6379> DECR visitors
(integer) 151

Why Counters Are Safe in Redis

Two web servers both call INCR at the same millisecond:

Without Redis (shared variable in memory — unsafe):
  Server A reads visitors = 100
  Server B reads visitors = 100
  Server A writes 101
  Server B writes 101   ← One increment lost!

With Redis INCR (single-threaded, safe):
  Server A sends INCR → Redis processes → 101
  Server B sends INCR → Redis processes → 102   ← Correct!

Storing Multiple Keys at Once

Use MSET and MGET to read or write several keys in a single round trip. This saves network time compared to sending commands one by one.

127.0.0.1:6379> MSET fname "Riya" lname "Sharma" city "Pune"
OK
127.0.0.1:6379> MGET fname lname city
1) "Riya"
2) "Sharma"
3) "Pune"

APPEND – Adding to an Existing String

127.0.0.1:6379> SET log:today "User logged in."
OK
127.0.0.1:6379> APPEND log:today " User clicked checkout."
(integer) 39
127.0.0.1:6379> GET log:today
"User logged in. User clicked checkout."

STRLEN – Getting the Length

127.0.0.1:6379> STRLEN log:today
(integer) 38

GETSET – Read the Old Value While Setting a New One

GETSET returns the current value and replaces it with a new one in a single atomic operation. This pattern works well for rotating tokens or resetting counters while capturing the last state.

127.0.0.1:6379> SET token "abc123"
OK
127.0.0.1:6379> GETSET token "xyz789"
"abc123"    ← old value returned
127.0.0.1:6379> GET token
"xyz789"    ← new value is now stored

Practical Example – Page View Counter for a Blog

Each time someone visits article ID 501:

  App calls: INCR article:501:views

Redis memory:
  Before visit:  article:501:views = 2047
  After INCR:    article:501:views = 2048

To display the count on the page:
  App calls: GET article:501:views
  Returns:   "2048"

No database query needed. Redis handles it in under 1 ms.

Key Points

  • A Redis String holds text, numbers, or binary data up to 512 MB.
  • SET writes a value; GET reads it; DEL removes it.
  • SETNX writes only if the key does not already exist — safe for locks and first-time writes.
  • INCR, INCRBY, DECR, and DECRBY handle numeric counters atomically.
  • MSET and MGET batch multiple operations into one network round trip.

Leave a Comment