Skip to content

Event Driven Architecture Doesn’t Eliminate Coupling

Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.

Learn more about Software Architecture & Design.
Join thousands of developers getting weekly updates to increase your understanding of software architecture and design concepts.


So you make what seems like a harmless change to an event that you’re publishing. Suddenly, a consumer of that event starts behaving incorrectly, and you’re left wondering, “I thought event driven architecture was supposed to prevent these issues. We replace direct API calls with events, put a message broker in the middle, and now our services can evolve independently, right?”

YouTube

Check out my YouTube channel, where I post all kinds of content on Software Architecture & Design, including this video showing everything in this post.

Loose Coupling Doesn’t Mean No Coupling

Well, not necessarily. Don’t get me wrong, I’m a big advocate for event driven architecture. It can reduce temporal coupling, invert the direction of dependencies, and add resilience. But just because you have one service no longer directly calling another service doesn’t mean they don’t depend on each other.

Let’s go over some of those hidden dependencies that are actually coupling you and how you can avoid them.

One of the biggest reasons developers adopt event driven architecture is to reduce coupling.

Let’s use a transportation logistics system as an example. Say we have a service called Tracking that publishes an event indicating where a vehicle is related to a shipment.

We could have one boundary, like Notifications, that wants to notify the customer via email about where their shipment is. We could have another boundary related to Dispatch that needs to calculate ETAs. And we could have another boundary related to Billing for invoicing our customers and paying our carriers based on how the shipment is actually progressing.

The benefit here is that Tracking doesn’t need to be aware of Notifications, Dispatch, or Billing, and it doesn’t have to invoke them directly. Rather, it can just publish an event, and whatever consumer wants to consume that event can.

Tracking is completely oblivious to any other parts of our system that actually care about the event it’s publishing.

Now, a lot of people say that event driven architecture promotes loose coupling. Sure, but we need to make the distinction that loose coupling does not mean no coupling.

Changing the Direction of Coupling

So what are we actually doing? Well, the first thing we’re doing is changing the direction of the coupling.

If you think about a command, we have a sender that wants to invoke something, and to do that, it needs to know who to call.

If you’re thinking about something in process, you have a method calling another method or a function calling another function. If you’re thinking about services, where you have one service calling another service, it needs to know who it’s calling.

What we’re doing with events is flipping that around.

Now the sender is the publisher, and it’s not saying who it needs to call. It’s just publishing an event that something happened. We have subscribers, whether those are other methods or other services doesn’t really matter. They’re the ones listening, consuming those events, and reacting to what happened.

What we’ve done is inverted the dependency, but we didn’t remove the coupling.

It’s important to understand this because changing the direction of the coupling also changes its characteristics, some of which you might not even be thinking about.

Reducing Temporal Coupling

But let’s look at another benefit of event driven architecture, which is reducing temporal coupling.

Think about a synchronous interaction where Tracking is calling Dispatch directly. Whether that’s in a monolith, in process and in memory, or one service calling another over a network, it’s the same idea. We have a request and response where we’re expecting a response.

When we’re asynchronous, however, Tracking places an event on our message broker, and Dispatch picks it up whenever it does. We’re not expecting a response.

We’re designing it that way. Tracking is putting the message on the broker topic, and whether Dispatch picks it up immediately, fails because it’s having an issue, or isn’t available at any given time, that’s not Tracking’s concern.

Because of this, you get the added benefit of how you want to deal with failures and the added resilience that comes with it.

But again, events don’t remove coupling. They change its characteristics, and some of those characteristics might not be as obvious.

Semantic Coupling: What Does an Event Actually Mean?

One of those characteristics is the semantics of an event.

Let’s say we have an event called ShipmentArrived. It’s supposed to describe something that actually happened in our system.

Our Dispatch boundary publishes this event, but the question is, what does ShipmentArrived actually mean from a consumer’s point of view?

When exactly does it happen?

Does it happen when the vehicle enters a geofence around the pickup or destination? Does it mean that the actual vehicle and trailer are at the gate of the warehouse?

What does ShipmentArrived actually mean?

Consumers need to understand what an event means and when it’s occurring. That’s what the event is supposed to convey to them. If you change that meaning, it can have a significant impact on those consumers.

