AI Skill Library

Microservices Patterns

Decomposition, API gateway, saga, circuit breaker, event sourcing, observability.

architecturemicroservicesbackend
# Microservices Patterns

## Decomposition
- By business capability (DDD bounded context).
- Anti-pattern: splitting by technical layer.

## API Gateway
- Single entry: auth, routing, rate limiting, SSL.
- BFF (Backend for Frontend): separate gateway per client type.
- Tools: Kong, AWS API GW, Traefik.

## Communication
- **Sync (HTTP/gRPC)**: request-response, real-time reads.
- **Async (Kafka/RabbitMQ)**: decoupled writes, resilient.

## Saga pattern
- **Choreography**: services emit + react to events.
- **Orchestration**: central coordinator directs steps.
- Each step has a compensating transaction for rollback.

## Circuit breaker
Closed -> Open (failing, fast reject) -> Half-Open (probe).
Prevents cascade failures. Libraries: Resilience4j, opossum (Node).

## Service discovery
- **Client-side**: query registry (Consul), load-balance yourself.
- **Server-side**: LB queries registry (K8s Service).

## Event sourcing
- Store state as append-only event log. Replay to reconstruct.
- Combine with CQRS: write model (events) + read model (projections).

## Observability (3 pillars)
- **Metrics**: Prometheus + Grafana
- **Logs**: structured JSON, correlation IDs (ELK / Loki)
- **Traces**: OpenTelemetry -> Jaeger / Zipkin
Propagate `traceparent` header (W3C Trace Context) across services.

API: /api/skills/microservices-patterns