Distributed Systems- NServiceBus-Couplings- Part1

Barış Tutakli
6 min readFeb 1, 2023

Hi Everyone, today I’m going to talk about Distributed Systems and NServiceBus. To better understand the benefits of NServiceBus, I thought it would be helpful to mention distributed systems and couplings beforehand.

Prerequisites

  • Curiosity😃
  • Patience😅

#HowToReduceCoupling #NServiceBus #Messaging #MessagingPatterns #Saga #Commands #Events

With the evolution of technology and the internet, having new machines and accessing new technologies has become easier. So, instead of relying on a single main machine to handle all client requests, people came up with new ideas to improve their systems.

2 and 3-tiered systems were the actual beginning of distributed systems, which were started by making remote procedure calls. In the 1990s N-tier systems appeared and then services appeared in the 2000s. Having a lot of machines in different places wasn’t sufficient enough because of monolithic projects.

A monolithic project is a project which is charged to do everything. The project has many responsibilities. So, the more responsibility a project has, the harder it is to manage the project. Then debugging and deployment become more challenging, which increases the time required to make changes due to couplings.

After dealing with these problems people decided to separate the responsibilities of each project for better management and maintenance. As a result, numerous applications came into view.

So, what is a distributed system?

A distributed system is a collection of programs(components, nodes, applications such as web applications…) that are physically separated and are connected by a network.

As an example, we can look at microservice architectures, which are a way of building distributed systems by breaking down monolithic projects.

As you see there are multiple applications and somehow they are communicating with each other

In other words, one application depends on the others. This is where couplings come into play. What does coupling mean?

What does coupling mean?

Coupling is the measure of how much an application depends on other applications . Coupling is vital for microservices because too much coupling can negatively impact the future of a project. For example many processes will be affected such as the deployment process, development processes .

Are there different coupling types?

Yep! There are different types of coupling, such as “afferent coupling,” “efferent coupling,” “platform coupling,” “temporal coupling,” and “spatial coupling.”

Let’s go over each of them one by one and give some examples from real world projects.

Afferent coupling, also known as incoming coupling or incoming dependencies, is a measure of how many other applications utilize a specific package.

Efferent coupling, also known as outgoing coupling or outgoing dependencies, is a measure of how many other applications are utilized by a specific package.

Let’s look at the floowing classes:

BookingRepository

BookingService

BookingReporter

As you see BookingService is using booking repository. and also BookingReporter is also using booking repository. So BookingService and BookingReporter have one efferent coupling. BookindRepository has two afferent couplings.

To give another example, suppose that you created the following four applications: Sales, Identity, Shipping, and Consents applications

are making requests to Identity applications to get user profiles etc.

So, Sales application, Shipping application and Consents application have one efferent coupling and Identity application has three afferent couplings.

Platform Coupling

If some components or applications are built for a specific platform. It means that they are coupled to a specific platform or hardware.

Thus, platform coupling is the dependency of a specific platform or hardware.

For instance, consider a scenario where your manager asks you to build an application for the Windows operating system. Then, suddenly our customers wanted to switch to Linux. What will we do? If the application is not cross platform, we’ll have to build the application for Linux. so, This would result in loss of time and money.. As you see, being highly coupled to a specific platform makes us lose money and time. If it was built in .net core language, we’ll be able to use the same application on different os. Thus, we’ll prevent losing money and time.

Temporal Coupling

An application or component is reliant(dependent) on another application for a specific duration, such as a part of the day, week, or year.

For example, suppose that there is an application which runs only between 9:00 am and 10:00 am to read data from one place and write it to another. So, Other applications depend on this application for only one hour, and this is an example of temporal coupling.

Spatial Coupling

This coupling occurs when one application needs to access data or resources of another application.

For instance, suppose that there is a database which is used by many applications. If one manager decides to migrate one table to another place, this change will affect the ability of other projects to access the data.

In a system where there are couplings, the response latency can be affected. So, how could the response latency be affected?

There are many game changers such as firewall, certificates, the geographic location of the caller and backend, current load, etc.

Let’s go over some of them with examples:)

  • If someone accidentally changed some firewalls configuration, all of your applications might be inaccessible from external systems and internal systems.
  • Suppose that every certificate is renewed automatically, however, somehow one vital certificate couldn’t be renewed. What will happen? As the example above, all of your applications might be inaccessible from external and internal systems.
  • What about the current load? If your applications receive too many requests that they can’t handle quickly, it will affect the response latency. The clients might get event timeout exceptions because of the overload.

How to reduce couplings?

Now it’s time to talk about NServiceBus and its benefits. NServiceBus is a messaging library that provides message-based communication infrastructure to build scalable, reliable, maintainable systems. NServiceBus allows applications to communicate with each other in a loosely coupled way.

I mean, how does NserviceBus do that communication in a loosely coupled way?

For example, Suppose that there is one windows service as a consumer of a specific queue and it’s located in a remote server. Then, somehow the service breaks down, what will happen? The messages will be stored in the queue till we start the windows service. Then, it will continue consuming messages. While trying to restart the consumer service, other applications won’t be affected. They will be able to send messages to the queue.

In addition to that NServiceBus provides some features such as error handling, retries message routing, etc. These features decrease errors because suppose that a message couldn’t be handled. It will be resent to the queue depending on the priority level. Then, if the message can’t be handled after some retries, it will be sent to the error queue. Thus, we’ll be able to investigate the cause of the error.

So, what do I mean by saying messages?

There are two different types of messages. These are events and commands.

An event is a result of some processes. By publishing an event, we notify subscribers that some processes are completed. An application can publish an event in different ways such as using an instance of endpoint or using IMessageHandlerContext.

For instance, after creating a user the handler class can publish a UserProfileCreatedEvent to notify other systems.

A command is used to execute an action. An application can send commands in different ways such as using an instance of endpoint or using IMessageHandlerContext.

For example, one application can send a command to create a new user.

In the next artcile, we’ll dive into messaging patterns and provide pratical examples. Then, we will explore the creation of a distributed system using various technologies to showcase a real-world application.

Would you like to help me to better my articles?🙂 Do let me know your view on this 🙂 See you next time! Thank you for reading!

Resources:

--

--