Microservices… Microservices… Microservices…
You may think they are independent? Probably you were told that microservices are independent. Well… indeed, they are. Each is an independent deployment unit. This should be a separate architectural quantum.
It has its own domain, has high cohesion, has its own database, and sometimes lives in separate repositories.
But all those things don’t make your microservices independent automatically!
In the real world microservices have lots of coupling. As the authors of Software Architecture: The Hard Parts say, without that, they would be useless.
They have lots of dependencies - database, message broker, file system, libraries, other architectural quanta.
And what happens when something changes? Database upgrade - some SQL queries may stop working. Broker upgrade - you were using a deprecated feature in your broker, and now you get exceptions. Full file system storage - rejected writes. Other microservices do not answer - the business process stops working. Deploying a microservice requires coordination with the deployment of another microservice.
If something is really separated, it doesn’t really mean that it is.
Types of couplings
Dynamic coupling and static coupling. Those are two that we should know.
Those couplings describe two different types of dependencies.
Dynamic coupling is dependence on things at runtime that are needed to complete the business process. For example: to buy something in an online shop - imagine two microservices, an order microservice and a payment microservice. The order microservice has a use case buy, and when it is fired, it requires the payment microservice to satisfy your payment in advance. You see it? It was required during runtime.
Static coupling involves dependencies on contracts, source code, etc. A structural dependency. Imagine the example above; here the dependency is not that another service exists and works but when you understand a structure of response, and the target service understands your request.
Where can I see dynamic coupling in the real world?
- Imagine that you are depending on someone’s app (e.g. a REST HTTP API): you need this to complete the flow; because of a network error, it may fail; in a while it may be up - without anyone’s action.
- Imagine that your service has a database - the situation may be the same as in the REST HTTP API example, same network errors, up and down. The database system is required to complete the flow, even if the contracts between the app and the database work.
Reducing static coupling is also possible by API versioning and depending on stable public contracts or an anti-corruption layer. You may limit it, but there may still be dynamic coupling.
Not caring about such coupling may lead to very unstable systems that fail all the time - this is not the target of systems that we are building.
Static coupling
Let’s begin with what it is exactly:
- Depending on a contract with another service (contract coupling)
- Depending on a database schema (schema coupling)
- Depending on implementation details (code coupling)
- Shared library with a domain model inside (shared library coupling)
- Calculating properties on particular properties (semantic coupling - domain knowledge leak)
- Services require parallel deployment (deployment coupling)
- Services that have configuration that must be the same as in other services (e.g. success code -
1,9,3; fail codes -2,7,4; and both services must have the same configuration) (configuration coupling)
Static coupling is something that may break our system, usually after someone’s interaction - configuration change, database schema change, deployment.
How to address these problems? Well…
- Contract coupling - anti-corruption layer, using stable APIs and it’s best when another service takes versioning into account or uses the contract-expand pattern
- Schema coupling - do not poke another service’s database and use the contract-expand pattern to make changes
- Code coupling - dependency inversion principle
- Shared library coupling - do not share domain; share mechanics - like logging, authorization or telemetry
- Semantic coupling - move rules and calculations to the domain and leave them only there; rely on calculated values
- Deployment coupling - use the contract-expand pattern, contract versioning or feature flags; keep backward compatibility
- Configuration coupling - move configuration to the domain application; leave it there and rely on provided stable values; do not interpret more
Those things are static. Fixing them involves action (implementation, deployment), and action has a cost. When something is wrongly designed, that leads to production failures - those may be expensive to fix. Sometimes you may encounter bigger problems that will force your service to be down longer than the SLA allows.
You may think about reducing the number of coupling points. But is it a good way? Not really, you need them (without dependencies your system would be useless). The point is to take control over them:
- Do I need such coupling?
- Does my app require live data or is eventual consistency ok?
- Microservices allow us to have duplicated data across their databases - can I replicate them? Or does something block me?
- Do I need a shared library? Is it ok that my microservices will have different configurations? Will I control it?
- Is the implementation of saga/outbox/inbox worth its cost?
- Why do I have to poke another system’s database? Are boundaries ok?
You have to ask yourself lots of questions to decide. Coupling is not bad. It’s bad when you don’t have control over it.
Dynamic coupling
Imagine three microservices, each with its own database and deployment:
Orders, Stock, Payment
And imagine that you want to place an order in an e-shop. What happens when the stock service stops responding? No order will be placed. The business is not earning money. But does it have to be a synchronous call? Or can it be asynchronous, and are we ok with eventual consistency?
- When a synchronous call (e.g. HTTP) is required, then we have to deal with downtime - this is the cost of the requirement
- When eventual consistency is ok (e.g. send a Rabbit message - it may be the case when selling immaterial things)
You have to weigh the cost.
What if you don’t want downtime and a synchronous call is required? Is a synchronous call really required? Maybe changing the approach is possible?
Here are some other examples of dynamic coupling:
- A Redis cache is required to complete the business process; if you use
try/catchand make the Redis flow optional, then you reduce dynamic coupling cuz it is optional - Database - it is a dynamic dependency cuz it is needed to complete the business process
- Identity Provider - when it is necessary to communicate always with it to authenticate the user (you can make this dependency looser by making, for example, a JWT validatable with a key/certificate that is downloaded and cached when the app first starts)
- Many others… Did you get it?
This is something that must be present during runtime and may change without deploying the app (your or dependent).
As you can see, making microservices independently deployable doesn’t make your microservices independent per se but operationally dependent.
Why it matters
You may already see that talking about coupling is not just fun. It’s not something that we can talk about for fun and then say the real world is the real world and just do what is comfortable. You should always keep in mind that it is a cost that has to be weighed continuously.
Sometimes it is ok to take responsibility for high coupling. You may own some configuration panels that are rarely used; gathering data for them synchronously from other microservices may be ok. Dealing with events may have a higher cost. Then it is ok.
It’s not about fancy and elegant code, but there are:
- Maintenance cost - e.g. when you have semantic coupling, you may be forced to change the logic that doesn’t belong to you, so you don’t really know how to do that. It’s not your domain.
- Reduced blast radius of application failure - properly implementing eventual consistency may give you applications that will be more resistant to failures
- Independent deployments - you want to have independent deployments. Without that you will cause downtime in your app - this is especially a problem when your app is 0-downtime. Buuut is HTTP API versioning for back office frontends needed? It has a cost; maybe deploying applications together is justified (but this is an independent decision of engineers against architecture at that moment)
- You don’t want to coordinate many teams because of deployments - you may wait for someone’s readiness; this is uncomfortable. Rollback may require action by two teams. This requires some agreement between teams - this is expensive.
Please note: some architectural designs are really big red flags – not only trade-offs (e.g. changing one microservice requires changes in another microservice).
What I do before any change
And this is where the hard work begins. Talking about coupling may be fun, but making decisions against it (the right ones!) is hard.
There’s not a single answer - we have to analyze our problems - each is different!
- Identify dependencies - who talks to whom?
- What static coupling do I have? How to reduce it?
- What dynamic coupling do I have? How to reduce it?
- Where is the bounded context?
Is reducing coupling required? Does the cost justify the needs? You may take the responsibility.
Do I have the possibility to use asynchronous communication? Or is synchronous communication required? Is eventual consistency ok? Or is atomic consistency required?
If the cost is worth it, I always prefer asynchronous communication with eventual consistency - this is fault tolerant.
What if I’m forced to introduce semantic coupling?
- Do I have control over the API I’m integrating with?
- May I change it easily? May I change it somehow?
- What’s the deadline? Is there time to fix that? If not, what are the trade-offs of owning it? When there is no time, maybe it is better to schedule a change after release, if it is possible and relatively easy to change?
- Or maybe the cost of maintaining this is justified by its value? And we can own it? Will it cause problems for us in future development?
No single answer. Instead identify dependencies, understand coupling between them (what is the dimension of them?), then check trade-offs (what we gain, what we lose?). Answering those questions will allow you to choose what problems you want to own, and what problems you don’t want.
TL;DR
You don’t want to eliminate couplings. You want to take control over them.
If you eliminated all of them, you would create a system that is useless!
You have to know what dependencies are in your systems. What coupling is there between them? What type, what does it involve? What do I gain? What do I lose? May I own it?
Those are simple questions that will provide you with a weapon to fight with your system and lead to a better understanding of what you have, what you are building. What problems you will have and where.
Next time someone asks you where to put this feature, this property or how to build a flow - you will know which path to take and what the best option is.
Ps. Inspiration for this article is a book: Software Architecture: The Hard Parts written by Neal Ford, Mark Richards, Pramod Sadalage, and Zhamak Dehghani.
Comments
Join the conversation using your GitHub account. You can also browse the discussions on GitHub.