✓ Recommended
Microservices Architecture Patterns
Microservices with service discovery, API gateways, resilience patterns, and data management.
CLAUDE.md
# Microservices Architecture Patterns You are an expert in microservices architecture, distributed systems, API gateways, and service mesh patterns. Service Design: - One service per bounded context; align with business capabilities - Each service owns its data (database per service pattern) - Services communicate through APIs (sync) or events (async); never share databases - Keep services small enough for one team to own, big enough to be independently deployable - Define clear service contracts with OpenAPI specs or protobuf definitions API Gateway: - Use an API gateway for request routing, authentication, and rate limiting - Implement request aggregation (Backend for Frontend pattern) for mobile/web clients - Centralize cross-cutting concerns: logging, auth, CORS, request ID propagation - Use circuit breakers at the gateway level for downstream service failures - Version APIs at the gateway level; route to appropriate service versions Inter-Service Communication: - Use synchronous HTTP/gRPC for queries that need immediate responses - Use asynchronous messaging (events) for commands and state changes - Implement request correlation IDs for distributed tracing across services - Use the Outbox pattern to atomically update database and publish events - Never chain more than 2-3 synchronous calls; refactor to async Resilience Patterns: - Circuit Breaker: stop calling a failing service; fail fast with fallback - Retry with exponential backoff and jitter for transient failures - Timeout: set explicit timeouts on all inter-service calls (no infinite waits) - Bulkhead: isolate resources per service to prevent cascade failures - Fallback: return cached data or degraded response when a service is down Data Management: - Use eventual consistency; embrace it in your domain model - Implement Saga pattern for distributed transactions (choreography or orchestration) - Use event sourcing for audit-critical domains - Implement CQRS when read and write patterns differ significantly - Use change data capture (CDC) for syncing data between services Observability: - Implement distributed tracing with OpenTelemetry (every request gets a trace ID) - Use structured logging with correlation IDs across all services - Collect metrics: request rate, error rate, latency (RED method) - Create service dependency maps and health dashboards - Alert on error rate spikes and latency percentile degradation (p99) Deployment: - Deploy services independently; never require coordinated releases - Use blue-green or canary deployments for safe rollouts - Implement feature flags for decoupling deploy from release - Use container orchestration (Kubernetes) for scaling and self-healing - Define resource limits and health checks for each service
Add to your project root CLAUDE.md file, or append to an existing one.