Tutorial

How to Rotate Refresh Tokens

Rotate refresh tokens on each use with replay detection, so a stolen refresh token is invalidated after one legitimate refresh.

On this page

  1. Introduction
  2. Issue a new refresh on use
  3. Invalidate the old
  4. Store a family id
  5. Detect replay
  6. Revoke the family on replay
  7. Test rotation
  8. Common pitfalls
  9. Conclusion

Introduction

The hardest part of detect refresh replay is not writing the code — it is deciding what the code is allowed to reject. A refresh rotation setup that accepts everything is easy to ship and expensive to operate, because every malformed input becomes a production surprise. The discipline is to enumerate the shapes the system actually receives, encode them in refresh rotation, and let the library reject the rest loudly. This keeps the happy path fast and the failure path visible.

Most teams adopt refresh rotation for detect refresh replay after an incident teaches them that unvalidated input is load-bearing. The incident is the expensive way to learn the lesson; a quarterly review of the input shapes is the cheap way. Pin the shapes with refresh rotation, refuse the deploy that breaks the pin, and treat the pin as a contract the rest of the system can rely on. A contract that is not enforced is a wish.

The sections below walk through the concrete decisions: where to put the refresh rotation check, how to keep it fast under load, what to do when the contract changes, and how to test the failure paths so they degrade on purpose instead of by accident.

Issue a new refresh on use

Issue a new refresh on use is the first decision because it determines what the rest of the refresh rotation pipeline can assume. A wrong choice here turns into detect refresh replay under load, and the invariant you thought refresh rotation guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Issue a new refresh on use is the one most teams skip: drive refresh rotation to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

async function refresh(old) {
  const p = verify(old);
  if (await used(old)) await revokeFamily(p.fam);
  await markUsed(old);
  return { access: issueAccess(p), refresh: issueRefresh(p.fam) };
}

The typescript above is the shape to ship for detect refresh replay. It is small enough to audit and specific enough that the failure behavior is obvious. Adapt the constants to your traffic and latency budget, and resist the urge to handle cases the system has not seen yet.

Invalidate the old

The mechanism behind Invalidate the old is straightforward in refresh rotation, but the consequences ripple. A wrong choice here turns into detect refresh replay under load, and the invariant you thought refresh rotation guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Invalidate the old is the one most teams skip: drive refresh rotation to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

Store a family id

Store a family id is where most refresh rotation implementations leak performance. A wrong choice here turns into detect refresh replay under load, and the invariant you thought refresh rotation guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Store a family id is the one most teams skip: drive refresh rotation to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

test('revoke family on replay', async () => {
  await refresh(t1);
  await expect(refresh(t1)).rejects.toThrow();
});

Pin the behavior with this test. A refresh rotation setup that passes this test degrades on purpose instead of by accident, which is the difference between a managed failure and an incident.

Detect replay

The hard part of Detect replay is not the happy path — it is what happens when refresh rotation fails halfway. A wrong choice here turns into detect refresh replay under load, and the invariant you thought refresh rotation guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Detect replay is the one most teams skip: drive refresh rotation to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

Revoke the family on replay

Revoke the family on replay should be observable without a debugger. A wrong choice here turns into detect refresh replay under load, and the invariant you thought refresh rotation guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Revoke the family on replay is the one most teams skip: drive refresh rotation to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

Test rotation

Test rotation is a trade, not a winner. A wrong choice here turns into detect refresh replay under load, and the invariant you thought refresh rotation guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Test rotation is the one most teams skip: drive refresh rotation to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

Common pitfalls

The common pitfalls in detect refresh replay all reduce to one mistake: treating refresh rotation as a formality instead of a contract. The contract is the thing the rest of the system relies on, and a weak contract propagates weakness everywhere.

  • Skipping the refresh rotation boundary so detect refresh replay enters unvalidated.
  • Assuming refresh rotation is fast without measuring the tail latency under load.
  • Letting detect refresh replay fail silently instead of failing loudly with a metric.
  • Testing only the happy path so the refresh rotation failure surfaces in production.
  • Sharing mutable state across requests so one bad input poisons the next.

Conclusion

Do not conflate the conclusion with the implementation. They look similar from the outside but the failure modes differ: refresh rotation fails fast and the caller knows, while an unchecked path fails slow and the caller guesses. Choose the one whose failure mode your system can absorb, and document the choice at the call site.

Self-check: write a test that drives refresh rotation to the failing condition and assert the system degrades gracefully. Then detect refresh replay is a property you maintain, not a feature you hope for.

Sources: OAuth 2.0 token rotation, JWT

FAQ

What is a family?
All refresh tokens from one login share a family id. Replay revokes the whole family.
Should I reuse the old?
Never. Always issue a new one and invalidate the old.

Related Examples