Microservice design in Serverless architecture needs to fully leverage its stateless and auto-scaling characteristics while paying attention to inter-service communication and coordination:
Design principles:
1. Single responsibility
- Function granularity: Each function does only one thing, keep it simple
- Business boundaries: Divide function boundaries according to business domains
- Reusability: Design reusable common functions
2. Stateless design
- External state: Store state in external services
- Idempotency: Ensure functions can be safely retried
- No side effects: Avoid functions producing unpredictable side effects
3. Event-driven
- Asynchronous communication: Use message queues for asynchronous inter-service communication
- Event sourcing: Record state changes through event streams
- Publish-subscribe: Use event buses for loose coupling
Service communication patterns:
1. Synchronous communication
- API Gateway: Call other services via HTTP/HTTPS
- Applicable scenarios: Scenarios requiring immediate results
- Disadvantages: Cold start latency exists, not suitable for high concurrency
2. Asynchronous communication
- Message queues: Use SQS, Kafka and other message queues
- Applicable scenarios: Long-running tasks, high concurrency scenarios
- Advantages: Decouple services, improve system elasticity
3. Orchestration pattern
- Step Functions: Use state machines to orchestrate multiple functions
- Applicable scenarios: Complex workflows
- Advantages: Visual management, comprehensive error handling
Best practices:
- Service splitting: Reasonably split services, avoid over-splitting
- API design: Design RESTful APIs, keep interfaces simple
- Error handling: Implement comprehensive error handling and retry mechanisms
- Monitoring and tracing: Use distributed tracing to monitor service call chains
Candidates should be able to share microservice architecture design experience from actual projects.