Decoupling Software Components with Events

How Events Make Software Systems More Responsive.

When we talk about building software that's divided into separate services, like microservices or service oriented architecture, there's a common way we set things up. It's pretty straightforward, much like a conversation. This is how we often write code: call some code, wait for it to finish, and then continue. This approach is perfect for many situations we face, especially websites that we interact with directly. You click a button and expect something to happen in response. However, as we start dealing with more and more separate services, things start to shift a bit. The more services there are, the more complex it gets to have all those services talking to each other directly and instantly.

With this basic understanding of software communication, let's explore a principle that strongly influences how we design our systems for effective interaction.

Command and Query Separation (CQS)

The discussion in the article about the importance of having loosely coupled components in software systems brings us to a fundamental principle of system functionality. This principle was clearly articulated by Bertrand Meyer, who pointed out a simple yet profound rule about how methods should behave in programming. He argued that a method should focus on doing one of two things: performing an action, which we call a command, or providing information, known as a query. Importantly, it should avoid trying to do both. The significance of this is that if you're just getting information from a method, you shouldn't inadvertently cause changes in the system. In essence, queries should be just that - queries, without any side effects, ensuring that the system remains unaltered and predictable. Having laid the groundwork with CQS, we turn to the first pillar of this principle: Commands.

Understanding Commands

Commands in software are pretty much like giving a direct order. It's like asking your friend to turn off the lights; you expect him to do just that, to change the state of the room from light to dark. In a software, a command is a message we send to another part of our application, asking it to perform a certain task that will change the way things are. It's kind of like saying, "Hey, I need you to do this task," and then hanging around to see if the task is done.

Here's a simple code example to illustrate commands:

public class LightSwitch
{
    public void TurnOff()
    {
        // Code to turn off the light
        Console.WriteLine("The light has been turned off.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        LightSwitch lightSwitch = new LightSwitch();
        // Sending a command to the LightSwitch
        lightSwitch.TurnOff();
    }
}

In this C# example, LightSwitch is a class that encapsulates the behavior of a light switch. The TurnOff() method is an action or command that changes the state, specifically turning off the light. We create an instance of LightSwitch and call the TurnOf() method on it, effectively sending a command to our "service" to change the state of the system by turning off the light. This is similar to giving a direct command in the real world and waiting for the action to be taken - here represented by the console output indicating that the light has been turned off.

With a clear grasp of commands, it's time to explore the other side of the CQS coin: queries.

The Role of Queries

Queries are all about looking things up without making waves. Think of it as wanting to know if someone has registered as a user without affecting their registration status or any other part of the system. It's just gathering information, plain and simple.

For example, sending a GET request to /users/{userID}, the API will simply return the details of the user associated with that ID. This operation doesn't change the user's data or affect any other part of the system. It's a straightforward, side-effect-free query of system state that perfectly embodies the concept of a query as outlined earlier in the CQS paradigm.

Beyond commands and queries, there's a third critical component in our discussion: events. Let's take a closer look at how events further the goal of component decoupling.

Using Events for Loose Coupling

Events are both a fact and a notification at the same time. Events act as indicators of actions or changes that have occurred. They carry information about these occurrences without anticipating any specific follow-up actions. These events move in a one-way direction, meaning they're sent out without waiting for or expecting any kind of reply—this is often referred to as a "fire and forget" approach. However, a new event might be triggered as a reaction to them, creating a chain of actions initiated by the original event.

This approach is powerful because it allows different parts of the system to listen for these announcements and decide independently if they need to respond. For example, if one service in our software system announces that a new user has signed up, other services can listen to that event. An email service might take action by sending a confirmation, while another service that tracks user statistics might update its records. Neither response is directly commanded by the service that announced the new user; they're optional, based on the needs and designs of the listening services. To see these concepts in action, let's examine how they play out in a microservices architecture, utilizing a practical example.

Practical Example: Microservices and Event-Driven Communication

To illustrate the architecture of three microservices with a message broker to facilitate event-driven communication, the following diagram shows the components and the message broker as an intermediary that handles the distribution of messages (events) between services. This setup is typical in a microservices architecture to achieve loose coupling and scalability.

https://www.plantuml.com/plantuml/png/bP6nQiCm48PtFSMHp7c132KD3LtQ4cewb911Ldximx9axix9KFhkTHoemOL2j0Z1zt_IhzkeUR6-ZSvrFZI-0YrsPoIZ9-5XfyUka-n3lQ0lHyCwV2Za7HMhYJVEgO2SEU18gzK37LwXUfLi9JUG8wrftvUHjORI6ovJ68BJUD5edUTkibthEVxwrrJqfGRX4agMxzTTwSAy3JW7lTVxxLp8RPVJSFZd5vOlNDMsZJqcVDxanZmBa2Pe_huP_OaOysN2_tPSxZarpcsGJkCyVm00
Loosely coupled components using events.

This setup underlines the role of the message broker in decoupling microservices by mediating event communication. The services do not communicate directly with each other, but instead interact through the message broker using events. This approach increases the flexibility, scalability, and maintainability of the system.

While our focus has been on microservices, it's important to note that the principles of commands, queries, and events apply equally to monolithic applications.

Beyond Microservices: Events in Monolithic Applications

It's worth noting that while our discussion has focused on microservices and the use of a message broker to facilitate event-driven communication, these concepts are not exclusive to microservices architectures. You can also apply these principles to decouple components within a monolithic application.

In a monolith, different modules or components can still publish and subscribe to events, even if they're part of the same codebase or deployment unit. Using events to communicate between components helps maintain a level of separation and modularity within the monolith. This approach allows individual parts of the application to remain loosely coupled, making the system easier to understand, extend, and maintain.

Here a simple example written in C#.

using System;

public class NewUserEventArgs : EventArgs
{
    public string Username { get; set; }
}

public class UserService
{
    public event EventHandler<NewUserEventArgs> NewUserRegistered;

