SproutStackSproutStack home
IntermediateRedis

Sessions and cache share one Redis

Users are being logged out under load. Security wants sessions to survive restarts.

The setup today

One Redis instance holds two kinds of data: rendered page fragments (cache-aside, 10-minute TTL) and login sessions (30-day TTL, refreshed on each visit). It was set up as a cache, so it evicts under memory pressure and has no persistence.

Current configuration

maxmemory 6gb
maxmemory-policy allkeys-lru
save ""          # RDB snapshots off
appendonly no

What you know

  • During traffic peaks the instance sits at its memory limit and evicts about 2,000 keys a second.
  • Support tickets about being logged out spike at the same times.
  • A Redis restart last month logged out every user.

The new requirement

Security and product agree on two requirements: a user must never be logged out because of cache pressure, and sessions must survive a Redis restart. The cache must stay as fast as it is today.

Make the call

2 questions · first-try answers earn the most XP

1. Which design meets both requirements?
2. The session instance uses appendfsync everysec. What is the worst case if the machine loses power?
What a strong answer also covers
  • Size the session instance from the data: active users × session size × headroom, and alert well before maxmemory.
  • Add a replica (with Sentinel) for the session instance, since persistence alone does not cover losing the machine.
  • Remember replication is asynchronous: a failover can lose the newest session writes, which again means a re-login, not a breach.
  • Migrate without logging everyone out: read from both instances for one session lifetime, write only to the new one.

The background

More drills