🌐 Microservices Architecture & Distributed Systems
1️⃣ Monolith vs Microservices & Conway's Law
Definition: Microservices Architecture ek aisi software design approach hai jisme ek single large application ko multiple independently deployable, loosely-coupled aur domain-focused small services me break kiya jata hai jo standard protocols (REST, gRPC, Event Streams) par baat karti hain.
| Criteria | Monolithic Architecture | Microservices Architecture |
|---|---|---|
| Deployment | Single large artifact (JAR/WAR/Binary). Single change par poora app redeploy hota hai. | Independent CI/CD per service. Teams ek doosre ko block kiye bina deploy karti hain. |
| Scaling | Vertical scaling ya pure monolith ko horizontally duplicate karna padta hai. | Selective scaling: Sirf heavy-load service (e.g. Payment) ko scale karo. |
| Tech Stack | Single language & framework lock-in. | Polyglot: Service A Node.js me, Service B Go me, Service C Python me. |
| Data Consistency | Strong consistency (Single ACID database). | Eventual consistency (Distributed databases). |
2️⃣ The API Gateway Pattern
Clients (Web, Mobile Apps) ko dozens of internal microservices ke private IP addresses expose karne ke bajay ek unified reverse-proxy entry point provide karna:
- Request Routing:
/api/v1/ordersko Order Service par aur/api/v1/usersko Auth Service par route karna. - Centralized Authentication: JWT token verification aur rate limiting gateway level par hi check karna.
- SSL Termination: Public HTTPS certificates gateway par decrypt karna taaki internal cluster me ultra-fast latency mile.
- Popular Gateways: Kong, Envoy Proxy, Spring Cloud Gateway, NGINX, AWS API Gateway.
3️⃣ Service Discovery & Dynamic Registration
Containers dynamically restart aur scale hote rehte hain, jisse unke IP addresses badalte hain. Service Discovery ek internal phonebook ki tarah kaam karta hai:
# Service Registry Workflow:
1. Order Service pod boots up with IP 10.244.1.45
2. Registers itself to Service Registry (e.g. HashiCorp Consul / Netflix Eureka)
3. Payment Service queries Registry: "Where is Order Service?"
4. Registry returns healthy instance IPs
5. Client-side load balancing (Round-Robin) picks an IP to dispatch request
4️⃣ Database-per-Service & The Saga Pattern
Microservices me do services kabhi bhi single shared database share nahi karti (Loose coupling rule). Multiple databases me transactions manage karne ke liye 2-Phase Commit (2PC) ke bajay Saga Pattern use hota hai:
• Choreography: Har service event publish karti hai jise agli service listen karke aage badhti hai (No central controller).
• Orchestration: Ek dedicated central Orchestrator service decide karti hai ki kounsa task execute hoga.
• Compensating Transaction: Agar Step 3 (Payment) fail hota hai, toh Orchestrator Step 2 (Inventory Reserve) ko cancel karne ke liye rollback/compensating event shoot karta hai.
5️⃣ Circuit Breaker Pattern (Fault Tolerance)
Agar koi downstream service slow ya down ho jaye, toh calling service ke saare threads block hone se bachane ke liye Circuit Breaker use hota hai (e.g. Resilience4j):
| State | System Behavior |
|---|---|
| CLOSED | Normal operations. Saari requests downstream service tak pass hoti hain. Errors count hote hain. |
| OPEN | Failure threshold (e.g. 50% failures) cross hote hi circuit trip ho jata hai. Requests instantly fail-fast hoti hain bina slow service ko call kiye. |
| HALF-OPEN | Cool-down period ke baad trial requests bhejkar check karta hai ki kya downstream service recover hui ya nahi. |
6️⃣ Asynchronous Event-Driven Messaging
| Criteria | Synchronous (REST / gRPC) | Asynchronous (Kafka / RabbitMQ) |
|---|---|---|
| Coupling | Tightly coupled (Caller blocks until response is received). | Decoupled (Producer sends event and immediately moves on). |
| Availability | Agar receiver down hai toh request fail ho jati hai. | Receiver down hone par message queue me safely buffer rehta hai. |
| Best Fit | User-facing read queries (Get User Profile). | Order placement, notifications, analytics telemetry. |
7️⃣ Distributed Tracing & Observability
Jab ek user click 10 alag-alag microservices ko trigger karta hai, toh bottlenecks debug karne ke liye Distributed Tracing mandatory hai:
# HTTP Trace Header Propagation (W3C Trace Context):
traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
├── Trace ID: 4bf92f3577b34da6a3ce929d0e0e4736 (Global transaction identifier)
└── Span ID : 00f067aa0ba902b7 (Current microservice step duration)
Observability Stack: OpenTelemetry (Instrumentation) + Jaeger/Zipkin (Tracing) + Prometheus (Metrics) + Grafana (Visual Dashboards).