Skip to content

Microservices


Microservices architecture is an approach to software development where an application is built as a collection of small, independent services that communicate over well-defined APIs.

Monolith vs Microservices
=========================
MONOLITH:
+---------------------------------------------------+
| User | Order | Payment | Notification | Product|
+---------------------------------------------------+
MICROSERVICES:
+--------+ +--------+ +--------+ +--------+
| User | | Order | |Payment | |Product |
| Service| | Service| |Service | |Service |
+--------+ +--------+ +--------+ +--------+
| | | |
+------------+------------+------------+
|
v
+-----------------------------------------------+
| Communication Fabric |
| (API Gateway, Service Discovery) |
+-----------------------------------------------+

Each Service is Independent
==========================
Service A Service B Service C
+--------+ +--------+ +--------+
| Deploy | | Deploy | | Deploy |
| Update | | Update | | Update |
| Scale | | Scale | | Scale |
+--------+ +--------+ +--------+
| | |
v v v
Can be down Can be down Can be down
independently independently independently
Database Per Service
===================
User Service Order Service Payment Service
+--------+ +--------+ +--------+
| PostgreSQL | PostgreSQL | PostgreSQL
| (Users DB) | (Orders DB) | (Payments DB)
+--------+ +--------+ +--------+
Benefits:
- Loose coupling
- Independent scaling
- Technology flexibility
- Failure isolation

12.3.1 Synchronous Communication (REST/gRPC)

Section titled “12.3.1 Synchronous Communication (REST/gRPC)”
Synchronous Communication
=========================
Client API Gateway User Service
| | |
| Request | |
|------------->| |
| | Forward |
| |----------------->|
| | |
| | Response |
| |<-----------------|
| Response | |
|<-------------| |
Example:
GET /api/users/123/orders

12.3.2 Asynchronous Communication (Message Queues)

Section titled “12.3.2 Asynchronous Communication (Message Queues)”
Asynchronous Communication
=========================
Order Service Message Queue Inventory Service
| | |
| Publish "Order | |
| Created" event | |
|---------------------->| |
| | Deliver message |
| |----------------------->|
| Return "Order | |
| Accepted" | |
|<----------------------| |
Process asynchronously
(can be slow)

API Gateway
===========
+--------------------------------------------------+
| API Gateway |
+--------------------------------------------------+
| - Request routing |
| - Authentication |
| - Rate limiting |
| - Protocol translation |
| - Response aggregation |
| - Logging |
+--------------------------------------------------+
| | | |
v v v v
+----+ +----+ +----+ +----+
|User| |Order| |Pay | |Prod|
|Svc | |Svc | |Svc | |Svc |
+----+ +----+ +----+ +----+
Popular API Gateways:
- Kong
- NGINX
- AWS API Gateway
- Apigee
- Traefik

Service Discovery
=================
+--------------------------------------------------+
| Service Registry |
+--------------------------------------------------+
| Service A: 10.0.1.5:8080 |
| Service B: 10.0.1.6:8080 |
| Service B: 10.0.1.7:8080 |
+--------------------------------------------------+
Service A wants to call Service B:
1. Query Registry: "Where is Service B?"
2. Registry returns: "10.0.1.6:8080"
3. Service A calls: http://10.0.1.6:8080
Tools:
- Consul
- Eureka
- etcd
- Kubernetes DNS

AdvantageDescription
Independent DeploymentDeploy services separately
Technology DiversityUse different tech stacks
Fault IsolationFailure doesn’t cascade
ScalabilityScale individual services
Team AutonomyTeams own their services
Faster DevelopmentSmaller codebases

ChallengeDescription
Distributed SystemsNetwork failures, latency
Data ConsistencyTransactions across services
TestingIntegration harder
DebuggingDistributed tracing needed
OperationsMore infrastructure
Service ContractsAPI versioning
Microservices Challenges
=======================
1. Network Calls
+--------------------------------------------------+
| Service A -----> Service B -----> Service C |
| | | | |
| Latency Timeout Retry |
+--------------------------------------------------+
2. Data Consistency
+--------------------------------------------------+
| Order Service Inventory Service |
| Deduct item Update stock |
| But if fails? But if fails? |
| Must compensate! Must rollback! |
+--------------------------------------------------+
3. Debugging
+--------------------------------------------------+
| Trace: Request-123 |
| User -> API -> Auth -> Order -> Inventory |
| Need distributed tracing! |
+--------------------------------------------------+

Bounded Contexts
================
E-commerce Domain
+--------------------------------------------------+
| |
| +----------+ +----------+ +----------+ |
| | User | | Order | |Product | |
| | Context | | Context | | Context | |
| +----------+ +----------+ +----------+ |
| | | | |
| +----------------+----------------+ |
| Shared Kernel |
| (Customer ID format) |
+--------------------------------------------------+
Each context = Potential microservice
Decomposition Strategies
=======================
1. By Business Capability
+--------------------------------------------------+
| User | Order | Payment | Shipping |
| Service | Service | Service | Service |
+--------------------------------------------------+
2. By Subdomain (DDD)
+--------------------------------------------------+
| Customer | Order | Billing | Warehouse |
| Domain | Domain | Domain | Domain |
+--------------------------------------------------+
3. By Use Case
+--------------------------------------------------+
| View- | Create- | Update- | Search- |
| Order | Order | Order | Product |
+--------------------------------------------------+

Circuit Breaker Pattern
=======================
Normal State:
Service A -----> Service B (working)
[Circuit: CLOSED]
Failure State:
Service A -----> Service B (timeout)
[Circuit: OPEN - fast fail]
Recovery:
[Circuit: HALF-OPEN - test periodically]
Retry Pattern
=============
Attempt 1: ------> X (fail)
Wait 100ms
Attempt 2: ------> X (fail)
Wait 200ms
Attempt 3: ------> X (fail)
Wait 400ms
Attempt 4: ------> ✓ (success!)
Techniques:
- Exponential backoff
- Jitter (randomization)
- Circuit breaker

Kubernetes Architecture
======================
+--------------------------------------------------+
| Kubernetes Cluster |
+--------------------------------------------------+
Control Plane:
+--------------------------------------------------+
| API Server | Scheduler | Controller | etcd |
+--------------------------------------------------+
Worker Nodes:
+----------+ +----------+ +----------+
| Node 1 | | Node 2 | | Node 3 |
+----------+ +----------+ +----------+
| Pod: A | | Pod: B | | Pod: C |
| Pod: D | | Pod: E | | Pod: F |
+----------+ +----------+ +----------+
K8s handles:
- Deployment
- Scaling
- Service discovery
- Load balancing

ScenarioWhy Microservices
Large teamsIndependent team ownership
Complex domainsClear bounded contexts
High scale needsIndependent scaling
Multiple platformsDifferent tech stacks
Rapid iterationIndependent deploys
ScenarioWhy Monolith
Small teamsComplexity overhead
Simple domainOver-engineering
Startup/MVPMove fast, iterate
Limited scaleDon’t need distribution

Key microservices concepts:

  1. Independent services - Deploy, scale, fail independently
  2. Own their data - Database per service
  3. Communication - Sync (REST/gRPC) or async (messages)
  4. API Gateway - Entry point, routing, auth
  5. Service Discovery - Find services dynamically
  6. Complex but powerful - Trade-off complexity for flexibility
  7. Start with monolith - Extract when needed

Next: Chapter 13: Event-Driven Architecture