Microservices Design Patterns
What is a Microservice ? How do you create one ??
A microservice is an architectural style where a single application is developed as a collection of small, autonomous services. Each service runs independently, communicating with clients and other services using lightweight protocols like messaging or HTTP.
Microservices architecture has revolutionized modern application development. By breaking down monolithic applications into smaller, independent services, microservices enable agility, scalability, and faster releases. However, this approach also introduces challenges that need to be addressed. Enter microservices design patterns! These patterns provide tried-and-true solutions to common issues in designing, deploying, and maintaining microservices-based systems.
How to create one:
Start with a Monolith:
- Begin by building a monolithic application that solves a specific problem (e.g., online pizza ordering).
- Identify the different capabilities required (e.g., managing pizza lists, handling payments, scheduling deliveries).
- As features evolve, consider breaking down the monolith into smaller services.
Design Microservices:
领英推è
- Model microservices using domain analysis and tactical domain-driven design (DDD).
- Define service boundaries based on business capabilities within bounded contexts.
- Each microservice should be self-contained and independently deployable.
Microservices Architecture with 12 factor approach
- Codebase: One codebase tracked in revision control, many deploys
- Dependencies: Explicitly declare and isolate dependencies
- Config: Store config in the environment
- Backing Services: Treat backing services as attached resources
- Build, release, run: Strictly separate build and run stages
- Processes: Execute the app as one or more stateless processes
- Port Binding: Export services via port binding
- Concurrency: Scale out via the process model
- Disposability: Maximize robustness with fast start-up and graceful shutdown
- Dev/Prod Parity: Keep development, staging, and production as similar as possible
- Logs: Treat logs as event streams
- Admin Processes: Run admin/management tasks as one-off processes
Common Microservices Design Patterns
- Database per service pattern: Each microservice has its own database, ensuring data isolation.
- Saga pattern: Manages distributed transactions across multiple services.
- API gateway pattern: The API Gateway acts as a central router, handling traffic between clients and microservices.
- Aggregator design pattern: Instead of making separate requests to each service, an Aggregator Microservice calls these services, collects their responses, and consolidates the information into a single, unified response for the user.
- Circuit breaker design pattern: Dynamically manages service availability by temporarily interrupting requests to failing services, preventing system overload and ensuring graceful degradation in distributed environments.
- Command query responsibility segregation (CQRS): It is an architectural pattern that separates read and write operations in a system. It distinguishes between commands (which modify data) and queries (which retrieve data), allowing for optimized handling of each type of operation.
- Asynchronous messaging: It is a communication pattern where messages are sent and received independently of the sender and receiver. It allows systems to handle tasks concurrently, improving scalability and responsiveness.
Details explanation in the subsequent posts