Microservice Architecture and Its 10 Most Important Design Patterns
This article explains microservice architecture, outlines its ten essential design patterns—including database per service, event sourcing, CQRS, saga, BFF, API gateway, strangler, circuit breaker, externalized configuration, and consumer‑driven contract testing—detailing their advantages, drawbacks, usage scenarios, and typical technology stacks.
1. Microservice Architecture
Microservice architecture decomposes a large, complex system into a set of small, independently deployable services that communicate via lightweight synchronous (e.g., REST, gRPC) or asynchronous (e.g., messaging) network calls.
Key Features
The application is split into independent processes, each containing multiple internal modules.
Services are vertically sliced by business capability rather than shared modules.
Boundaries are external; communication happens over the network.
Each service runs as an independent process and can be deployed separately.
Communication is lightweight and does not require a smart integration layer.
Advantages
Better development scalability.
Faster development cycles.
Supports iterative or incremental development.
Leverages modern cloud, container, DevOps, and serverless ecosystems.
Enables horizontal and fine‑grained scaling.
Reduces cognitive load for developers.
Disadvantages
Significantly more services, databases, processes, containers, and frameworks to manage.
Complexity shifts from code to infrastructure.
Large increase in RPC calls and network traffic.
System‑wide security management becomes more challenging.
Overall system design becomes harder.
Introduces distributed‑system complexities.
When to Use
Large‑scale web applications.
Enterprise projects requiring cross‑team collaboration.
Long‑term ROI outweighs short‑term costs.
Teams have architects or senior engineers capable of designing microservices.
2. Design Patterns for Microservices
2.1 Database per Microservice (Dedicated Database)
Each service owns its own logical data store, avoiding strong coupling at the database layer. Physical databases may be shared, but schemas are isolated.
Pros
Data ownership is clear and isolated per service.
Coupling between development teams is reduced.
Cons
Sharing data across services becomes more complex.
Maintaining ACID transactions across services is difficult.
Designing the data‑split for a legacy monolith is challenging.
When to Use / Not Use
Use in large enterprise applications where independent scaling and development are needed.
Avoid for small applications or when a single team owns all services.
Technology Examples
Any SQL or NoSQL database that supports logical separation (separate tables, collections, or schemas).
2.2 Event Sourcing
Instead of persisting the current state, every state‑changing event is stored immutably. Services can rebuild state by replaying events.
Pros
Provides atomic operations for highly scalable systems.
Automatically records change history with temporal queries.
Enables loosely coupled, event‑driven microservices.
Cons
Reading state requires replaying events or a separate read model (CQRS).
Overall system complexity increases; often requires domain‑driven design.
Must handle idempotency, event loss, and evolving event schemas.
When to Use / Not Use
Use for high‑throughput, scalable transactional systems (SQL or NoSQL) and event‑driven architectures.
Avoid for low‑scale transactional systems or when simple synchronous APIs suffice.
Technology Examples
EventStoreDB, Apache Kafka, Confluent Cloud, AWS Kinesis, Azure Event Hub, GCP Pub/Sub, MongoDB, Cassandra, DynamoDB; frameworks such as Lagom, Akka, Spring, Axon, Eventuate.
2.3 Command‑Query Responsibility Segregation (CQRS)
CQRS separates the write side (commands) from the read side (queries). Simple CQRS uses separate models; advanced CQRS pairs with Event Sourcing and distinct write/read stores.
Pros
Enforces single‑responsibility principle and clear separation of concerns.
Allows independent scaling of read and write workloads.
Cons
Read store is eventually consistent, adding latency.
System complexity grows; misuse can jeopardize projects.
When to Use / Not Use
Use in highly scalable microservices, especially when read traffic dominates.
Avoid for simple applications with balanced read/write loads.
Technology Examples
Write stores: EventStoreDB, Kafka, DynamoDB, relational databases. Read stores: Elasticsearch, Solr, Cloud Spanner, Aurora, Neo4j.
2.4 Saga
Saga implements distributed transactions as a sequence of local transactions, each publishing an event that triggers the next step. Compensating transactions roll back previous steps on failure.
Pros
Provides consistency for highly scalable, loosely coupled systems.
Works with databases that do not support two‑phase commit.
Cons
Requires handling of instant failures and ensuring idempotency.
Debugging is difficult; complexity grows with service count.
When to Use / Not Use
Use when event‑driven microservices need transactional guarantees.
Avoid for low‑scale relational systems or when services have circular dependencies.
Technology Examples
Axon, Eventuate, Narayana.
2.5 Backend‑for‑Frontend (BFF)
BFF creates a dedicated backend for each UI (web, mobile, TV) to tailor APIs, improve security, and reduce UI‑to‑service chatter.
Pros
Separates concerns per UI, allowing UI‑specific optimisations.
Enhances security by isolating downstream services.
Reduces frequent cross‑service calls from the UI.
Cons
Potential code duplication across BFFs.
Management overhead for many BFFs.
Must avoid embedding business logic; only UI‑specific behaviour.
When to Use / Not Use
Use when multiple UIs have distinct API needs or when extra security layers are required.
Avoid if all UIs share the same API or if core services are not behind a DMZ.
Technology Examples
Any backend framework (Node.js, Spring, Django, Laravel, Flask, Play, …).
2.6 API Gateway
API Gateway sits between clients and microservices, acting as a façade, routing requests, aggregating responses, and handling cross‑cutting concerns such as SSL termination, authentication, rate‑limiting, and logging.
Pros
Provides loose coupling between front‑end and back‑end services.
Reduces the number of client‑to‑service calls.
Enables centralized security, logging, and monitoring.
Cons
Can become a single point of failure.
Introduces additional network latency.
May become a performance bottleneck if not scaled.
Increases maintenance and development cost.
When to Use / Not Use
Use in complex microservice landscapes and large enterprises needing centralized control.
Avoid for small projects or when the number of services is minimal.
Technology Examples
Amazon API Gateway, Azure API Management, Apigee, Kong, WSO2 API Manager.
2.7 Strangler
Strangler pattern incrementally replaces parts of a legacy monolith with new microservices, routing traffic via a façade (often an API gateway) until the monolith can be retired.
Pros
Enables safe migration of a monolith to microservices.
Allows parallel development of new features and migration.
Provides control over migration pace.
Cons
Sharing data between legacy and new services can be difficult.
Facade adds latency.
End‑to‑end testing becomes more complex.
When to Use / Not Use
Use for large backend monoliths needing incremental migration.
Avoid for small monoliths where a full rewrite is simpler, or when traffic cannot be intercepted.
Technology Examples
Any API‑gateway‑backed framework.
2.8 Circuit Breaker
Circuit Breaker monitors failure rates of remote calls and short‑circuits requests when a threshold is exceeded, preventing cascading failures.
Pros
Improves fault tolerance and resilience.
Prevents cascading failures across services.
Cons
Requires sophisticated exception handling and monitoring.
Needs manual reset or automated recovery logic.
When to Use / Not Use
Use in tightly coupled, synchronous microservice calls or when a service depends on many others.
Avoid in loosely coupled, event‑driven systems or when a service has no downstream dependencies.
Technology Examples
API gateways, service meshes, libraries such as Hystrix, Resilience4j, Polly.
2.9 Externalized Configuration
Configuration values (databases, credentials, endpoints) are stored outside the application artifact and injected at runtime, reducing security risk and rebuilds.
Pros
Production credentials are not in source code, lowering security exposure.
Changes to configuration do not require rebuilding the binary.
Cons
Requires a framework that supports external configuration sources.
When to Use / Not Use
Use for any important production application.
Avoid for quick proof‑of‑concept or prototype code.
Technology Examples
All modern enterprise frameworks (Spring Boot, Micronaut, .NET Core, etc.) provide this capability.
2.10 Consumer‑Driven Contract Testing
Consumers define expectations (requests and responses or messages) as contracts; providers run these contracts as part of their test suite to ensure compatibility.
Pros
Detects breaking changes in provider APIs quickly.
Improves robustness in large, multi‑team microservice ecosystems.
Enhances team autonomy.
Cons
Requires extra effort to create and maintain contracts on both sides.
If contracts diverge from real behaviour, production failures may occur.
When to Use / Not Use
Use in large enterprises where many teams own different services.
Avoid for small, single‑team applications or when services are stable and rarely change.
Technology Examples
Pact, Postman, Spring Cloud Contract.
3. Summary
Microservice architecture enables large‑scale enterprise development by improving scalability, speed, and alignment with modern cloud‑native tooling, but it is not a universal silver bullet. Teams should adopt it only when the benefits outweigh the added operational and architectural complexity, and they should follow proven design patterns—dedicated databases, event sourcing, CQRS, saga, BFF, API gateway, circuit breaker, Strangler, externalized configuration, and consumer‑driven contract testing—to build robust, maintainable systems.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
