Skip to content

MicroService Messaging Patterns Queries

Wim van Gool edited this page Jul 24, 2019 · 2 revisions

On the previous page we explained in detail how Commands can be sent and processed either synchronously or asynchronously. We also looked at the various design patterns and infrastructural components that are involved when implementing these solutions. On this page we will do the same for Queries.

Sending & Processing Queries Synchronously

Queries are requests to a service to obtain some data from it. For this reason, a Query is actually composed of a Request/Response message pair, where the Request contains the parameters of the Query and the Response contains the requested data that is ultimately returned by a service. Because both Commands and Queries represent request-types, they are very similar in terms in communication and messaging patterns: both can be sent from a front-end or back-end component, but the receiver is always the back-end that defines the Query in its contract. In addition, like Commands, Queries can be sent and processed either synchronously or asynchronously.

However, whereas asynchronous communication provides great benefits for Commands, Queries are almost always sent and processed synchronously. The first and foremost reason for this is that the sender is expecting a Response from the service that processes the Query, and so it makes sense to await that response as the request is made:

Queries (Synchronous)

Synchronous communication via an API is a natural fit for Queries.

Of course, this does introduce a run-time dependency between the sender and receiver of a Query. As explained earlier, this run-time dependency is not a very big problem when the sender is a front-end that belongs to the same service as the back-end that processes the Query, but for inter-service communication we'd like to minimize these dependencies. A common tactic to minimize the need for (synchronous) querying between services in a distributed system is to have each service store a local copy of all data that it needs to process incoming Commands and Events. That way, it can just query its own data store instead of having to reach out to other services to obtain that data.

To illustrate how this works, let's assume that we have implemented some sort or web shop ordering system with two services: a UserAccountService and an EmailService. The UserAccountService is used for user-registration and thus manages the profile of a user, which contains things like the name and e-mailaddress of user. The EmailService is specifically designed to send various e-mails to users based on certain Events, like newly placed orders, advertisements, and so on. Naturally, the EmailService requires access to the e-mailaddress of a user to perform its job. About a decade or two ago, there was this principle in Service-Oriented Architecture that each service should manage and have exclusive access to a specific part of the domain's business data. As a result, each specific field, like the e-mailadress of a user, was only allowed to be stored in the data store of the service that owned that data, and every other service that needed to use that data for its own operations would have to query that data from the owning service:

Store-only-Once Principle

When services are only allowed to store data that they own, you force them to query each other regularly at run-time.

Needless to say, this design created numerous run-time dependencies between services and therefore delivered a very fragile system. Moreover, if the system was all but trivial, all these run-time queries could quickly cause all kinds of performance problems. Fortunately, with ongoing insight and distributed systems becoming the norm for most corporations, the aforementioned store-only-in-one-service limitation was dropped in favor of the keep-a-local-copy policy. In a modern system, our EmailService is now allowed to keep its own copy of the user's e-mailaddress, so that it doesn't have to query the UserAccountService whenever it needs to send an e-mail:

Store-local-Copy Principle

When services store all data they need for their operations locally, you increase the run-time independence and overall performance of services.

The way that the EmailService obtains and maintains its local copy is through the Events that the UserAccountService publishes. For example, when a new user registers himself, the UserAccountService is going to publish an Event like the UserRegisteredEvent containing the user's e-mailaddress. Should the user ever change his e-mailaddress, then the UserAccountService will publish an Event like UserEmailAddressChangedEvent containing the new value of this e-mailaddress:

Update-local-Copy Principle

You typically use Events to update local copies of data.

Note that in this design, the ownership of the e-mailaddress still lies with the UserAccountService. This means that the UserAccountService is still the only source of thruth for the e-mailaddress and that only this service is allowed to manage and update that value, but other services are allowed to keep a local copy of it, obtained through Events published by the owning service, for optimizing performance and reliability of the system as a whole.

Sending & Processing Queries Asynchronously

In the previous paragraph we stated that Queries are almost always send and processed synchronously, because Queries, by definition, involve a Response that is to be returned to the sender of the Request, and asynchronous communication implies that there is no reply - at least not a reply that contains the result of the operation.

Given this information it would appear as if Queries cannot be send and processed asynchronously. And yet, even though it is quite uncommon, there are ways to do it. One trick is to send a return-address along with the Request, so that the receiving service can send the Response to this address when it has processed the Query:

Queries (Asynchronous)

You can have the Response returned asynchronously by using the call-back pattern between services.

Remark that the return-address actually represents a callback-function; as such, this pattern is basically the same as passing in a delegate as an argument to a void-returning function that is called as soon as the Response has been created, but then applied in a Service-Oriented context:

