Skip to content

SOA 2.0

Wim van Gool edited this page Feb 12, 2019 · 22 revisions

<< SOA 1.0: From Applications to Service-Orientation

Given the problems of RPC-style communication, somewhere around 2014, people began to realize that we needed to move towards asynchronous communication between services. Asyncronous communication, e.g. using Publish/Subscribe between services, was not entirely new, but at the time it was applied much less often, because asynchronous communication didn't play well with distributed transactions. You have to realize that the mindset of the time was to hang on to the ACID-properties for data, even if that data was now spread out over multiple services and databases.

Asynchronous by default

Using asynchronous communication - particulary events - as the default communication-style between services, meant that services now became much more run-time independent, with the occiasional exception where synchronous communication was still required (e.g. requesting an access-token from an OAuth Service), but it also meant we had to drop the guarantees of absolute data consistency between services (which guarantee we never truly got anyway).

Consequently, we had to accept that a change in one service would gradually ripple through the system, instead of updating N services pretty much at once (or none at all) - something that is now commonly known as eventual consistency.

Eventual Consistency

At first, many people, primarily database administrators, who had been brought up in a world of atomicity and consistency, were worried we would end up with the same problems we were trying to solve in the first place: having multiple copies of data that were out-of-sync, resulting in lots of confusion and errors. Fortunately, these dark scenarios never really materialized, because people have been working with systems and sources that were eventual consistent for ages before computers even existed, and the requirement to retain full atomic consistency in a distributed system for it to function properly turned out to be an artificial one.

