A signed token proves who a request comes from. It says nothing about whether that account is still welcome. On a platform that pays out cash, an admin blocking an account has to take effect at the next request. Two mechanisms on Redis handle that: a status cache read on every request, and refresh tokens that rotate on use.

The problem with claims

Both clients send a JSON Web Token as a Bearer header. The token carries the user’s id, type and role, and the server can verify it without a database.

Whatever the token says was true when it was signed. An admin blocks an account for abuse, and the token in that user’s app is still valid until it expires.

Loading the user from MongoDB on every request would close that gap. It would also make “find user by id” the most frequent query in the system, on the same database that carries the point transactions.

The status cache

A sequence with four participants: a client on the web or the app holding a Bearer token, an API route that verifies the token first, Redis holding account status for one hour, and MongoDB holding users. Step one, the client sends a request with an access token. Step two, the route reads the account status from Redis. Only when the entry is missing: step three, the route loads the user from MongoDB, and step four, stores the status in Redis with a time limit. When an account is blocked or logs out: step five, the route deletes the status entry and the refresh token.
Figure 1. The account check on a request. The route verifies the token, then reads the account's status from Redis. MongoDB is asked only on a miss, and a status change deletes the entry.

The server keeps one small hash per user in Redis: whether the account is blocked, whether it has withdrawn, whether login is allowed, its user type and its role. A request is checked in this order.

  1. Verify the token’s signature and expiry.
  2. Reject tokens issued for a different purpose. A short-lived token that only authorises a top-up page cannot be used as a session.
  3. Read the status hash. On a hit, refuse the request if the account is blocked or login is switched off.
  4. On a miss, load the user from MongoDB, write the hash with a one-hour lifetime, and apply the same test.

The cache stays current because the admin and session paths below delete or rewrite the entry.

EventWhat happens to the keys
An admin blocks an accountThe status entry and the refresh token are deleted
An account is converted to an advertiserThe status entry and the refresh token are deleted
An admin changes whether login is allowedThe status entry is rewritten with the new value
The user logs outThe status entry, the refresh token and the grace set are deleted

A change made on any other path is seen when the entry expires, at most an hour later.

Entries written before the role field existed are treated as a miss, so a deploy that adds a field does not have to flush the cache. The admin checks read more of the hash: the user type has to be admin, and the blocked, withdrawn and login-allowed flags all have to be clear, before the admin’s user document is loaded from MongoDB for the route.

Refresh tokens that rotate

A refresh token obtains the next access token. Each refresh returns a new access token and a new refresh token, and the hash stored for the user is replaced, so Redis holds one current refresh token per user.

A sequence with three participants: a client holding one refresh token, a refresh route that allows one refresh at a time per user, and Redis holding the hash of the current token. Step one, the client sends its refresh token. Step two, the route takes a short lock and compares the hash. If it is the current token, or one replaced under 120 seconds ago: step three, the route stores the new hash and keeps the old one briefly, and step four, returns new access and refresh tokens. If it differs from the stored hash and the grace set: step five, the route removes this account's tokens, and step six, answers 401, sign in again.
Figure 2. Refresh-token rotation. The route compares a hash of the presented token with the one in Redis. The current token, or one replaced within the grace window, is rotated. A token that differs from both gets a 401, and the stored hashes are removed.

Redis holds four keys per user for this. The shapes below are the ones in use.

user:status:{id}       hash     blocked, withdrawn, loginAllowed, userType, role      1 hour
refresh:{id}           string   SHA-256 of the current refresh token                  7 days
refresh:grace:{id}     set      hashes of tokens replaced in the last 120 seconds     120 seconds
refresh_lock:{id}      lock     one refresh in progress per user                      10 seconds

Only a hash of the refresh token is stored, so a copy of Redis does not contain usable tokens. Refresh tokens are signed with a different secret from access tokens, and an API test asserts that an access token presented at the refresh route is rejected.

The refresh route does this:

  1. Verify the refresh token’s signature.
  2. Take the per-user lock. If another refresh holds it, wait one second and try once more. If the lock is still held, answer 429 with “refresh in progress”.
  3. Hash the presented token and compare it with the stored hash, then with the grace set.
  4. A token that differs from the stored hash and is not in the grace set gets a 401, and the user’s refresh keys are deleted.
  5. On a match, read the account’s status from the cache, or from MongoDB on a miss. A blocked account, or one with login switched off, gets a 403.
  6. Issue a new pair that carries the account’s current type and role, store the new hash, and move the previous hash into the grace set for 120 seconds.

Step 6 takes the type and role from the status read in step 5 and does not copy them from the old token. A refresh after a conversion therefore returns tokens with the new type.

Why there is a grace window

Concurrent refreshes are routine. A browser can have two tabs open. An app can fire several requests at once when it comes back to the foreground. If the access token has expired, each of them tries to refresh with the same refresh token. The first one rotates it. The others arrive milliseconds later holding a token that no longer matches the stored hash. Without a tolerance, ordinary users would be signed out by their own second tab.

Two things prevent that. The lock makes the refreshes of one user take turns. The grace set makes the later ones succeed: for 120 seconds after a rotation the previous hash is still accepted. The grace set came from a design note written in August 2026 for exactly this race.

The clients are written to make the race rare in the first place. The web client refreshes about five minutes before the access token expires, before any request has failed. The app’s HTTP client sends one refresh when it sees a 401, parks every other request in a queue until that refresh returns, and replays them with the new token.

Failure modes

What happensWhat the system doesCovered by
A blocked account sends a request with a valid tokenThe status check refuses it. Blocking also deletes its refresh token, and a refresh attempt fails the same status check with a 403The status check inside the shared request helper
Two refreshes arrive togetherOne takes the lock and rotates. The other waits, or is accepted inside the grace windowThe rotation and grace groups of the Redis integration tests
A token that differs from the stored hash arrives after the windowThat request gets a 401 and the stored hashes are deletedThe same integration tests
An access token is sent to the refresh routeRejected, because the secrets differAn API test
A refresh succeeds for an account whose role changedThe new pair carries the current type and roleA test on the claims of refreshed tokens
Redis is switched off by configurationNo client is created, and the status check reads the user from MongoDB on each requestA test that asserts no client is created when Redis is disabled

The Redis tests need a real server, so they are excluded from the unit run. In CI they ran as their own job with a Redis service container.

Limits

  • One refresh token per user, not per device. Signing in on a second device replaces the first device’s token. Per-device sessions would need one hash per device and a way to list them.
  • Deleting a refresh token does not cancel an access token already issued. That token stays valid until it expires. The status check on each request is what stops a blocked account in the meantime.
  • The status entry is trusted for up to an hour. A change that does not go through one of the four paths in the table reaches the request check only when the entry expires.