Library / Case Studies
Scalability and Authentication at Twitch
Curious how Twitch Identity services address scalability and performance challenges related to authentication?
By Twitch engineers · Twitch · Added 2026-10-12
All content belongs to Twitch engineers / Twitch. We only link to it and give credit. Report a problem
Learn this case study
About 37 min left. Your work is saved as you go.
What you are about to study
Twitch explains a real system. You should know the basics: caching, queues and replication.
Ideas you will meet
- Distributed Systems
- Consensus, time, failure and the fundamentals of scale-out.
- Web Security
- OWASP risks, XSS, CSRF, CORS and secure design.
What we already know about this
Our own lessons on these ideas. Read them here before you start, so it makes sense.
Real-World Systems · 10 minSecuring a web APIAuthentication, authorisation, input handling and abuse protection: the layers that keep an API safe.Read here
Think in layers
No single control secures an API. Defence comes from several layers, each assuming the others may fail.
100%Figure SDA-RWS-14-1 · API security layers · © forcsguy.com
1. Transport
Use HTTPS everywhere, with modern TLS and HSTS. Never accept credentials over plain HTTP.
2. Authentication: who is calling?
- Users: OAuth 2.0 / OpenID Connect access tokens, or secure session cookies for browsers (see the earlier lessons).
- Machines: API keys or client credentials, stored as hashes, scoped, and rotatable.
- Send credentials in the
Authorizationheader, never in URLs. - Keep token lifetimes short, support revocation, and protect refresh tokens carefully.
- Add multi-factor authentication for sensitive accounts, and protect login endpoints with rate limits.
3. Authorisation: what may they do?
This is where most real API breaches happen.
- Check on every request, on the server, both the action (role or scope) and the specific object. "Is this user logged in?" is not enough; ask "may this user read order 123?" (broken object-level authorisation is the top API risk).
- Apply least privilege: tokens carry only the scopes they need.
- Never rely on the client hiding buttons.
- Do not expose sequential ids as a security measure. Use authorisation checks (random ids only help a little).
4. Input and output
- Validate every input against a schema: types, lengths, ranges, allowed values. Reject unknown fields (prevents mass assignment, where a caller sets fields like
isAdmin). - Use parameterised queries to prevent SQL injection, and encode output to prevent injection into other systems.
- Limit request size and file types, and scan uploads.
- Return only the fields needed (avoid excessive data exposure), and paginate lists.
- Return generic error messages to clients and keep detail in logs, so errors do not leak internals.
100%Figure SDA-RWS-14-2 · Two ways a caller proves who it is · © forcsguy.com
100%Figure SDA-RWS-14-3 · Signing each request with a shared secret (HMAC) · © forcsguy.com
Image: Spanish Coches · CC BY 2.0 · Source · Wikimedia Commons: Peaje - Toll - Péage (5020183265)5. Abuse protection
- Rate limiting per user, key and IP, with stricter limits on expensive or sensitive endpoints.
- Quotas and timeouts; protect against huge queries (depth limits in GraphQL, page size caps).
- Bot and credential-stuffing defences: lockouts, delays, CAPTCHAs, breached-password checks.
6. Webhooks and service-to-service calls
- Sign outgoing webhooks with an HMAC over the body and a timestamp. Receivers verify the signature in constant time, reject old timestamps and handle events idempotently.
- Between services use mutual TLS or signed tokens, and network policies, instead of trusting "internal" traffic.
7. Browsers: CORS and cookies
- Configure CORS with an explicit allow-list of origins. Never reflect any origin with credentials enabled.
- Cookies:
HttpOnly,Secure,SameSite. Protect state-changing requests from CSRF.
An example worth knowing: in 2021 a flaw in a popular Java logging library (Log4Shell) let attackers run their own code simply by getting a crafted string written to a log.
100%Figure SDA-RWS-14-4 · How untrusted text became remote code execution · © forcsguy.com
8. Secrets, data and visibility
- Keep secrets in a secrets manager, not in code or images, and rotate them.
- Encrypt sensitive data at rest, and minimise what you store.
- Log authentication events and authorisation failures (never secrets), and alert on anomalies. Keep an inventory of your APIs so old versions and forgotten endpoints do not stay exposed.
- Keep dependencies patched, and test: security tests, dependency scanning, and periodic penetration testing.
More on this site
Related reading
- MDN: Cross-Origin Resource Sharing (CORS)MDN contributors · MozillaDocumentation
- Introduction to JSON Web TokensAuth0 · jwt.ioDocumentation
- RFC 6749: The OAuth 2.0 Authorization FrameworkIETFDocumentation
- AWS Security BlogAmazon Web Services engineers · Amazon Web ServicesBlogs
Show 6 moreShow fewer
- OWASP Cheat Sheet SeriesOWASP · OWASP FoundationDocumentation
- Google Security BlogGoogle engineers · GoogleBlogs
- How we built rate limiting capable of scaling to millions of domainsCloudflare engineering · CloudflareCase Studies
- OWASP Top 10OWASP · OWASP FoundationDocumentation
- Rate LimitingKeycdn engineers · KeycdnCase Studies
- Scalability and Authentication at TwitchTwitch engineers · TwitchCase Studies
Go deeper on this
A path through videos, articles, other case studies and code on the same ideas as Scalability and Authentication at Twitch.
Warm upGet the vocabulary first.
Books · Free from the author
Concurrent and Distributed Systems (Cambridge lecture notes)
Eight lectures on time, broadcast, replication, consensus and transactions, with notes, slides and video. Free from the author under CC BY-SA.
University of CambridgeIntermediate
Books · Free from the author
Computer Networks: A Systems Approach
How networks are built and why: protocols, routing, congestion control and the internet's architecture. The authors publish it free online under CC BY 4.0.
Systems ApproachIntermediate
WatchHow others solve the same problem, on video.
Videos belong to their creators and play from YouTube. More on YouTube (opens in a new tab)
ReadEngineers explain it in their own words.
Blogs · Free from the author · Read here
Scalable Web Architecture and Distributed Systems
A full chapter on the building blocks of large websites: availability, performance, reliability, scalability, caches, proxies, indexes, load balancers and queues. Free to read here under CC BY 3.0.
The Architecture of Open Source ApplicationsBeginner40 min
Papers · Read here
Facebook’s photo storage
This paper describes Haystack, an object storage system optimized for Facebook’s Photos application. Facebook currently stores over 260 billion images, which translates to over 20 petabytes of data.
USENIXIntermediate
See it in another companyThe same ideas, a different system.
Case Studies · Read here
Clock Skew when Syncing Password Payloads at Drobbox
How Dropbox Passwords fixed sync errors from clock skew by replacing a two-way timestamp merge with a three-way merge against a base payload.
DrobboxIntermediate
Case Studies · Read here
OAuth Audit Toolbox at Indeed
Integrating third party apps with GSuite can be risky. Indeed's OAudit Toolbox allows you to educate your users and improve application security.
IndeedIntermediate
Read the codeSee how it looks when it is real.
GitHub
Engineering Blogs
A huge directory of company engineering blogs to learn from real production systems.
kilimchoiIntermediate
GitHub
Awesome Scalability
Curated reading list of articles on the architecture of scalable, reliable systems.
binhnguyennusIntermediate
Keep learning
Loading community…