Skip to content
Tutorials Center
My learning

Topics

Lesson 40 of 65 ยท Reliability and Operations

Security for System Design

Security essentials for system design: authentication versus authorization, tokens, RBAC and ABAC, encryption, secrets, defense in depth and threat modeling, with Python demos.

Security is not a feature you add at the end. It shapes where services sit, how they talk and what data they keep. In design discussions, you are expected to show that you considered who can do what, how that is verified, and what happens when something is breached. This lesson gives you the vocabulary and the standard building blocks.

Authentication versus authorization

  • Authentication (authn) answers who are you? It verifies identity: a password, a one-time code, a passkey, a signed token.
  • Authorization (authz) answers what are you allowed to do? It decides whether the identified caller may perform a specific action on a specific resource.

A system can authenticate you perfectly and still be insecure if it forgets to check authorization on each request. The classic bug is IDOR (insecure direct object reference): GET /invoices/1042 works for any logged-in user because the server never checks that invoice 1042 belongs to them. Always authorize on the server, for every request, using the identity from the verified credentials, never from a field the client supplies.

Authentication in distributed systems

Three common mechanisms:

  • Sessions: after login the server stores a session and gives the browser a random session ID cookie. Simple and revocable, but the session store must be shared across servers.
  • Tokens (such as JWT): the server issues a signed token containing claims (sub, exp, role). Any service can verify the signature without calling the auth server, which suits microservices. Downsides: they are hard to revoke before expiry, so keep access tokens short-lived (minutes) and use refresh tokens.
  • OAuth 2.0 / OpenID Connect: a standard for "log in with X" and for delegating limited access to third parties. The user authenticates with an identity provider, and your app receives tokens instead of passwords.
Python Signed tokens: verify, expire and tamper

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Note that the token is signed, not encrypted: anyone holding it can read the claims. Never put secrets in it.

Passwords

Never store passwords or compare them in plain text. Store a slow, salted hash made with a purpose-built algorithm (Argon2id, bcrypt or scrypt). A salt is a random value per user that makes identical passwords hash differently and defeats precomputed (rainbow) tables; slowness makes guessing expensive. Add rate limiting and lockouts on login, multi-factor authentication, and breached-password checks.

The demo below uses SHA-256 repeated many times as a stand-in only to show salts and slowness; in real systems use a vetted library, not hand-made hashing.

Python Why salts matter (stand-in hash for illustration)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Authorization models

RBAC (role-based access control): permissions attach to roles (viewer, editor, admin), and users get roles. Simple, auditable and enough for most apps. It struggles when rules depend on context: "editors can edit only their own team's documents during business hours."

ABAC (attribute-based access control): decisions are computed from attributes of the subject (department, clearance), the resource (owner, sensitivity), the action and the environment (time, location, device). Policies are rules over attributes, so ABAC expresses fine-grained, contextual rules and scales without a role explosion, at the cost of more complex policy management and testing. Other models include ACLs (a list per resource) and relationship-based systems like Google Zanzibar (permissions from relationships such as "member of folder owner group").

Python RBAC and ABAC side by side

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Protecting data and traffic

  • Encryption in transit: use TLS everywhere, including between internal services. Terminate TLS at the edge or load balancer for public traffic; use mutual TLS (mTLS) so services authenticate each other.
  • Encryption at rest: encrypt disks and databases; for sensitive fields use application-level encryption with keys from a KMS (key management service). Rotate keys.
  • Secrets management: never commit keys or passwords. Keep them in a secrets manager (Vault, cloud secret stores) and inject at runtime. Rotate them and give each service its own credentials.
  • Least privilege: each service, person and token gets the minimum permissions needed, for the shortest time.
  • Input validation and output encoding: validate everything from outside (SQL injection, XSS, SSRF, path traversal). Use parameterized queries.
  • Auditing: log security-relevant events (logins, permission changes, data exports) in tamper-resistant storage.

Defense in depth at the architecture level

Layers of protection

Edge CDN, WAF, DDoS shield, rate limits.
Gateway Authenticate, validate, throttle.
Services Authorize per request, mTLS.
Data Encrypt, least-privilege access, backups.

Assume any single layer will eventually be bypassed.

1 / 2

Place public entry points in a DMZ, keep databases in private subnets with no public IP, and control traffic between tiers with security groups or network policies. An API gateway centralizes authentication, rate limiting and request validation. Use a WAF for common web attacks and put rate limiting in front of login and expensive endpoints.

Threat modeling in an interview

A simple, structured way to bring security into any design:

  1. Identify assets: user data, payments, credentials.
  2. Identify actors: anonymous users, authenticated users, other services, administrators, attackers.
  3. Walk the data flow and ask "what could go wrong here?" using STRIDE: Spoofing, Tampering, Repudiation, Information disclosure, Denial of service, Elevation of privilege.
  4. Pick controls: authentication, authorization, encryption, validation, rate limits, logging.
  5. Plan for breach: detection, containment, key rotation and recovery.

Do not invent your own cryptography or token format in production. Use well-reviewed libraries and standards, and keep them updated. Most real breaches come from misconfiguration, missing authorization checks and leaked credentials, not from broken ciphers.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A logged-in user changes the invoice ID in a URL and sees someone else's invoice. What is missing?

    The user was authenticated, but the server never checked that the invoice belongs to them. This is an insecure direct object reference.
  2. 2. Why are JWT access tokens usually short-lived?

    Services verify signatures without asking the auth server, so a stolen token stays valid until expiry. Short lifetimes limit the damage.
  3. 3. When is ABAC a better fit than RBAC?

    ABAC evaluates policies over attributes of the user, resource and environment, which captures contextual rules that roles cannot.
  4. 4. What does a per-user salt do?

    A random salt per user ensures one cracked hash does not reveal others and rainbow tables do not help.
  5. 5. What is the purpose of mTLS between services?

    In mutual TLS the client also presents a certificate, so services authenticate each other.

Summary

  • Authentication proves identity; authorization decides permissions and must be checked server-side on every request.
  • Use sessions or short-lived signed tokens; use OAuth/OIDC for delegated access; hash passwords with salted slow algorithms.
  • RBAC is simple; ABAC handles contextual rules; relationship-based models suit sharing graphs.
  • Encrypt in transit and at rest, manage secrets centrally, apply least privilege and audit everything important.
  • Defend in layers and use a simple threat model (assets, actors, STRIDE, controls) in every design.

Next: LSM Trees and B-Trees