Home / Companies / WorkOS / Blog / Post Details
Content Deep Dive

OAuth token refresh has a race condition. Fix it with a conditional write, not a distributed lock.

Blog post from WorkOS

Post Details
Company
Date Published
Author
Maria Paktiti
Word Count
2,183
Company Posts That Month
89
Language
English
Hacker News Points
-
Post removed?
No
Summary

OAuth token refresh concurrency can cause two distinct problems: stale token records from competing writes and, more seriously, refresh-token replay detection that may revoke an entire user session when providers rotate tokens. The recommended design prioritizes prevention and database-level correctness: refresh tokens ahead of expiry with jitter and a safety skew, collapse duplicate requests within each process using single-flight handling, and use a versioned conditional database update so stale workers cannot overwrite newer token data. Distributed Redis locks can reduce cross-process duplicate refreshes but cannot guarantee exclusive access because leases may expire while a worker is paused, so they should be treated as an optional throughput optimization rather than the core correctness mechanism. Because no client-side coordination can fully prevent duplicate calls across an unreliable network boundary, implementations must account for provider-specific rotation, reuse-grace periods, token lifetimes, and error semantics, distinguishing terminal grant failures from transient provider issues. The text also describes WorkOS Pipes and Relay as managed services that centralize token storage, refresh handling, concurrency serialization, authorization-state reporting, and provider calls.

Trends Found in this Post
Trend Post Mentions Total Month Mentions Posts Companies MoM
Agent sandbox 1 59 18 11 +26%
Use This Data

Use this post, company, and trend context to find content marketing opportunities, perform competitive analysis, or address product feature gaps via the Plushcap MCP server or the Plushcap API.