AgentSkillsCN

microservices-patterns

设计微服务架构,明确服务边界,采用事件驱动通信机制,并融入韧性设计模式。 适用场景:在构建分布式系统、拆分单体架构、实施微服务架构、规划服务边界时使用。

SKILL.md
--- frontmatter
name: microservices-patterns
description: |
  Design microservices architectures with service boundaries, event-driven communication, and resilience patterns.
  Use when: building distributed systems, decomposing monoliths, implementing microservices, designing service boundaries.
triggers: ["microservices", "distributed system", "service mesh", "event-driven", "saga pattern", "circuit breaker"]

Microservices Patterns

Design distributed systems with proper service boundaries, communication patterns, and resilience.

Core Principles

  • Single Responsibility: Each service owns one business capability
  • Data Autonomy: Each service owns its data, no shared databases
  • Loose Coupling: Services communicate through well-defined contracts
  • Failure Isolation: One service failure shouldn't cascade
  • Independent Deployment: Services deploy independently

Quick Reference

PatternPurposeWhen to Use
API GatewaySingle entry pointMultiple clients, aggregation needed
Circuit BreakerPrevent cascade failuresRemote service calls
SagaDistributed transactionsMulti-service business process
Event SourcingAudit trail, replayComplex state transitions
CQRSRead/write optimizationHigh-read or complex queries

Service Decomposition

By Business Capability:

code
├── OrderService       # Order lifecycle
├── PaymentService     # Payment processing
├── InventoryService   # Stock management
├── ShippingService    # Delivery tracking
└── NotificationService # Alerts and emails

By Bounded Context (DDD):

  • Identify ubiquitous language per context
  • Define context boundaries
  • Map relationships between contexts

Communication Patterns

StyleWhen to UseExamples
Synchronous (REST/gRPC)Real-time response neededUser queries, validation
Async Events (Kafka)Fire-and-forget, broadcastOrder placed, payment received
Async Request-ReplyLong operationsReport generation, batch processing

Critical Don'ts

  • Never share databases between services
  • Don't create distributed monoliths (tightly coupled services)
  • Avoid synchronous chains (A→B→C→D)
  • Don't skip compensating transactions in sagas
  • Don't adopt microservices before team is ready

When to Load References

  • For resilience patterns: Read references/resilience-patterns.md
  • For data patterns: Read references/data-patterns.md

External Resources