Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 42 ยท Caching and Application Patterns

Sessions and Rate Limiting

Store user sessions in Redis hashes with TTLs and build rate limiters: fixed window, sliding window log, token bucket and leaky bucket, made atomic with Lua.

Two of the most common jobs for Redis have nothing to do with caching query results. The first is session storage: a login session is a small record that every request reads, that must disappear on its own after a while, and that several web servers must share. The second is rate limiting: counting how often a user or IP did something in the last few seconds and saying "slow down" when it is too often.

Both rely on three features you already know: hashes for small records, expiry from keys and expiry, and the fact that single commands such as INCR are atomic. They also expose the main trap in Redis application code: a sequence of separate commands is not atomic, so limiters need a transaction or a Lua script.

Real Redis cannot run on this page, so redis-cli and Lua blocks are read-only. The runnable examples use a JavaScript stand-in with a fake clock (milliseconds that only move when the program says so) and the same command semantics.

A session in Redis

Random id Unguessable, kept in a cookie.
Hash user, role, csrf token, last seen.
TTL Idle sessions vanish by themselves.

The cookie holds only the id, never the data.

1 / 2

Sessions with hashes and TTL

The usual design: when a user logs in, the application generates a long random session id (at least 128 bits from a cryptographically secure generator), stores the data under session:<id> and sends the id to the browser in a cookie marked HttpOnly, Secure and SameSite. Every request looks the session up by id. Read-only redis-cli view:

TEXT
127.0.0.1:6379> HSET session:9f2c41 user 42 role editor csrf 7d1e created 1700000000
(integer) 4
127.0.0.1:6379> EXPIRE session:9f2c41 1800
(integer) 1
127.0.0.1:6379> HGETALL session:9f2c41
1) "user"
2) "42"
...
127.0.0.1:6379> TTL session:9f2c41
(integer) 1799

A hash is a good fit: you can update one field (HSET session:9f2c41 last_seen ...) without rewriting the others, and HGET fetches a single field. Two expiry designs exist, and many apps use both:

  • Sliding (idle) expiry: each request calls EXPIRE again, so the session lives while the user is active and dies after, say, 30 idle minutes.
  • Absolute expiry: store the creation time and refuse sessions older than a fixed limit (for example eight hours), even if active, so a stolen cookie cannot live forever.

Practical rules: regenerate the id at login (to prevent session fixation), DEL the key at logout, and keep sessions small. If you need "log out everywhere", also keep a set user:42:sessions with that user's ids. Run HSET and EXPIRE together in one pipeline or MULTI block so a crash between them cannot leave a session that never expires.

JS Session store with sliding and absolute expiry

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first session survives a 25 minute gap and dies after 31. The second is active every 20 minutes, so the idle timer never fires, yet the absolute limit still ends it once eight hours have passed. Note that TTL of a missing key is -2, as in real Redis.

Rate limiting: counting requests

A rate limiter answers "may this client do this now?" Redis fits because counters are fast, atomic and expire by themselves. The four classic algorithms trade accuracy, memory and burst behaviour.

Fixed window. One counter per key per time slot, such as rate:user42:<minute>: INCR, and on the first hit EXPIRE to the window length. Cheap and simple, but it has a boundary flaw: a client can send the limit at the end of one window and the full limit again at the start of the next, doubling the allowed rate for a moment.

Sliding window log. Keep every request as a member of a sorted set whose score is its timestamp. To check a request: ZREMRANGEBYSCORE removes entries older than the window, ZCARD counts what is left, and if below the limit ZADD records this request. Exact, but memory grows with the limit (one entry per request), and the steps must be atomic.

Sliding window counter. A cheaper approximation: keep the current and previous fixed-window counts and weight the previous one by how much of it still overlaps the sliding window. One or two integers per client, with small error.

The next demo sends five requests just before a window boundary and five just after it, against a limit of five per ten seconds.

JS Fixed window versus sliding window log

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The fixed window let ten requests through in under two seconds, while the sliding log held the line at five and recovered once the old entries aged out.

Token bucket and leaky bucket

A token bucket holds up to capacity tokens and gains rate tokens per second. A request takes one token; with none left it is rejected. Idle clients accumulate tokens, so short bursts up to the capacity are allowed while the long-run rate stays at rate. State is just two numbers per client: the token count and the time of the last update, so a hash is enough. Refill is computed lazily when a request arrives: tokens = min(capacity, tokens + elapsed * rate).

