Lesson 14 / 18

Session Storage

Share login sessions across multiple app servers.

Why not just app memory?

If sessions live in one server's memory, a user gets logged out when a load balancer routes them to a different instance. Redis gives every server the same shared session store.

Storing a session

A hash holds the session's fields, and a TTL matching the session timeout expires it automatically on logout-by-inactivity.

HSET session:xyz789 user_id "42" role "admin"
EXPIRE session:xyz789 1800
HGETALL session:xyz789

Output:

(integer) 2
(integer) 1
1) "user_id"
2) "42"
3) "role"
4) "admin"

Quick check: Why is Redis a good fit for sessions in a multi-server app?

  • It stores sessions on disk permanently
  • Every app server can read the same shared, fast session store
  • It automatically logs every user out
Answer

Every app server can read the same shared, fast session store — A shared, low-latency store means any server can serve any user's session without stickiness.