    public void RegisterUser(string username)
    {
        Console.WriteLine($"{username} has been registered.");
        OnNewUserRegistered(new NewUserEventArgs { Username = username });
    }

    protected virtual void OnNewUserRegistered(NewUserEventArgs e)
    {
        NewUserRegistered?.Invoke(this, e);
    }
}

public class WelcomeEmailService
{
    public void OnNewUserRegistered(object sender, NewUserEventArgs e)
    {
        Console.WriteLine($"Sending welcome email to {e.Username}.");
    }
}

public class UserStatisticsService
{
    public void OnNewUserRegistered(object sender, NewUserEventArgs e)
    {
        Console.WriteLine($"Updating statistics for new user {e.Username}.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        UserService userService = new UserService();
        WelcomeEmailService welcomeEmailService = new WelcomeEmailService();
        UserStatisticsService userStatisticsService = new UserStatisticsService();

        // Subscribe to the NewUserRegistered event
        userService.NewUserRegistered += welcomeEmailService.OnNewUserRegistered;
        userService.NewUserRegistered += userStatisticsService.OnNewUserRegistered;

        // Register a new user to trigger the event
        userService.RegisterUser("JohnDoe");
    }
}

As we wrap up our exploration, let's reflect on how the integration of commands, queries, and events shapes the architecture of modern software systems.

Conclusion

Finally, the central role of events in achieving decoupling in software systems is the essence of our discussion. Events stand out as silent facilitators of communication, allowing different parts of a system to remain independent, yet informed about the occurrences that matter to them. This decoupling is crucial, not just for the system's scalability and flexibility but for its overall health and maintainability.

The importance of events goes hand in hand with the principle of unidirectional data flow, a concept that ensures information moves in a single, clear direction. This approach simplifies understanding and managing state changes across the system, reducing the likelihood of complex dependencies and unpredictable behaviors.

Together, these concepts form a blueprint for designing robust and reliable systems. Using events and one-way data flow changes the game. It makes us rethink how we build and update our software, making everything simpler and smarter.

Subscribe to Rico Fritzsche

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe