Microservices Patterns
API Gateway
Single entry point for all clients. Handles routing, auth, rate limiting, SSL termination, request aggregation. Examples: Kong, AWS API Gateway, Nginx. Avoids client-to-service coupling.
Service Discovery
How services find each other. Client-side: service queries registry (Consul, Eureka). Server-side: load balancer queries registry. DNS-based: Kubernetes services.
Circuit Breaker
Stops calling a failing service after N failures. States: Closed (normal) → Open (fail fast) → Half-Open (test recovery). Prevents cascade failures. Library: Hystrix, Resilience4j.
Saga Pattern
Distributed transaction as a sequence of local transactions + compensating actions. Choreography (events) or Orchestration (coordinator). Replaces 2PC in microservices.
Sidecar Pattern
Helper process alongside main service. Handles cross-cutting concerns: logging, monitoring, TLS, service mesh proxy. Examples: Envoy, Istio sidecar. Deployed as a container.
Bulkhead Pattern
Isolate failures by partitioning resources (thread pools, connection pools, circuits). One failing service doesn't exhaust resources for others. Named after ship bulkheads.
Strangler Fig Pattern
Incrementally replace a monolith. Route traffic to new microservice for specific routes, keep old system for the rest. Gradually strangle the monolith. Low risk migration.
Event-Driven Communication
Services communicate via events (Kafka, RabbitMQ). Loose coupling, async, resilient. Challenge: eventual consistency, event ordering, debugging distributed flows.
Backends for Frontends (BFF)
Separate API layer per client type (web BFF, mobile BFF). Each optimized for its client's needs. Avoids one-size-fits-all API. Used by Netflix, SoundCloud.
Distributed Tracing
Track a request across services using trace ID propagation. Visualize latency per hop. Tools: Jaeger, Zipkin, AWS X-Ray, OpenTelemetry. Essential for debugging microservices.