Loose Coupling and Shared-Nothing

A digression accompanying the Scalability series.

A thick rope laid into a tightly interwoven round knot on grey wooden planks. The opposite of loosely coupled. Photo: Alberto Gasco @ Unsplash - Unsplash license (rotated and cropped)

The team that builds the search service for an online shop wants to add a new field to the search index. Three lines of code, an afternoon’s work. It still takes two weeks before the field is in production.

The product table also belongs to the team next door, so the schema change has to be agreed with them. The shared release isn’t until the Tuesday after next. And before that, someone from a third team wants to look over the change, because it touches “his” library.

Nobody has done anything wrong here. The three lines just depend on things that belong to other people. That is coupling: how much one part of a system has to know about another, agree with it, or wait for it.

The pattern comes in every size. Two threads that share a HashMap behind a synchronized block are tightly coupled. So are two services that write to the same database table. And so are three teams working on the same monolith – whatever the org chart says.

Two ways of depending on each other

Two parts are loosely coupled when they talk to each other only through an agreed interface and everything behind it is nobody else’s business. As long as the interface holds, each side can rebuild its internals without asking the other. Tight and loose are the two ends of a spectrum: the more two parts know about each other and share with each other, the tighter they are coupled. Almost every system sits somewhere in between.

That sounds like a single scale. For scalability there are two, and they have surprisingly little to do with one another: one for the teams that change a system, and one for the requests it processes.

The first is design-time coupling: what do I need to know about the other side, and whom do I have to ask before I change something? Shared code, shared tables and shared releases drive it up. Stable, versioned interfaces bring it down.

The second is runtime coupling: does it depend on another service whether I can get my work done quickly right now? A shared resource creates it: if two services share a database, the load of one shows up in the response times of the other. Every synchronous call creates it too. If service A waits for an answer from service B, A inherits B’s response time and B’s outages, no matter how beautifully the API is documented.

So a REST interface with a stable contract loosens the coupling between the teams, but does nothing about runtime coupling. Caches and circuit breakers ease that to a degree. To get rid of most of it, you need asynchronous communication. In 2003, Eugster and colleagues described for publish/subscribe systems what gets decoupled in the process: sender and receiver do not have to be active at the same time (time decoupling), and neither blocks while the other is busy (synchronization decoupling).

The reverse case exists too. If one service puts its jobs into a table that another works through later, neither waits for the other. But both depend on the schema of that table – neither can change it alone anymore.

This digression is about sharing: what shared tables, shared code and shared databases do to teams and to requests. What synchronous calls do, and when asynchrony is worth it, is covered in the digression Asynchronous Communication and Redundant Data.

Four-quadrant matrix: design-time coupling on the horizontal axis, runtime coupling on the vertical, each running from tight to loose. Bottom left the shared database, bottom right REST with a stable contract, top left the table as a queue, top right events with their own data.

Two axes, four quadrants: a stable contract loosens design-time coupling, a table used as a queue loosens runtime coupling – only events with their own data loosen both.

Shared-nothing: sharing nothing at all

At the far end of the spectrum sits shared-nothing. Michael Stonebraker popularized the term in 1986 for database architectures whose nodes share neither memory nor disks and talk to each other only over the network.

Applied to services, that means: your own database, your own cache, your own deployment pipeline, no shared code. For teams, it means that each one owns its service, infrastructure included, and decides for itself when to ship what.

On a small scale, such a step fits into a single line of code. In Java, Math.random() uses a random number generator that all threads share, and under load they fight over its state. ThreadLocalRandom gives each thread its own generator, and the fight is over. Shared-nothing, pocket-sized.

One size up, the same step looks like this: the search service keeps its own copy of the product data instead of asking the product service on every request. If the product service goes down, search doesn’t notice at first. The price is data that lags behind the original – and the work of keeping it up to date.

What coupling has to do with scalability

Coupling slows things down in two places: the people who change a system, and the requests it processes.

More teams that don’t wait for each other

As a system grows, so does the number of people working on it. At some point, design-time coupling decides whether ten teams get ten times as much done as one – or mostly sit in coordination meetings together.

Shared code and shared data models force shared releases. Every change becomes a consultation, every refactoring a negotiation with the team next door. There is a simple test for this: whom do I have to ask before I can deploy? Every name on that list is a coupling that no architecture diagram shows.

The DORA research, which has spent years measuring what fast-delivering software teams do differently, asks essentially the same question. It calls teams loosely coupled when they can make large-scale changes without permission from outside the team and release independently of other services, and it counts this among the things that predict continuous delivery.

Loose coupling turns the one block that needs coordinating into many units that can work in parallel. If you like, you can read that as Amdahl’s Law for organizations. The law comes from computer architecture and says: the part of the work that cannot be parallelized limits what additional processors can deliver. The shared release is such a part. Every team has to pass through it, one after the other.

More load without everyone queuing

