I have a bunch of microservices whose functionality I expose through a REST API according to the API Gateway pattern. As these microservices are Spring Boot applications, I am using Spring AMQP to achieve RPC-style synchronous communication between these microservices. Things have been going smooth so far. However, the more I read about event-driven microservice architectures and look at projects such as Spring Cloud Stream the more convinced I become that I may be doing things the wrong way with the RPC, synchronous approach (particularly because I will need this to scale in order to respond to hundreds or thousands of requests per second from client applications).
I understand the point behind an event-driven architecture. What I don't quite understand is how to actually use such an pattern when sitting behind a model (REST) that expects a response to every request. For example, if I have my API gateway as a microservice and another microservice which stores and manages users, how could I model a thing such as a GET /users/1
in a purely event-driven fashion?
Best Answer
Repeat after me:
You can have one, or the other, or both, or neither. They're entirely different tools for entirely different problem domains. In fact, general purpose request-response communication is absolutely capable of being asynchronous, event-driven, and fault tolerant.
As a trivial example, the AMQP protocol sends messages over a TCP connection. In TCP, every packet must be acknowledged by the receiver. If a sender of a packet doesn't receive an ACK for that packet, it keeps resending that packet until it's ACK'd or until the application layer "gives up" and abandons the connection. This is clearly a non-fault-tolerant request-response model because every "packet send request" must have an accompanying "packet acknowledge response", and failure to respond results in the entire connection failing. Yet AMQP, a standardized and widely adopted protocol for asynchronous fault tolerant messaging, is communicated over TCP! What gives?
The core concept at play here is that scalable loosely-coupled fault-tolerant messaging is defined by what messages you send, not how you send them. In other words, loose coupling is defined at the application layer.
Let's look at two parties communicating either directly with RESTful HTTP or indirectly with an AMQP message broker. Suppose Party A wishes to upload a JPEG image to Party B who will sharpen, compress, or otherwise enhance the image. Party A doesn't need the processed image immediately, but does require a reference to it for future use and retrieval. Here's one way that might go in REST:
POST
request message to Party B withContent-Type: image/jpeg
201 Created
response message to Party A with aContent-Location: <url>
header which links to the processed imageContent-Location
headerThe
201 Created
response code tells a client that not only was their request successful, it also created a new resource. In a 201 response, theContent-Location
header is a link to the created resource. This is specified in RFC 7231 Sections 6.3.2 and 3.1.4.2.Now, lets see how this interaction works over a hypothetical RPC protocol on top of AMQP:
Do you see the problem here? In both cases, Party A can't get an image address until after Party B processes the image. Yet Party A doesn't need the image right away and, by all rights, couldn't care less if processing is finished yet!
We can fix this pretty easily in the AMQP case by having Party B tell A that B accepted the image for processing, giving A an address for where the image will be after processing completes. Then Party B can send A a message sometime in the future indicating the image processing is finished. AMQP messaging to the rescue!
Except guess what: you can achieve the same thing with REST. In the AMQP example we changed a "here's the processed image" message to a "the image is processing, you can get it later" message. To do that in RESTful HTTP, we'll use the
202 Accepted
code andContent-Location
again:POST
message to Party B withContent-Type: image/jpeg
202 Accepted
response which contains some sort of "asynchronous operation" content which describes whether processing is finished and where the image will be available when it's done processing. Also included is aContent-Location: <link>
header which, in a202 Accepted
response, is a link to the resource represented by whatever the response body is. In this case, that means it's a link to our asynchronous operation!Content-Location
header to determine if processing is finished. If so, Party A then uses the link in the async operation itself to GET the processed image.The only difference here is that in the AMQP model, Party B tells Party A when the image processing is done. But in the REST model, Party A checks if processing is done just before it actually needs the image. These approaches are equivalently scalable. As the system gets larger, the number of messages sent in both the async AMQP and the async REST strategies increase with equivalent asymptotic complexity. The only difference is the client is sending an extra message instead of the server.
But the REST approach has a few more tricks up its sleeve: dynamic discovery and protocol negotiation. Consider how both the sync and async REST interactions started. Party A sent the exact same request to Party B, with the only difference being the particular kind of success message that Party B responded with. What if Party A wanted to choose whether image processing was synchronous or asynchronous? What if Party A doesn't know if Party B is even capable of async processing?
Well, HTTP actually has a standardized protocol for this already! It's called HTTP Preferences, specifically the
respond-async
preference of RFC 7240 Section 4.1. If Party A desires an asynchronous response, it includes aPrefer: respond-async
header with its initial POST request. If Party B decides to honor this request, it sends back a202 Accepted
response that includes aPreference-Applied: respond-async
. Otherwise, Party B simply ignores thePrefer
header and sends back201 Created
as it normally would.This allows Party A to negotiate with the server, dynamically adapting to whatever image processing implementation it happens to be talking to. Furthermore, the use of explicit links means Party A doesn't have to know about any parties other than B: no AMQP message broker, no mysterious Party C that knows how to actually turn the image address into image data, no second B-Async party if both synchronous and asynchronous requests need to be made, etc. It simply describes what it needs, what it would optionally like, and then reacts to status codes, response content, and links. Add in
Cache-Control
headers for explicit instructions on when to keep local copies of data, and now servers can negotiate with clients which resources clients may keep local (or even offline!) copies of. This is how you build loosely-coupled fault-tolerant microservices in REST.