// Sender.
public void DoSomething()
{
    GetSomeData(new GetSomeDataRequest(), response =>
    {
        // Do something with the response...
    };
}

// Receiver.
public void GetSomeData(GetSomeDataRequest request, Action<GetSomeDataResponse> callback)
{
    // Execute the query internally.
    var response = GetSomeDataFromDataStore(request);

    // Invoke the callback so that the sender can consume the response.
    callback.Invoke(response);
}

The goal of this pattern is that the sender is not blocked in any way as the Query is being processed, which can be useful if the Query takes more than average time to complete. In C#, this pattern was widely used before the Task Parallel Library and async/await were added to the language. In javascript, we still use it a lot for making asynchronous http-requests.

On an enterprise level, asynchronously processing a Query can become useful if the Query takes somewhere from several seconds up to a few minutes to complete, which could, if processed synchronously, block the sender for too long or cause timeouts to occur. Another reason why you may want to process Queries asynchronously is when a certain Query sometimes experiences peakload (i.e. the Query is executed many times in a short period of time) but the actual response-time for each call is not crucial, allowing the receiver to process them at the rate it can handle with somewhat limited infrastructure. An example of such a case is when a ReportingService runs a background-task periodically, requesting statistical information for a large amount of entities in separate Queries in a short period of time from another service, but is able to handle and generate the reports asynchronously as Responses come in:

Queries (Throttling)

Asynchronously processing a Query can be a fitting solution if bursts of Requests come in from time to time, but response times aren't crucial.

Note that if performance of a Query is a problem, you should first look for ways to optimize it and bring down the average processing time or increase the timeout limit of that Query before you consider the asynchronous Query execution pattern, because it is not a trivial thing to implement and adds to the complexity of your architecture. First of all, the sender must expose a specific endpoint and share the (return-)address of that endpoint to receive the Response from the receiver. Secondly, the sender of the Request must somehow be able to correlate each Response with a specific Request so that it can appropriately handle each Response, as shown in the image above.

How you receive and correlate the Response may depend on what type of connection you are using. If you have the ability to create a full duplex connection between the sender and the receiver, then both the return-address and correlation are implicitly tied to the connection itself (provided you open and close the connection per call). However, in many situations, full-duplex communication is not possible because of limitations in the protocol, firewalls and other concerns. Full-duplex connections are, for example, not supported in the http-protocol. In these scenarios the sender could send both the return-address and a unique correlation-id with the Request, so that the receiver can return its Response to the specified return-address while adding the same correlation-id to the Response-message. Upon receiving the Response, the sender can now use the correlation-id to correlate the Request with its Response.

By sharing a return-address and exposing a separate endpoint for the Response to come in, you essentially process a Query using two one-way connections. When at least one of those one-way connections is still a synchronous connection, however, the run-time dependency between the services still exists. For example, when service A sends a Request to the api of service B and service B then sends its Reponse back to the api of service A, any downtime of either service could cause the Query to fail. You could solve this problem by introducing queues in between, making both connections truly asynchronous:

Queries using Queues

By introducing queues - one for the Requests and another for the Responses - you can make your Queries truly asynchronous and drop the run-time dependency between the services.

In this case, the return-address would not be the endpoint of an api, but, for example, a name or connection-string of the queue through which the Response is to be returned. Of cource, we now run into the same problem with authorization and validation as we did when we introduced a queue between the sender and receiver of a Command: by placing a Request directly into a queue, the receiver can only authorize and validate this Request after its been dequeued, and if one of these checks fail, its hard to notify the sender of the error. There are basically three solutions to solve this problem:

  1. Make sure that Requests that are placed into the queue never fail authorization or validation. This basically means that the Request can only come from trusted sources, such as is the case with service-to-service communication. If you rely on this using client-server communication, however, the client must be a trusted source (like a web-server, not running on the user's device) and pre-validate the Request before it is added to the queue.
  2. Implement a protocol between sender and receiver such that the receiver can either return a success-response containing the Response-message (i.e. data), or an error-response containing the error that occurred.
  3. Implement the same hybrid solution as we suggested for Commands, where the Request is sent to an api that first authorizes and validates it and then forwards it to a queue for further processing. Note that the api is only needed for the Request, as the Response doesn't need to be authorized or validated when returned.

Queries (Hybrid Solution)

Queries can also be implemented using a hybrid solution, where Requests are first authorized and validated by an API and then forwarded to a queue.

Next up: Events >>

Clone this wiki locally