Video in progress

Someone proposes caching user permissions (roles, permissions) in Redis. What security problems do you see, and how do you handle them?

The main risk is that when a permission is removed, the cache still says it exists: a fired employee or a blocked account can get in until the TTL expires. The solution: a short TTL, explicit invalidation when permissions change, rechecking against the source for critical actions, and protecting the cached data.

📖 Answer

A permissions cache has two kinds of mistakes, and they are not equal:

  • a permission was granted, but the cache still says "no": the user waits a minute, which is annoying;
  • a permission was removed, but the cache still says "yes": a security incident.

So the design must be asymmetric.

  1. Revocation should arrive fast: when a role is removed, an account is blocked, a password changes, we delete the cache entry right away and send an event to the L1 caches. But this is "best effort", so:

  2. A short TTL: 1-5 minutes. That's the worst window you can tolerate, and you agree on the number with the security team.

  3. A version/epoch: an auth_epoch counter for the user, in the DB and in Redis. It goes up on every block or role change. The cached permission record is stored with the epoch, and on check, if the epoch doesn't match, it's reloaded. The epoch itself is very cheap to read and is a single small key.

  4. Critical actions don't go through the cache: money transfers, admin actions, changing permissions are checked against the source (the DB or the IdP).

  5. Fail closed: if Redis is down or the data fails to deserialize, you don't answer "no permission" but go to the source; and if the source is down too, reject the critical actions.

  6. Protecting Redis itself: anyone with write access to the permissions cache can write an admin role for themselves. Use an ACL so only the auth service can write, TLS, a separate cluster or at least a separate prefix + ACL. A signed value (HMAC) is also an option if many services read it.

JWT has the same problem: the roles inside a token live until the token expires. So the access token should be short (5-15 minutes), and the list of revoked tokens (a deny-list) goes in Redis by jti with a TTL. In that case losing Redis data is dangerous, so run it with noeviction and HA.

Negative caching (caching a "no permission" result) also needs care: its TTL should be even shorter than the positive result's, so the user doesn't wait long after a new permission is granted.