The second place is the requests. And it isn’t sharing as such that slows them down. It is access that lets only one in at a time.

Work that everyone has to pass through one at a time is called the serial fraction. Just 5% of it is enough, and at twenty times the throughput of a single instance you hit the ceiling, however much hardware you add – Amdahl’s Law again, this time for machines. But a shared resource counts toward the serial fraction only to the extent that access to it has to be serialized – by a write lock, say, or a central coordinator. If it is only read, accessed lock-free or partitioned, many can use it at once.

Incidentally, shared-nothing does not make the serial fraction inside a service disappear; each service keeps its own. It removes the shared chokepoint between services, which is exactly the part that sharing created in the first place.

But the serial fraction is not the only effect. A service that has to wait at a shared resource takes longer to answer. By Little’s Law – concurrent requests = throughput × response time – a longer response time at the same throughput means more requests in the system at once. Thread pools and connection pools fill up, even though not one more request is coming in than before.

And then there is the neighbor. If the search service and the order service share a database, both loads land on the same utilization curve, and by Kingman’s formula that curve is anything but linear: the higher the utilization, the more steeply waiting times grow, more than doubling between 80 and 90%. A load peak in search, say after a newsletter, therefore slows down the orders, even though nothing is going on there at all. On top of that, two very different query profiles meet at the shared database, and that mix makes queues grow too, because it makes service times more irregular.

Queuing theory, by the way, has nothing against sharing as such: four checkout lanes with one shared queue are more efficient than four lanes with a queue each, because no lane sits idle while someone is waiting next door. So separate databases are the more expensive option: each needs its own headroom for its own peak, and one database’s headroom is no help to the other. What you get in return is isolation: every service has its own curve, and one service’s peak is no longer the other’s problem.

Three time series stacked on top of each other for search and the order service on a shared database. At the top, the load: search has a peak after a newsletter, the order service stays flat. In the middle, the utilization of the database as a stacked area of both loads, reaching the 90% line at the peak. At the bottom, the response time: at the same moment both curves shoot up, including the order service's.

Shared database: the search peak drives up the shared utilization, and the orders slow down along with it.

The same three time series with separate databases. The load is unchanged. The utilization shows two lines: the search database reaches the 90% line at the peak, the order service's stays flat. For the response time, only the search curve shoots up; the order service's stays flat.

Separate databases: the same peak now slows down only search.

Shopify learned this the hard way. The failure of a single Redis instance shared by all shops once took down the entire platform. Since then, Shopify has run in pods: fully isolated instances, each with its own MySQL, Redis and Memcached.

Sharing gets most expensive when the participants also have to coordinate. In 1996, Jim Gray and colleagues worked out just how expensive: if every database node may write and the copies are reconciled synchronously, the number of deadlocks grows with the cube of the number of nodes if the amount of data stays the same. Ten times as many nodes, each bringing its own traffic, mean a thousand times as many deadlocks. The paper isn’t called “The Dangers of Replication” for nothing.

An ideal, not a dogma

I have yet to see a system that really shares nothing. Somewhere there is always a common network, an identity provider, a load balancer – and team autonomy ends at the coffee machine, if not before. Even the database world, where the term comes from, has gone back to mixing: the cloud database Snowflake, for instance, keeps all data in shared storage and separates only the compute clusters that work on it. So rather than asking whether a system is shared-nothing, I find another question more useful: where are we still sharing, and which of those places is slowing us down right now?

Not sharing isn’t free, after all. If you share nothing, you operate more: more databases, more pipelines, more monitoring. And data that a simple JOIN used to bring together suddenly has to be copied and kept up to date. The step pays off where a shared resource demonstrably slows down teams or requests. But there it makes a difference, long before the system is perfect.

At OTTO, from 2012 on, I saw what happens when you follow the principle through consistently. Each team owned its own subsystem, with its own database, its own deployment and the freedom to choose its own technology. The architecture simply didn’t allow sharing. That was one of the reasons otto.de could grow from 200 to more than 4,000 page views per second without anyone having to touch the basic architecture.

One problem remains, though, even when every service really does have its own database: as soon as services call each other and wait for the answer, they depend on each other again at runtime. Just over the network instead of through a table.


References

  • DORA – Capabilities: Loosely coupled teams. dora.dev.
  • Eugster et al. (2003) – The Many Faces of Publish/Subscribe. ACM Computing Surveys, 35(2), 114–131.
  • Gray et al. (1996) – The Dangers of Replication and a Solution. ACM SIGMOD Record, 25(2), 173–182.
  • Shatrov (2018) – E-Commerce at Scale: Inside Shopify’s Tech Stack. Shopify Engineering.
  • Snowflake – Key Concepts & Architecture. Snowflake Documentation.
  • Stonebraker (1986) – The Case for Shared Nothing. IEEE Database Engineering Bulletin, 9(1), 4–9.

Comments