Security basics for system design
Overview
Security isn’t a component you add at the end; it’s a property of every layer. In design interviews you’re rarely asked to design a security system, but a design that exposes private files or trusts every internal call raises flags. This lesson covers the essentials you should mention and get right.
Defence in depth
No single control is perfect, so layer them. If the WAF misses an attack, input validation catches it; if a service is compromised, least-privilege credentials limit what it can reach; if data is stolen, encryption limits its value.
Encryption
- In transit: TLS for every connection, including between internal services and to databases. A service mesh can provide mutual TLS (mTLS), where both sides present certificates, giving encryption and service identity.
- At rest: encrypt disks, databases, backups and object storage (usually a checkbox with managed services), with keys in a key management service (KMS). For especially sensitive fields, add application-level encryption or tokenisation (payment card data is typically handled by a PCI-compliant provider so you never store it).
Authentication
Proving who a user or service is:
- Users sign in through an identity provider using OAuth 2.0 and OpenID Connect, with MFA for sensitive accounts. Don’t build password storage yourself if you can avoid it; if you must, hash with a slow algorithm such as bcrypt, scrypt or Argon2.
- After sign-in, clients carry a token: a short-lived access token (often a signed JWT) plus a refresh token, or a session ID in a secure, HTTP-only cookie (week 1, stateless services).
- Services authenticate to each other with mTLS certificates or signed service tokens, not shared passwords.
- The gateway can validate tokens once and pass a trusted identity downstream.
Authorisation
Deciding what an authenticated identity may do. This is where many real breaches happen:
- Check on every request and every object.
GET /files/981must verify that the caller can access file 981, not just that they’re logged in. Missing object-level checks (IDOR) are among the most common API vulnerabilities. - Use clear models: role-based (admin, editor, viewer) or attribute/relationship-based (owner, shared-with, same organisation).
- Enforce in the service that owns the data, close to the data.
Least privilege and secrets
- Each service gets its own credentials with only the permissions it needs (its own database user, a scoped cloud role).
- Keep secrets (API keys, database passwords) in a secrets manager (Vault, AWS Secrets Manager), never in code or Git, and rotate them.
- Limit and audit human access to production.
Input and output
- Validate input (types, sizes, formats) at the boundary and again where it matters.
- Use parameterised queries to prevent SQL injection; escape output to prevent cross-site scripting.
- Limit request and upload sizes; scan uploaded files; never trust file names or content types from clients.
- Protect against server-side request forgery when your server fetches user-supplied URLs.
Abuse and availability
- DDoS protection and a WAF at the edge (CDN providers offer both).
- Rate limiting per user, key and IP; stricter limits on login and sign-up.
- Bot detection and CAPTCHAs for sign-up, login and checkout abuse.
Privacy and audit
Collect only the personal data you need, know where it lives, support deletion requests, and respect data-residency rules (GDPR, India’s DPDP Act and similar laws). Keep audit logs of sensitive actions (who accessed what, when) in tamper-resistant storage.
In an interview
A short, specific sentence goes a long way: “All traffic is TLS, with mTLS inside; the gateway validates OAuth tokens; services check object ownership on every request; files live in a private bucket served via presigned URLs; secrets are in a vault; login is rate-limited.”
Recap
Secure designs layer defences at the edge, gateway, services and data. Encrypt in transit and at rest, authenticate users with standard protocols and short-lived tokens, authorise every request against the specific object, give every component least privilege with secrets in a vault, validate input, and protect against abuse with rate limits, WAF and DDoS protection.
Secure a file-sharing API
Review this design for a file-sharing app and list the security fixes: (1) the mobile app calls GET /files/{fileId} with a JWT; the server checks the JWT signature and returns the file; (2) files are stored in a public S3 bucket so downloads are fast; (3) the database password is in the service's config file in the Git repository; (4) internal services call each other over plain HTTP inside the VPC; (5) there's no limit on login attempts.
(1)
(2)
(3)
(4)
(5)(1) Authentication isn't authorisation: also check that the user owns the file or has
been granted access (object-level authorisation). Otherwise anyone can change the ID
and read other people's files (IDOR). Use unguessable IDs too, but never rely on them alone.
(2) Make the bucket private (block public access). Serve downloads with short-lived
presigned URLs or signed CDN URLs issued after the permission check.
(3) Remove the secret from Git (and rotate it - it's compromised). Store secrets in a
secrets manager/vault, inject at runtime, give each service its own least-privilege
database user.
(4) Use TLS between services (mTLS via a service mesh gives encryption plus service
identity). Don't trust the network perimeter alone (zero trust).
(5) Rate-limit login attempts per account and per IP, add lockout/backoff and CAPTCHA
after failures, support MFA, and alert on credential-stuffing patterns.Summary
- Defence in depth: layer protections at the edge, gateway, services and data so one failure isn't a breach.
- Encrypt in transit (TLS everywhere, including inside the network) and at rest; manage keys and secrets in a vault.
- Authentication proves identity (OAuth 2.0 / OpenID Connect, tokens, MFA); authorisation checks what that identity may do on every request and object.
- Validate input, use parameterised queries, and apply least privilege to every service and person.
- Protect against abuse with rate limits, WAF and DDoS protection, and keep audit logs.