System Design AI

OAuth 2.0 in System Design Interviews: When to Use It and What to Say

OAuth 2.0 is a protocol that lets one service obtain limited access to a user’s resources on another service without sharing passwords. In a design interview you’ll typically bring it up when the problem involves delegated access – for example, letting a mobile app call a user's photo library stored in a cloud service, or enabling a third-party analytics dashboard to read a company's metrics.

The problem OAuth solves

Imagine you are building a photo-sharing platform that wants to let users embed their Instagram pictures in a custom gallery. The naïve solution is to ask users for their Instagram credentials and store them. That approach has three major flaws:

  1. Security risk – you become responsible for protecting another service’s passwords.
  2. User friction – users are reluctant to type credentials into a third-party site.
  3. Revocation difficulty – the only way a user can cut off your access is to change their Instagram password, which also locks out every other app that holds it.

OAuth 2.0 solves this by issuing a short-lived access token that the photo-sharing platform can use on the user’s behalf. The user never shares their password with the platform; they only grant permission through a consent screen hosted by Instagram.

How OAuth 2.0 works – step by step example

Below is a minimal "Authorization Code" flow for a web app that wants to read a user's Google Drive files.

  1. Client registration – The web app registers with Google and receives a client_id and client_secret.
  2. User initiates login – The app redirects the user’s browser to Google’s authorization endpoint with parameters:
Code
 https://accounts.google.com/o/oauth2/v2/auth?
 client_id=YOUR_CLIENT_ID&
 redirect_uri=https://app.example.com/callback&
 response_type=code&
 scope=https://www.googleapis.com/auth/drive.readonly&
 state=xyz
  1. User authenticates & consents – Google shows a login screen, then a consent screen listing the requested scopes.
  2. Authorization code returned – After consent, Google redirects back to https://app.example.com/callback?code=AUTH_CODE&state=xyz.
  3. Token exchange – The app server POSTs to Google’s token endpoint:
Code
 POST https://oauth2.googleapis.com/token
 grant_type=authorization_code&
 code=AUTH_CODE&
 redirect_uri=https://app.example.com/callback&
 client_id=YOUR_CLIENT_ID&
 client_secret=YOUR_CLIENT_SECRET

Google responds with an access_token (an opaque string at Google; some providers issue JWTs) and, if offline access was requested, a refresh_token.

  1. API call – The app includes Authorization: Bearer ACCESS_TOKEN in the HTTP header when calling the Drive API.
  2. Token refresh – When the access token expires (typically after an hour), the app uses the refresh token to obtain a new access token without user interaction.

The key ideas are delegation (Google authenticates the user), least privilege (scopes limit what the app can do), and short-lived tokens (reducing damage if a token leaks).

Classic design questions where OAuth appears

Design QuestionWhere OAuth is relevantTypical spot in the diagram
Social login for a SaaS productUser authentication via Google/FacebookAt the edge, before the app’s own session service, showing a redirect to the identity provider.
API gateway for a multi-tenant data platformThird-party analytics tools need read-only access to tenant dataBetween the gateway and the data service, labeling the token validation component.
Mobile app accessing a user's cloud storageThe app needs permission to read/write the user’s filesIn the mobile-to-backend request path, showing the OAuth token exchange and refresh flow.

In each case you would draw a box for the Authorization Server (e.g., Google) and a Token Validation step inside your own service, often next to the API gateway.

Trade-offs and the alternative interviewers may ask about

AspectOAuth 2.0Alternative (e.g., API keys or custom JWT auth)
User experienceSingle-click consent, no password sharingUsers must create/manage another credential
SecurityTokens are short-lived, revocable, scopedAPI keys are long-lived, harder to rotate
ComplexityRequires redirects, token storage, refresh logicSimpler request signing, but less flexible
InteroperabilityStandard across many providersCustom scheme ties you to one implementation

Interviewers often probe the complexity side: “What if the redirect flow adds latency?” or “How would you handle token revocation at scale?” Be ready to discuss caching token introspection results, using short token lifetimes, and background revocation checks.

30-second spoken version

"OAuth 2.0 lets a service act on a user’s behalf without ever seeing the user’s password. The user is redirected to the identity provider, grants a set of scoped permissions, and the provider returns a short-lived access token (plus a refresh token). Your service validates the token on each request, and can refresh it when it expires. It’s the go-to solution for social login, third-party API access, and mobile-to-cloud interactions, trading a bit of redirect latency for tighter security and user-friendly consent."

Common mistakes candidates make

  1. Over-detailing the spec – Interviewers want the high-level flow, not every RFC parameter. Stick to the redirect, code, token exchange, and usage steps.
  2. Confusing OAuth with OpenID Connect – OIDC adds an ID token for authentication; OAuth alone only grants authorization.
  3. Ignoring token storage – Mention where you keep refresh tokens securely (e.g., encrypted DB) and how you rotate them.
  4. Skipping revocation handling – Explain how you would invalidate a token when a user revokes access, perhaps via token introspection or a revocation list.
  5. Reaching for the implicit flow – Current OAuth security guidance deprecates the implicit flow; single-page and mobile apps should use the Authorization Code flow with PKCE.

How to practice this

  1. Pick a classic design prompt (e.g., “Add Google login to a SaaS dashboard”). Sketch the request path and insert the OAuth redirect and token validation components.
  2. Use System Design AI’s canvas to draw the flow, then ask the AI coach to review your diagram for missing pieces.
  3. Run through the 30-second spoken version aloud until it feels natural, then rehearse answering follow-up trade-off questions.

Questions people ask

When should I bring up OAuth in a design interview?

Whenever the problem involves delegating user authority to a third-party service—social login, third-party analytics, or a mobile app accessing a user's cloud storage. It signals that you understand secure delegation without password sharing.

What’s the difference between the Authorization Code and Implicit flows?

Authorization Code exchanges a short-lived code for a token on a backend server, giving you refresh tokens and better security. Implicit returned the token directly to the browser and was once used for SPAs; it is now deprecated in favor of Authorization Code with PKCE.

How do I handle token revocation at scale?

Cache token introspection for a few minutes, subscribe to revocation notifications from the provider, and keep a revocation list that the API gateway checks before forwarding requests.

Can I replace OAuth with a simple API key for third-party integrations?

You can, but you lose scoped permissions, easy revocation, and a user-friendly consent flow. API keys are suited for trusted machine-to-machine calls, not for delegating user authority.