A leaky bucket is the mirror image: requests pour water in, it leaks out at a constant rate, and a request that would overflow the bucket is rejected. As a meter (a counter that drains) it behaves like a token bucket. As a queue it also smooths the output to a perfectly steady rate, which suits protecting a backend with a fixed throughput; in Redis the queue form is typically a list or stream drained by a worker.

Why it must be atomic

The refill logic is read, calculate, write: three steps. If two requests interleave between the read and the write, both see one remaining token and both are allowed. A Lua script runs as a single unit, because Redis executes it without running other commands in between. Real script (read-only here):

LUA
-- KEYS[1] bucket key; ARGV: capacity, tokens per second, now in ms
local state = redis.call('HMGET', KEYS[1], 'tokens', 'ts')
local capacity = tonumber(ARGV[1])
local rate = tonumber(ARGV[2])
local now = tonumber(ARGV[3])
local tokens = tonumber(state[1]) or capacity
local ts = tonumber(state[2]) or now
tokens = math.min(capacity, tokens + (now - ts) / 1000 * rate)
local allowed = 0
if tokens >= 1 then
  tokens = tokens - 1
  allowed = 1
end
redis.call('HSET', KEYS[1], 'tokens', tokens, 'ts', now)
redis.call('PEXPIRE', KEYS[1], math.ceil(capacity / rate * 1000) * 2)
return { allowed, tostring(tokens) }

Two details: Lua numbers returned to the client are truncated to integers, so the token count is returned with tostring; and the PEXPIRE lets idle buckets disappear (a full bucket and a missing one are equivalent). Instead of passing now from the client, whose clock may differ between servers, a script can read the server's clock with redis.call('TIME') in current versions. Lua scripting covers EVAL in depth. A MULTI/WATCH retry loop works too, but a script is shorter and has no retries.

JS Token bucket, leaky meter and the race

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In the first block the burst of seven requests uses the five tokens and rejects two; after a pause the bucket has refilled. The leaky meter shows the same idea mirrored. The last block replays the interleaving that causes the race and shows the atomic version rejecting the second client.

Response details and choosing an algorithm

Good limiters tell clients what happened: respond with HTTP status 429 and a Retry-After header, and add headers such as the limit, the remaining count and the reset time. Compute Retry-After from the algorithm: for a token bucket it is (1 - tokens) / rate seconds.

  • Fixed window for coarse, cheap limits (daily quotas) where boundary doubling does not matter.
  • Sliding log for small limits that must be exact (login attempts: 5 per 15 minutes).
  • Sliding window counter for large-scale APIs where approximate is fine.
  • Token bucket when you want to permit bursts but cap the average; it is what most public APIs use.

Decide what the limiter does when Redis is unreachable: fail open (allow requests, risking overload) or fail closed (block them, risking an outage caused by the limiter). For a general API, fail open with an alert is the usual choice; for security limits such as password attempts, consider failing closed. In a cluster, keep all keys one script touches in the same hash slot.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What should the browser cookie contain in a Redis-backed session design?

    The cookie carries only an unguessable id. The server looks the data up in Redis, so the client cannot read or change it.
  2. 2. What problem does an absolute session lifetime solve that sliding expiry does not?

    With only sliding expiry, an active (or attacker-driven) session never dies. A fixed maximum age since login bounds the damage.
  3. 3. What weakness does a fixed-window rate limiter have?

    Counters reset at window boundaries, so a burst straddling the boundary can reach about twice the limit in a short time.
  4. 4. Why is a token bucket check usually written as a Lua script?

    Redis runs a script without interleaving other commands, which closes the race between reading the token count and writing it back.
  5. 5. Which algorithm allows short bursts while enforcing an average rate?

    A token bucket accumulates tokens while idle, so a burst up to its capacity is allowed, while the refill rate caps the long-run average.

Summary

  • Store a session as a hash under session:<random id>, put only the id in a cookie, and use EXPIRE for idle timeouts plus a stored creation time for an absolute limit.
  • Fixed windows are cheap but allow boundary bursts; sliding logs are exact but cost memory; sliding counters approximate cheaply.
  • A token bucket stores a token count and a timestamp, refills lazily and allows bursts; a leaky bucket drains at a constant rate.
  • Multi-step limiter logic must be atomic: use a Lua script (or MULTI with WATCH).
  • Return 429 with Retry-After, and decide deliberately whether the limiter fails open or closed.

Next, work queues, locks and ranking: Queues, Distributed Locks and Leaderboards.