Take Billing, for example. It really matters to them when they’re invoicing the customer and paying the carrier based on that event.

Let’s say initially ShipmentArrived was always published when the truck physically got to the gate of the warehouse.

But later on, you implement geofencing. Now the event gets published when the truck enters a geofence that’s five miles away from the warehouse. The truck might get caught in traffic and not actually arrive at the warehouse for another hour, even though the event has already been published.

Well, this now has a significant impact on what happens in Billing.

Typically, when people think about events and contracts, they’re thinking about versioning. If I need different fields or the structure has to change, then I have to version the event. That’s my contract, kind of like an API.

Yes, it is. But semantics also matter. What does the event actually mean, and behaviorally, when and why is it published?

In this example, you could argue that it’s not an Arrived event at all. Entering a geofence is something completely different from actually arriving at the warehouse.

Semantically and behaviorally, we shouldn’t even be using that existing event to represent both things.

Behavioral Coupling: When the Workflow Changes

I also want to touch on behavioral coupling a little bit because we often have workflows.

In our shipment example, we arrive at the pickup, pick up the freight, load it, depart, and start going to the destination.

Our consumers might need to react to different events throughout that workflow.

Guess what that means? They’re coupled to it.

They know the sequence in which things are supposed to happen. If we change that sequence or add some other events as part of it, how does that affect them?

A consumer might be expecting one event to occur before another. It might be relying on that sequence to determine what it should do next.

If we change the workflow, we’ve potentially changed the behavior those consumers depend on, even if we haven’t changed the structure of a single event.

So while event driven architecture can reduce temporal coupling, we still have these other characteristics like semantic and behavioral coupling that we need to be aware of.

Not All Events Are Used for the Same Purpose

But there’s another big issue people have with event driven architecture, and it really comes down to misunderstanding or conflating how they’re actually using events.

I want to reference a webinar by Chris Simon, hosted by Particular Software, called “It’s like 10,000 streams when what you need is a queue.”

It really gets into this because different infrastructure serves different purposes. If you’re using the wrong infrastructure for what you’re actually trying to accomplish, you’re going to be in a pile of pain.

Maybe you want to be using a queue or topic, or maybe you want an event log for event streaming, like Kafka. Or maybe what you actually need is an event store.

They all have different utility, and they’re for different purposes.

There’s a big difference between using business events as part of workflow and coordination versus thinking about events from a state perspective.

If you’re thinking about event sourcing, you’re recording the events that actually happened to get us to where we are at any given point in time. Those events are effectively what you’re using to represent the state of your system.

That’s very different from using business events for workflow and coordination.

And that’s also very different from using events for data propagation, where you’re trying to move data around based on what’s changing, maybe for projections or reporting purposes.

These all have different utility. They’re not all the same thing just because you’re using an event.

How you’re using events will define the characteristics of coupling you have.

The same applies to other forms of data distribution.

For example, maybe you’re using CDC (Change Data Capture), where you’re publishing data changes from your database.

Guess what the characteristic of coupling is there?

You’re coupling consumers to the schema of your database.

Now you have consumers that are very aware of what your actual internal database schema looks like. If you make changes to that schema, you potentially affect every consumer that depends on those changes being published.

Just because the data is being published through events doesn’t mean those consumers are independent of the database schema.

Boundaries and Contracts Determine Coupling

Which brings us back to the point that events don’t eliminate coupling. Message brokers don’t eliminate coupling.

We can pretend that all of a sudden, because we’re using events, we’re no longer coupled. But the reality is that we still are.

What determines how our system is coupled is boundaries and contracts.

Event driven architecture has a ton of benefits. We can reduce temporal coupling, invert the dependency so that publishers are totally unaware of their consumers, and get the benefit of how we want to deal with errors and faults, including the resilience that comes with asynchronous communication.

There’s a lot of value there.

But it’s not about thinking we’re completely removing coupling. It’s about managing it and being aware of the different forms and characteristics of coupling we actually have.

The question isn’t whether your system is coupled. It’s what are you coupled to, and how will changes affect those dependencies?

Join CodeOpinon!
Developer-level members of my Patreon or YouTube channel get access to a private Discord server to chat with other developers about Software Architecture and Design and access to source code for any working demo application I post on my blog or YouTube. Check out my Patreon or YouTube Membership for more info.