Microservices is an architectural style that structures an application as a collection of small, autonomous services, each running in its own process and communicating through well-defined APIs. Each microservice is focused on a specific business capability and can be developed, deployed, and scaled independently.
┌──────────────────────────────────────┐
│ Monolithic Application │
│ │
│ ┌────────────────────────────────┐ │
│ │ │ │
│ │ User Interface Layer │ │
│ │ │ │
│ ├────────────────────────────────┤ │
│ │ │ │
│ │ Business Logic Layer │ │
│ │ • Auth • Orders • Payment │ │
│ │ • Inventory • Shipping │ │
│ │ │ │
│ ├────────────────────────────────┤ │
│ │ │ │
│ │ Data Access Layer │ │
│ │ │ │
│ └────────────────────────────────┘ │
│ ▼ │
│ ┌────────────────────────────────┐ │
│ │ Single Database │ │
│ └────────────────────────────────┘ │
└──────────────────────────────────────┘
Single Deployment Unit
┌─────────────────────────────────────────────────────────┐
│ Microservices Application │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌────────┐ │
│ │ Auth │ │ Orders │ │ Payment │ │Shipping│ │
│ │ Service │ │ Service │ │ Service │ │Service │ │
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ └───┬────┘ │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │Auth DB │ │Order DB │ │ Pay DB │ │Ship DB │ │
│ └─────────┘ └─────────┘ └─────────┘ └─────────┘ │
│ │
│ Each service: Independent, Deployable, Scalable │
└─────────────────────────────────────────────────────────┘
| Aspect | Monolithic | Microservices |
|---|---|---|
| Deployment | Single unit | Independent services |
| Scalability | Scale entire app | Scale specific services |
| Technology | Single stack | Polyglot (multiple languages) |
| Development | Single team | Multiple teams |
| Failure | Entire app down | Isolated failures |
| Updates | Redeploy all | Update individual services |
| Complexity | Lower initially | Higher overall |
| Data | Shared database | Database per service |
| Testing | Simpler initially | Requires integration testing |
| Performance | In-process calls | Network calls (overhead) |
Each service focuses on one business capability:
✅ Good:
- Order Service: Handles order processing only
- Payment Service: Handles payments only
- Inventory Service: Manages inventory only
❌ Bad:
- Monolithic Service: Handles orders, payments, inventory, shipping
Services are independent and communicate via APIs:
Service A ←→ API ←→ Service B
↓ ↓
DB A DB B
Related functionality grouped together within a service.
Each service can be:
- Developed independently
- Deployed independently
- Scaled independently
- Failed independently
Each service owns its database:
# Order Service
apiVersion: apps/v1
kind: Deployment
metadata:
name: order-service
spec:
template:
spec:
containers:
- name: order-api
image: order-service:v1
env:
- name: DATABASE_URL
value: "postgres://order-db:5432/orders"
---
# Payment Service (separate DB)
apiVersion: apps/v1
kind: Deployment
metadata:
name: payment-service
spec:
template:
spec:
containers:
- name: payment-api
image: payment-service:v1
env:
- name: DATABASE_URL
value: "postgres://payment-db:5432/payments"┌─────────────┐ ┌─────────────┐
│ Order │─────REST────▶ │ Payment │
│ Service │ │ Service │
│ │◀────Response───│ │
└─────────────┘ └─────────────┘
Example: REST API
apiVersion: v1
kind: Service
metadata:
name: payment-service
spec:
selector:
app: payment
ports:
- port: 80
targetPort: 8080
type: ClusterIP# Order service calls payment service
curl http://payment-service/api/process-payment \
-d '{"amount": 100, "currency": "USD"}'┌─────────────┐ ┌──────────┐ ┌─────────────┐
│ Order │────▶│ Message │────▶│ Inventory │
│ Service │ │ Queue │ │ Service │
└─────────────┘ └──────────┘ └─────────────┘
Example: RabbitMQ/Kafka
# Order service publishes event
apiVersion: apps/v1
kind: Deployment
metadata:
name: order-service
spec:
template:
spec:
containers:
- name: order-api
image: order-service:v1
env:
- name: RABBITMQ_URL
value: "amqp://rabbitmq:5672"┌─────────────────────────────────────┐
│ Service Mesh │
│ (Istio, Linkerd) │
│ │
│ • Traffic Management │
│ • Security (mTLS) │
│ • Observability │
│ • Load Balancing │
└─────────────────────────────────────┘
┌─────────────┐
Clients ───────▶│ API Gateway │
└──────┬──────┘
│
┌────────────────┼────────────────┐
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│ Service │ │ Service │ │ Service │
│ A │ │ B │ │ C │
└──────────┘ └──────────┘ └──────────┘
Benefits:
- Single entry point
- Authentication/Authorization
- Rate limiting
- Request routing
Kubernetes Example:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: api-gateway
spec:
rules:
- host: api.example.com
http:
paths:
- path: /orders
pathType: Prefix
backend:
service:
name: order-service
port:
number: 80
- path: /payments
pathType: Prefix
backend:
service:
name: payment-service
port:
number: 80┌───────────────┐ ┌───────────────┐
│ Order │ │ Payment │
│ Service │ │ Service │
└───────┬───────┘ └───────┬───────┘
│ │
▼ ▼
┌───────────────┐ ┌───────────────┐
│ Order │ │ Payment │
│ Database │ │ Database │
└───────────────┘ └───────────────┘
Benefits:
- Data isolation
- Independent scaling
- Technology choice per service
Service A ──┐
├──▶ Circuit Breaker ──▶ Service B
Service C ──┘
States:
• Closed: Normal operation
• Open: Service B is down, fail fast
• Half-Open: Testing if Service B recovered
Example: Using Istio
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: payment-circuit-breaker
spec:
host: payment-service
trafficPolicy:
outlierDetection:
consecutive5xxErrors: 5
interval: 30s
baseEjectionTime: 30s┌─────────────────────────────────┐
│ Service Registry │
│ (Kubernetes DNS, Consul) │
└──────────────┬──────────────────┘
│
┌──────────┼──────────┐
▼ ▼ ▼
┌────────┐ ┌────────┐ ┌────────┐
│Service │ │Service │ │Service │
│ A │ │ B │ │ C │
└────────┘ └────────┘ └────────┘
Kubernetes Built-in Service Discovery:
# Services are discoverable via DNS
curl http://payment-service.default.svc.cluster.localOrder ─▶ Payment ─▶ Inventory ─▶ Shipping
│ │ │ │
│ Success │ Success │ Success │
└─────────┴───────────┴────────────┘
If any fails:
Order ◀─ Compensate ◀─ Compensate ◀─ Failed
# Order Service
apiVersion: apps/v1
kind: Deployment
metadata:
name: order-service
labels:
app: order
tier: backend
spec:
replicas: 3
selector:
matchLabels:
app: order
template:
metadata:
labels:
app: order
spec:
containers:
- name: order-api
image: ecommerce/order-service:v1
ports:
- containerPort: 8080
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: order-db-secret
key: url
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "200m"
---
apiVersion: v1
kind: Service
metadata:
name: order-service
spec:
selector:
app: order
ports:
- port: 80
targetPort: 8080
type: ClusterIP
---
# Payment Service
apiVersion: apps/v1
kind: Deployment
metadata:
name: payment-service
labels:
app: payment
tier: backend
spec:
replicas: 2
selector:
matchLabels:
app: payment
template:
metadata:
labels:
app: payment
spec:
containers:
- name: payment-api
image: ecommerce/payment-service:v1
ports:
- containerPort: 8080
env:
- name: PAYMENT_GATEWAY_KEY
valueFrom:
secretKeyRef:
name: payment-secret
key: gateway-key
---
apiVersion: v1
kind: Service
metadata:
name: payment-service
spec:
selector:
app: payment
ports:
- port: 80
targetPort: 8080
type: ClusterIP# Scale only the order service
kubectl scale deployment order-service --replicas=10
# Payment service remains at 2 replicas
kubectl scale deployment payment-service --replicas=2# Update order service
kubectl set image deployment/order-service \
order-api=ecommerce/order-service:v2
# Other services unaffected# Order Service: Node.js
containers:
- name: order-api
image: node:16-alpine
# Payment Service: Go
containers:
- name: payment-api
image: golang:1.19-alpine
# Inventory Service: Python
containers:
- name: inventory-api
image: python:3.10-slimIf Payment Service crashes:
✅ Order Service: Still running
✅ Inventory Service: Still running
❌ Payment Service: Kubernetes auto-restarts
# Liveness and readiness probes
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5- More services to manage
- Complex inter-service communication
- Distributed system challenges
- No ACID transactions across services
- Eventual consistency
- Need for Saga pattern
- Service-to-service calls over network
- Cascading failures
- Need for timeouts and retries
- Integration testing across services
- End-to-end testing
- Contract testing
- More deployments
- Monitoring and logging complexity
- Service mesh configuration
# Health checks
livenessProbe:
httpGet:
path: /health
# Timeouts
readinessProbe:
timeoutSeconds: 5
# Retry logic in application code# Prometheus monitoring
apiVersion: v1
kind: Service
metadata:
name: order-service
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "8080"
prometheus.io/path: "/metrics"# Install Istio
istioctl install
# Enable automatic sidecar injection
kubectl label namespace default istio-injection=enabled# Use Ingress or API Gateway
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: api-gateway
annotations:
nginx.ingress.kubernetes.io/rate-limit: "100"# Configuration
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
LOG_LEVEL: "info"
# Secrets
apiVersion: v1
kind: Secret
metadata:
name: db-credentials
type: Opaque
data:
username: YWRtaW4=
password: cGFzc3dvcmQxMjM=- Large, complex applications
- Multiple teams working independently
- Need for different scaling requirements
- Long-term project with evolving requirements
- Different technology stacks needed
- Small, simple applications
- Small team (< 5 people)
- Tight deadlines
- Limited operational expertise
- No clear service boundaries
┌────────────────────────────────────────┐
│ Why Kubernetes for Microservices? │
├────────────────────────────────────────┤
│ ✅ Service discovery (DNS) │
│ ✅ Load balancing (Services) │
│ ✅ Self-healing (Controllers) │
│ ✅ Scaling (HPA, VPA) │
│ ✅ Rolling updates (Deployments) │
│ ✅ Configuration (ConfigMaps/Secrets) │
│ ✅ Storage (Persistent Volumes) │
│ ✅ Networking (CNI, Network Policies) │
└────────────────────────────────────────┘
- Martin Fowler on Microservices
- 12-Factor App
- Kubernetes Services
- Kubernetes Deployments
- Service Mesh (Istio, Linkerd)
- Kubernetes Networking
- Deploying Applications
- Services and Load Balancing
- ConfigMaps and Secrets Management
Next Steps: Learn about Kubernetes Services and Deployment strategies for microservices.