HLD · Architecture
Microservices Architecture
Decompose a system into independently deployable services with clear boundaries.
1. Monolith vs microservices
| Monolith | Microservices |
|---|---|
| Single codebase & deploy | Many services, independent deploy |
| Simple debugging, shared DB | Team autonomy, polyglot stacks |
| Scale entire app together | Scale hot services only |
| Good for early stage / small teams | Good for large orgs with clear domains |
Interview line: "Start monolith; extract services when team or scaling pain justifies operational cost."
2. Domain-driven service boundaries
Split by business capability, not technical layer.
- User Service — auth, profiles
- Order Service — checkout, order state
- Payment Service — charges, refunds
- Notification Service — email, push
Bad split: "Database service" + "API service" — creates chatty coupling.
3. Communication patterns
Synchronous (HTTP/gRPC)
Order service calls Payment service and waits. Simple mental model; cascading failures if Payment is down.
Mitigation: Timeouts, retries (idempotent ops only), circuit breaker.
Asynchronous (message queue)
Order placed → event to queue → Notification sends email. Loose coupling; harder to trace.
4. Circuit breaker pattern
States: Closed (normal) → Open (fail fast after N errors) → Half-open (trial request).
Example: Recommendation service down — product page loads without recommendations instead of hanging 30s.
5. Data ownership
Each service owns its database. No shared tables across services — access via API/events only.
Example violation: Order service直接 JOINs users table in Payment DB — creates tight coupling.
6. Distributed tracing & observability
One user request spans 5 services. Use correlation ID (trace ID) in headers; tools: Jaeger, Zipkin, OpenTelemetry.
7. Example: Netflix-style decomposition
- Monolith streaming app → extract billing, encoding, recommendations
- Encoding pipeline async via queue (heavy GPU workers)
- API gateway + BFF (backend-for-frontend) per client (TV vs mobile)
- Each team owns SLA for their service
8. Challenges (say these in interviews)
- Network latency & partial failures
- Distributed transactions → sagas (see Saga note)
- DevOps overhead: CI/CD per service, K8s, service mesh
- Testing integration across services
Quick revision
- Split by domain, not by layer; each service owns its data.
- Sync: HTTP/gRPC + timeout + circuit breaker.
- Async: Events/queues for decoupling.
- Don't microservice too early — operational tax is real.
- Observability: Trace IDs across services mandatory.