Skip to content

Security

Security is broad. Unless you have deep experience or are applying for a security role, you likely only need the basics.

The essentials

  • Encrypt in transit and at rest.
  • Sanitize all user inputs and any input parameters exposed to users to prevent XSS (cross-site scripting) and SQL injection.

  • Use parameterized queries to prevent SQL injection.

  • Apply the principle of least privilege (give users/processes only the permissions they need).

Quick checklist

Concern Mitigation
Data in transit TLS/HTTPS
Data at rest Encryption (disk/DB)
SQL injection Parameterized queries + input sanitization
XSS Escape/sanitize user input
Over-permission Least privilege

Key takeaways

  • For most system design interviews, mention the four bullets above and move on.
  • Security is cross-cutting: it applies to every layer (edge, app, DB, storage).

Common Interview Questions

Design a secure URL shortener

Why it's asked here: URL shorteners have real abuse/security concerns.

Key points to discuss:

  • Rate limiting to stop abuse; auth/API keys for privileged operations.
  • Validate/sanitize input URLs; prevent open redirect and SSRF.
  • Use HTTPS (encrypt in transit); hash secrets; least privilege.
  • Prevent enumeration (unpredictable short codes).
flowchart LR
    U[User] --> R[Rate limiter]
    R --> A[App + auth]
    A -->|validate URL| H[Hash generator]
    H --> D[(URLs DB)]
    U -->|HTTPS| A
Design an authentication/authorization system (SSO)

Why it's asked here: auth is the core security design question.

Key points to discuss:

  • Tokens (JWT) vs sessions; sign with strong keys; short expiry + refresh.
  • Encrypt credentials at rest (bcrypt/argon2); TLS in transit.
  • OAuth2/OIDC flows for third-party login (SSO).
  • Least privilege and scopes for authorization.
flowchart LR
    U[User] --> ID[Identity Provider]
    ID -->|OAuth2 / OIDC| A[App]
    A -->|JWT| RES[Resource]
    A -->|validate| ID

Further reading


Authentication/Authorization System (SSO) Full Design

Assume the system supports 100 million registered users, 20 million daily logins, and a peak of 20,000 authentication requests per second. Functional requirements are to allow password, MFA, and federated third-party login; issue tokens; support SSO across many service providers; validate access tokens; and enforce authorization scopes. Non-functional requirements include low-latency login, high availability, replay and forgery resistance, horizontal scalability, and auditability. User, client, and token records are small enough for SQL, while login events and audit logs become large and should be partitioned by time.

The high-level architecture centers on a central identity provider and authorization server. A user tries to reach a service provider, which redirects unauthenticated requests to the identity provider. The identity provider authenticates the user directly or through an OAuth2/OIDC flow with a third-party provider, creates a session or authorization code, and returns the user to the service provider. The service provider exchanges the one-time authorization code for an ID token, a short-lived access JWT, and a refresh token. The service provider and downstream resource servers validate the access JWT against the identity provider JWKS endpoint and enforce scopes. This lets many applications share one login system while keeping user credentials centralized.

The data model is split by access pattern. A relational database stores users, credential hashes, registered OAuth clients, user grants or consents, refresh token hashes, and audit events because these records require uniqueness, indexed lookups, and transactional updates. Passwords are never stored in plaintext; they are hashed with bcrypt or argon2. Refresh tokens are stored as opaque hashes rather than raw values, so a leaked database cannot replay them. Redis or another fast store caches hot token metadata, token revocation lists, and short-lived authorization codes because these are high-volume and latency-sensitive. Signing private keys live in a KMS or HSM, not in the application database. A separate append-only log or partitioned table stores authentication events for rate limiting and audit.

The core mechanism is the OAuth2 authorization code flow with PKCE and OIDC layering. The identity provider validates credentials by comparing a hash and optionally verifies a TOTP or WebAuthn MFA claim. It then issues a short-lived authorization code bound to the client ID, redirect URI, and PKCE code verifier. The client exchanges that code for tokens. The access JWT is signed with RS256 or ES256, contains iss, sub, aud, exp, jti, and scope claims, and expires quickly. Resource servers validate the signature using the identity provider public JWKS, check expiration, issuer, audience, and scopes, and reject revoked tokens by consulting a cached revocation list or token introspection endpoint. Refresh tokens have a longer lifetime and can be rotated on each use.

For scaling and availability, the identity provider tier is stateless behind a load balancer and can run many replicas in multiple availability zones. The relational database is replicated for read-heavy profile and client lookups, and the user table can be sharded by user ID if necessary. Redis is sharded and replicated for ephemeral codes, session state, and revocation lists. Signing keys are rotated with a key ID so validators can fetch and cache multiple public keys. Brute-force and credential-stuffing attempts are reduced with rate limiting, failed-login tracking, and optional bot detection. A third-party identity provider outage is isolated by keeping local password or fallback login paths available, and the system can issue shorter-lived tokens during degraded mode.

Important trade-offs include stateless JWT validation versus immediate revocation, centralization versus fault isolation, and security versus latency. Stateless JWTs scale well but require a revocation strategy, such as short expiry plus a distributed denylist. Opaque refresh tokens make revocation easier but add a database lookup. Centrally managed credentials reduce duplicated login logic but make the identity provider a critical dependency, so it must be replicated. Password hashing with a high bcrypt or argon2 work factor slows attackers but also adds login latency. Federation through third-party providers improves user experience but adds trust, availability, and data-consistency risks.

flowchart LR
    U[User Browser] -->|HTTPS| SP[Service Provider / App]
    SP -->|redirect login| IDP[Identity Provider / Authorization Server]
    IDP -->|credential check| DB[(SQL User and Client DB)]
    IDP -->|MFA check| MFA[MFA Service]
    IDP -->|sign tokens| KMS[KMS / Signing Keys]
    IDP -->|code or token| SP
    SP -->|access JWT| API[Resource Server]
    API -->|JWKS validation| IDP
    IDP --> C[(Redis Codes / Revocation / Sessions)]