Design a secure architecture for a distributed microservices system
A secure microservices architecture applies defense-in-depth and zero-trust: authenticate and authorize every request, encrypt traffic end to end, isolate the network, and centralize secrets + audit. No request is trusted by virtue of its origin.
Infrastructure, 15 components, about 40 min
1. Scope
In scope
- • User auth (OAuth/OIDC + JWT)
- • Service-to-service auth (mTLS)
- • Network isolation
- • Secrets management
- • Audit logging
Out of scope
- • Application bug classes (XSS/SQLi) deep-dive
- • Compliance certification process
- • Endpoint security
2. Functional requirements
Must have
- • Authenticate every external request
- • Authorize access per service/resource
- • Encrypt all traffic
- • Manage + rotate secrets
- • Audit security events
Nice to have
- • Token revocation
- • Fine-grained RBAC/ABAC
- • Anomaly detection
3. Non-functional requirements
4. Back-of-envelope estimation
Assumptions
- • Many microservices in private subnets
- • JWT validated at gateway + services
- • mTLS via service mesh
- • Secrets in Vault with rotation
Derived numbers
5. API design
OIDC login → issue JWT access/refresh tokens
Carry JWT (user) + mTLS (service identity)
6. High-level design
What each box in the reference diagram is responsible for:
7. Data model
8. Deep dives
Zero-trust
Traditional perimeter security trusts anything "inside" the network. Zero-trust assumes the network is hostile: every request — even service-to-service — must prove identity (authn) and permission (authz). A breached service can't freely move laterally because each hop is independently verified.
User vs service identity
Two distinct mechanisms: users authenticate via OAuth 2.0/OIDC and carry a short-lived JWT validated at the gateway and downstream services. Services authenticate to each other via mTLS — each holds a certificate (issued/rotated by the service mesh) proving its identity, so a rogue process can't impersonate a service.
Network isolation & blast radius
Services run in private subnets (no public IPs); sensitive ones (payments) in further-isolated subnets with strict firewall rules. If one service is compromised, segmentation limits what it can reach — minimizing blast radius.
Secrets & audit
No secrets in code or env files. A central manager (Vault) issues short-lived, rotated credentials and encrypts them. Every authn/authz decision and sensitive action is logged immutably to a SIEM for monitoring, alerting, and compliance — so breaches are detectable and auditable.
9. Bottlenecks
- Auth as SPOF/latency → local JWT validation + cached JWKS
- mTLS overhead → service mesh handles it transparently
- Token revocation → short TTLs + blacklist
- Audit volume → stream to SIEM, sample non-critical
10. Trade-offs
Perimeter or zero trust
Choose: Zero trust: every request authenticated and authorized regardless of network location, with mTLS between services.
Instead of: Trusted internal network: simpler, but one breached host can reach everything.
Most breaches move laterally; zero trust shrinks the blast radius.
Token format
Choose: Short-lived signed JWTs carry the user's identity on every call (services prove their own identity with mTLS), with opaque refresh tokens stored server-side.
Instead of: Opaque tokens checked against a central store on every call: instant revocation, more latency and a dependency.
Short lifetimes bound the revocation gap while keeping calls local.
Secrets distribution
Choose: A secrets manager issuing short-lived, dynamically generated credentials (for example per-service database users).
Instead of: Static secrets in environment files: leak once, valid forever.
Short-lived credentials make a leak expire on its own.
11. Failure modes
Signing key leaked
Attacker can mint valid tokens.
Mitigation: Keys in an HSM or KMS, key ids in tokens, rotation with overlap, and an emergency rotation runbook.
Certificate authority outage
Services cannot renew mTLS certificates and calls start failing.
Mitigation: Certificates valid for days with renewal starting at half life; alert well before expiry.
Over-broad service permissions
A compromised service reads data it never needed.
Mitigation: Least-privilege policies per service, reviewed automatically from observed access.
12. What interviewers probe
Answer each one yourself before opening it.
How does a request carry the end user's identity through five services?Show a strong answer
Propagate a signed token with the user and the calling service chain; each service authorizes on both, not on network origin.
Where do audit logs go and how are they protected?Show a strong answer
Shipped off-host to an append-only store (WORM) with restricted access; tamper evidence via hashing chains.
How do you detect a compromised service?Show a strong answer
Baseline normal call graphs and data access; alert on new destinations, unusual volumes or privilege use.
How do you rotate a database password without downtime?Show a strong answer
Two valid credentials at once: create the new one, roll services, then revoke the old.
Sign in to track this guide in your curriculum progress.