PrepZone Logo
PrepZone

Redis Strings, Lists, and Sets

SET, GET, LPUSH, SADD — the commands behind sessions, queues, and unique tags.

Why this matters

  • VaultCommerce product views use INCR for real-time counters, lists for audit trails, and sets for "customers who viewed this category."
  • Wrong type choice costs you: scanning a list for membership is O(n); a set gives O(1) lookups.
  • These three types cover most cache-aside and lightweight queue patterns before you reach sorted sets and streams.
StringSessions, counters
ListQueues, feeds
SetUnique tags
Sorted SetLeaderboards
HashUser profiles
StreamEvent log
Each type maps to a common backend pattern.

Strings: sessions and counters

Strings store text, integers, or binary up to 512 MB. Atomic increment/decrement makes them ideal for inventory and view counts without Postgres row locks.

Java
SET session:tok_a8f2 "{\"userId\":42,\"role\":\"buyer\"}" EX 1800
GET session:tok_a8f2
INCR product:SKU-8842:views
INCRBY inventory:SKU-8842 -1
SETNX lock:checkout:42 "worker-7" EX 30
MSET promo:FLASH10 "active" promo:WELCOME "active"
MGET promo:FLASH10 promo:WELCOME

String commands VaultCommerce uses

  • SET key value EX seconds — write with TTL (sessions, cache entries).
  • SETNX / SET ... NX — acquire a short-lived lock for idempotent checkout.
  • INCR / INCRBY — atomic counters for views, stock reservations, rate limits.
  • MGET — batch-read multiple product cache keys in one round trip.

Lists: recent activity feeds

Lists are linked lists with O(1) push/pop at head or tail. VaultCommerce stores the last ten cart events per user for support dashboards — not for random access by ID.

Java
LPUSH user:42:cart-events "added SKU-8842"
LPUSH user:42:cart-events "removed SKU-1100"
LRANGE user:42:cart-events 0 9
LLEN user:42:cart-events
LPOP user:42:cart-events

Use BLPOP when a worker blocks waiting for the next webhook retry job. Lists are queues, not indexes — avoid LINDEX in hot paths for large lists.

Sets: unique tags and audiences

Sets store unique unordered strings. O(1) add, remove, and membership checks make them perfect for deduplication.

Java
SADD category:electronics:viewers "user:42" "user:99" "user:42"
SISMEMBER category:electronics:viewers "user:42"
SCARD category:electronics:viewers
SINTER wishlist:42 wishlist:99
SUNION promo:email-list promo:sms-list

VaultCommerce tracks newsletter opt-ins with SADD newsletter:subscribers {email_hash} and checks duplicates before sending campaigns.

Python integration sketch

Java
import redis

r = redis.Redis(host="localhost", port=6379, decode_responses=True)

def cache_product(sku: str, payload: str, ttl: int = 3600) -> None:
    r.set(f"product:{sku}", payload, ex=ttl)

def reserve_stock(sku: str, qty: int = 1) -> int:
    remaining = r.decrby(f"inventory:{sku}", qty)
    if remaining < 0:
        r.incrby(f"inventory:{sku}", qty)  # rollback
        raise ValueError("out of stock")
    return remaining

Quick recall

Everything you need if you only revisit this box.

  • Strings: sessions, JSON cache, atomic counters — use SET EX, INCR, SETNX.
  • Lists: ordered queues and recent-event feeds — LPUSH, LRANGE, BLPOP.
  • Sets: unique membership and tag intersections — SADD, SISMEMBER, SINTER.
  • Match the type to access pattern: membership belongs in sets, not lists.
  • VaultCommerce prefixes keys by domain to keep namespaces isolated.
  • Batch reads with MGET to cut round trips on product grid pages.

Test yourself

Answer these before moving on — recall is what makes it stick.