Learn Labs
Application Patterns

Sessions, Queues & Counters

Session stores, job queues, delayed jobs, and real-time counters

Three more patterns that show up in almost every web backend.

Session store

A web session is small, read on every request, written often, and should disappear when the user goes idle. That's a near-perfect fit for a Redis hash with a sliding TTL:

HSET session:7f3a9c user_id 42 csrf x9f2 created 1791789000loginEXPIRE session:7f3a9c 180030 min idle timeoutHGETALL session:7f3a9cevery request...EXPIRE session:7f3a9c 1800...and slide the timeout (pipeline both)HSET session:7f3a9c cart_items 3update one fieldDEL session:7f3a9clogout

Things to decide up front:

  • Session ID: at least 128 random bits from a CSPRNG. Never a user ID or a sequential number.
  • "Log out everywhere": keep a set of a user's session IDs, SADD user:42:sessions 7f3a9c, so you can find and delete them all. (The set entries outlive expired sessions; clean them lazily.)
  • Durability: a failover can lose the last second of writes, which for sessions usually means a few users get logged out. If that's unacceptable, the session store needs appendfsync always or a database.
  • Eviction: sessions must not be evicted to make room for cache entries. Use noeviction or a separate instance from your cache.

Job queues

Three ways to build a queue in Redis, from simplest to most robust:

ApproachDeliveryCrash safetyMultiple consumer groups
List + BRPOPEach job to one workerLost if a worker crashes mid-jobNo
List + BLMOVE to a processing listEach job to one workerRecoverable, with a reaper you writeNo
Stream + consumer groupEach job to one worker per groupBuilt in: pending list + XAUTOCLAIMYes

For new work, use streams. They're covered in full in Streams. Libraries like Sidekiq (Ruby), BullMQ (Node), RQ and Celery (Python) build queues on Redis and handle retries, scheduling, and dead letters for you.

Delayed jobs

A sorted set scored by run-at time:

ZADD app:jobs:delayed 1791790000 '{"type":"send_reminder","user":42}'

# a scheduler loop, every second:
ZRANGE app:jobs:delayed -inf <now> BYSCORE LIMIT 0 100   # jobs that are due
# for each: ZREM it (only the caller whose ZREM returns 1 owns the job), then enqueue it

ZREM returning 1 is the claim: if two scheduler processes see the same due job, only one successfully removes it. Doing the range and the remove in one Lua script avoids even fetching jobs another scheduler is taking.

Real-time counters

Atomic increments make Redis a natural home for live counts:

INCR app:stats:views:article:991total viewsHINCRBY app:stats:views:2026-10-12 article:991 1per day, one hash per dayPFADD app:stats:uniques:article:991:2026-10-12 user:42unique viewers (HyperLogLog)ZINCRBY app:trending:2026-10-12T14 1 article:991trending this hour

The usual pattern is count in Redis, persist periodically: a job reads and resets counters every minute and writes them to Postgres or ClickHouse, so Redis holds only the live window and the database holds history.

# move a counter to the database atomically: read and reset in one step
GETDEL app:stats:views:article:991     # returns the count and deletes the key

Feature flags and config

Values every request reads, changed rarely:

HSET app:flags new_checkout on beta_search off
HGET app:flags new_checkout

At high request rates this becomes a hot key. Cache it in application memory for a few seconds, or use client-side caching (CLIENT TRACKING), so each app server reads Redis once rather than once per request.

Lab

  1. Implement login/logout and a sliding 30-minute session in Python with a hash, plus "log out everywhere" with a per-user set.
  2. Build the delayed-job scheduler: add 5 jobs due 1-5 seconds from now and a loop that moves due jobs into stream:jobs.
  3. Write a view counter with INCR, and a flush job that uses GETDEL to move counts into a Python dict "database" every 5 seconds while another thread increments. Confirm no increments are lost.

On this page