The move towards asynchronous, event-driven architectures did force us to rethink some patterns we used to apply, especially regarding implementation of long-running processes (or saga's as they were now called), but it was well worth the effort. Instead of having process services coordinating or orchestrating an entire process top-down, we now much more often used choreography to execute an entire process from begin to end.

Choreography

The service types we defined in the first era remained the same for the most part, but their roles and design slightly changed. For example, since we no longer needed process services to coordinate long running processes, they were now used to reconstruct the current state of a process by monitoring the events of a process. This way, the state or progress of a long running process can be displayed in a user interface and/or it can be used to manually or automatically control these processes, if necessary.

Process Service

This pattern of reconstructing or rebuilding a service's internal state by listening to the events of interest isn't something exclusively reserved for Process Services: it can be used by any service and is also particularly useful for BI Services. BI Services typically build a large data-warehouse and all the necessary reports from the business data that is managed by Business Services, and since these Business Services will publish any change or update of their data through events, BI Services only have to plug-in to the service bus and start gathering that data - another nice feature of event-driven architectures.

CQRS and Event-Sourcing

Also in this era, the architectural pattern known as CQRS became popular, which stands for Command-Query Responsibility Segregation. It was first coined by Greg Young and represents a variation of the much older Command-Query Separation design pattern, which encourages you to write functions that either only mutate data (known as the commands) or only retrieve data (known as the queries), so that functions never cause any hidden side effects that will make a program obscure, hard to understand and thus prone to errors. As such, CQS says you should not do this:

// Strictly speaking, this is not allowed by CQS.
public sealed class Counter
{
    private int _value;

    // Increment and return new value - both 'command' and 'query'.
    public int Increment() =>
        ++_value;
}

But this:

// This implementation follows the CQS-pattern.
public sealed class Counter
{
    private int _value;

    // Property for reading the current value (query).
    public int Value =>
        _value;

    // Only increment - no return value (command).
    public void Increment() =>
        _value++;
}

CQRS is the same pattern, but instead of focussing on single functions of a programming language, it tells you that the functions (endpoints) of a service should either only mutate data or retrieve data. The advantage of this pattern is that it allows you to separate all the code that is specialized in mutating data (i.e. your business logic and domain model) from the code that is optimized to retrieve data from (typically) your database:

CQRS Basic

Basically, it's another form of separation of concerns in your code and/or architecture, allowing you to implement, test, optimize and even deploy both sides of a service separately, if desired.

Note that there are some variations to choose from when applying CQRS to your service. The most basic version of CQRS is to only separate the code that handles commands (i.e. the business logic and your domain model) from the code that executes the queries, and to read from and write to the same database (schema), as depicted in the picture above.

The most elaborate version of CQRS, on the other hand, is to completely separate both code, endpoints and databases of your service to handle commands and queries separately. The command-side of your service will then write the mutations to a specialized database, and consequently update the other database(s) - that are optimized for queries - asynchronously through the events it publishes. This implies that the query-side of your service has become eventual consistent with its command-side.

CQRS Advanced

There are also versions in between which I will not go into right now, but the point is that on the left there are variations that are simple and already provide some benefits to testing and developing your services and on the right there are variations that are more complex and elaborate, but have the added benefit of making your services highly scalable (i.e. because the query-side can easily be scaled-out in production if desired).

One of the techniques that is often combined with CQRS - especially with the more scalable variations where performance is critical - is Event-Sourcing. Event-Sourcing is the technique where the command-side of your service does not persist the state of your domain as-is but as a series of events, never updating previous data but only appending to the end of the stream(s). The database that is used to store these streams is typically called an Event-Store.

Event-Store

The are several advantages to Event-Sourcing. The first one I already mentioned is performance: because the code only appends data to the end of stream, database locks are almost never required and writes and super-fast. Another benefit is that you automatically obtain a full history of mutations (events). You can use this history for audit-purposes (who changed what and when), but it also gives you the opportunity to replay the entire event-history to, for example, seed a BI Service with all historic data, possibly gaining insights you would otherwise have had to wait years for to obtain.

Historic Event Replay

Of course, Event-Sourcing also has its costs. First of all, it typically makes the application-code a little more complex, because to execute any business logic, the internal state of your domain model has to rebuild from the historic events before new ones can be applied. Not every developer has experience writing this sort of code, and so you either need seasoned developers who already have this experience or accept that they go through a bit of a learning curve.

Secondly, even though storage has become relatively cheap, it's not free, and Event-Stores can really grow quickly when a lot of data/mutations/events are being stored. Another added complexity of Event-Stores is that, though it's technically possible to query data from them using projections over event-streams, you are usually better off implementing and deploying specialized query-databases (i.e. read-models), as discussed earlier. Technically this is not very complex, but it does always add that extra bit of mental complexity to the solution and it also typically costs a little more to have two (or more) databases instead of just one - both in licenses and maintainance. And so Event-Sourcing can be extremely useful in some situations, but it's like all other technical or architectural decisions: it's a trade-off between benefits and costs.

Domain-Driven Design

Last but not least, the theory and practice of Domain-Driven Design has evolved and matured over the last few years. The book, concepts and mindset of DDD had been around for years already, but its practice has now been interwoven with event-driven, service-oriented architecture completely, and so it has become another elegant architectural tool for the software architect and developers to apply.

For example, by explicitly identifying Bounded Contexts within an organization and consistently applying the Ubiquitous Language inside those contexts, the software architect could now draw a fine, high level overview of a service landscape. By grouping services into these bounded contexts, an architect can make well-argumented, strategic decisions on how or when to build, integrate or just buy parts of the system. As such, the Bounded Context and Ubiquitous Language are so-called strategic design patterns.

Bounded Contexts

In contrast, the tactical design patterns of DDD, such as aggregates, repositories, entities, value objects, domain events and so forth, are meant to guide the implementation of a single service. It is, in fact, encouraged to only apply the tactical design patterns where they are truly worth the effort, because applying these patterns is an investment that should somehow be earned back. For very simple, CRUD-like applications, for example, you may not want to spent a lot of time and money on modeling a complex domain model. For services that are part of your core domain, on the other hand, spending time on creating and maintaining an elegant domain model that's managing the complexity of your business may give you a strategic advantage over your competitors.

Note that if you choose to apply the tactical design patterns of DDD in a service, then you should also apply CQRS, at least in its most basic form where you separate your business logic from your queries. This is because the elegant Domain Model you will put so much effort into to get right is meant to tackle the complexity of your business logic (i.e. the Command-side of your service) and should not be contaminated with concerns that are implemented for data-retrieval (i.e. the Query-side of your service).

With all these new patterns & technologies, we made another big leap forward in building robust, agile and scalable systems. But of course, the journey doesn't end here. Let's take a look at where we are today.

SOA 2.5: MicroServices and other new technologies >>

Clone this wiki locally