Service Oriented Architecture - AMQP or HTTP
Asked Answered
C

3

65

A little background.

Very big monolithic Django application. All components use the same database. We need to separate services so we can independently upgrade some parts of the system without affecting the rest.

We use RabbitMQ as a broker to Celery.

Right now we have two options:

  1. HTTP Services using a REST interface.
  2. JSONRPC over AMQP to a event loop service

My team is leaning towards HTTP because that's what they are familiar with but I think the advantages of using RPC over AMQP far outweigh it.

AMQP provides us with the capabilities to easily add in load balancing, and high availability, with guaranteed message deliveries.

Whereas with HTTP we have to create client HTTP wrappers to work with the REST interfaces, we have to put in a load balancer and set up that infrastructure in order to have HA etc.

With AMQP I can just spawn another instance of the service, it will connect to the same queue as the other instances and bam, HA and load balancing.

Am I missing something with my thoughts on AMQP?

Courage answered 30/5, 2013 at 14:7 Comment(0)
W
106

At first,

  • REST, RPC - architecture patterns, AMQP - wire-level and HTTP - application protocol which run on top of TCP/IP
  • AMQP is a specific protocol when HTTP - general-purpose protocol, thus, HTTP has damn high overhead comparing to AMQP
  • AMQP nature is asynchronous where HTTP nature is synchronous
  • both REST and RPC use data serialization, which format is up to you and it depends of infrastructure. If you are using python everywhere I think you can use python native serialization - pickle which should be faster than JSON or any other formats.
  • both HTTP+REST and AMQP+RPC can run in heterogeneous and/or distributed environment

So if you are choosing what to use: HTTP+REST or AMQP+RPC, the answer is really subject of infrastructure complexity and resource usage. Without any specific requirements both solution will work fine, but i would rather make some abstraction to be able switch between them transparently.

You told that your team familiar with HTTP but not with AMQP. If development time is an important time you got an answer.

If you want to build HA infrastructure with minimal complexity I guess AMQP protocol is what you want.

I had an experience with both of them and advantages of RESTful services are:

  • they well-mapped on web interface
  • people are familiar with them
  • easy to debug (due to general purpose of HTTP)
  • easy provide API to third-party services.

Advantages of AMQP-based solution:

  • damn fast
  • flexible
  • cost-effective (in resources usage meaning)

Note, that you can provide RESTful API to third-party services on top of your AMQP-based API while REST is not a protocol but rather paradigm, but you should think about it building your AQMP RPC api. I have done it in this way to provide API to external third-party services and provide access to API on those part of infrastructure which run on old codebase or where it is not possible to add AMQP support.

If I am right your question is about how to better organize communication between different parts of your software, not how to provide an API to end-users.

If you have a high-load project RabbitMQ is damn good piece of software and you can easily add any number of workers which run on different machines. Also it has mirroring and clustering out of the box. And one more thing, RabbitMQ is build on top of Erlang OTP, which is high-reliable,stable platform ... (bla-bla-bla), it is good not only for marketing but for engineers too. I had an issue with RabbitMQ only once when nginx logs took all disc space on the same partition where RabbitMQ run.

UPD (May 2018): Saurabh Bhoomkar posted a link to the MQ vs. HTTP article written by Arnold Shoon on June 7th, 2012, here's a copy of it:

I was going through my old files and came across my notes on MQ and thought I’d share some reasons to use MQ vs. HTTP:

  • If your consumer processes at a fixed rate (i.e. can’t handle floods to the HTTP server [bursts]) then using MQ provides the flexibility for the service to buffer the other requests vs. bogging it down.
  • Time independent processing and messaging exchange patterns — if the thread is performing a fire-and-forget, then MQ is better suited for that pattern vs. HTTP.
  • Long-lived processes are better suited for MQ as you can send a request and have a seperate thread listening for responses (note WS-Addressing allows HTTP to process in this manner but requires both endpoints to support that capability).
  • Loose coupling where one process can continue to do work even if the other process is not available vs. HTTP having to retry.
  • Request prioritization where more important messages can jump to the front of the queue.
  • XA transactions – MQ is fully XA compliant – HTTP is not.
  • Fault tolerance – MQ messages survive server or network failures – HTTP does not.
  • MQ provides for ‘assured’ delivery of messages once and only once, http does not.
  • MQ provides the ability to do message segmentation and message grouping for large messages – HTTP does not have that ability as it treats each transaction seperately.
  • MQ provides a pub/sub interface where-as HTTP is point-to-point.

UPD (Dec 2018): As noticed by @Kevin in comments below, it's questionable that RabbitMQ scales better then RESTful servies. My original answer was based on simply adding more workers, which is just a part of scaling and as long as single AMQP broker capacity not exceeded, it is true, though after that it requires more advanced techniques like Highly Available (Mirrored) Queues which makes both HTTP and AMQP-based services have some non-trivial complexity to scale at infrastructure level.

After careful thinking I also removed that maintaining AMQP broker (RabbitMQ) is simpler than any HTTP server: original answer was written in Jun 2013 and a lot of changed since that time, but the main change was that I get more insight in both of approaches, so the best I can say now that "your mileage may vary".

Also note, that comparing both HTTP and AMQP is apple to oranges to some extent, so please, do not interpret this answer as the ultimate guidance to base your decision on but rather take it as one of sources or as a reference for your further researches to find out what exact solution will match your particular case.

Whitney answered 25/6, 2013 at 12:5 Comment(12)
We ended up going with HTTP/REST. I really wanted to go the AMQP route because it fit so nicely into our architecture but my team didnt want to try something new so that's a bummer. So much more work is needed for development of a redudant and highly available SOA system using HTTP instead of AMQP and RPC.Courage
@Whitney I think one thing to mention (and correct me if Iam wrong) is that with AMQP you can actually push messages to the destination where as with HTTP you cant (working on request-response method)Endomorph
@Endomorph HTTP and AMQP are different concepts, so I'd prefer not use such criteria for their comparison.Whitney
@Whitney I agree but still thats a capability which should be considered when thinking about solution messaging solution (to http or not to http)Endomorph
@Endomorph exactly, AMQP is very different from HTTP by many factors, like already mentioned advanced routing, connection multiplexing (which added in http2) and so on. Same for HTTP, caching, proxying, methods and so on. My original point is HTTP and AMQP are on the different level and comparing them may be like comparing car and train: while both are vehicles, they are different in many aspects.Whitney
Here is a good read from the comparison point of view :blogs.perficient.com/ibm/2012/06/07/mq-vs-httpHypocoristic
@Whitney I know this is an old question, but as a clarification why does AMQP scale better than HTTP? Intuitively, AMQP forces all traffic through a broker and I would assume that the broker then becomes a bottleneck especially if all message passing in the architecture is based on funneling traffic through a broker? In that sense HTTP would seen to scale better even if inferior in other respects?Ingrowth
@Ingrowth Thank you for a good question. At that time of writing here's what was in my mind: speaking about particular implementation of AMQP - RabbitMQ, scaling horizontally would mean adding a node in a cluster and enabling queue mirroring, then scaling application horizontally would mean adding more workers, so eventually we have no SPOF, while with REST solutions, we normally have to have LB in front of it which became a real bottleneck. Nowadays I would say that with service discovery and k8s it shouldn't make any significant difference in terms of scaling infra nowadays.Whitney
@Ingrowth Though as long as broker/LB is not a bottleneck, scaling application with adding more workers to AMQP looks easier to me, and giving that AMQP and MQ in general fits a long-running tasks processing far more better and used for tasks which doesn't require realtime processing, broker is rarely a bottleneck, unlike LB in HTTP.Whitney
I don't know one way or the other but it would be surprising to me if a broker scales better than a load balancer. I would assume it generally scales worse but message queues generally provide desirable extra features on top of HTTP and that would be a key trade off.Ingrowth
In case of RabbitMQ, if clustering used and queue has HA option on, single broker is rarely and issue (at some cost, obviously). I see your point, I agree that scales better is a subjective opinion which may be influenced by experience with technologies in question and to state this I should backed it by more facts. What I'm going to do is to alter original answer to point that both solutions scales well. The issue comparing HTTP and AMQP is that it's like apple to oranges to some extent. Thanks a lot for bringing this up! Merry Xmas!Whitney
I have a lot more experience then when I originally asked this question. I am very glad that my team stopped me and we stuck with the standard approach. For what it's worth though I have personally 100% abandoned using REST in any future projects and try to exclusively use gRPC or Cap'n Proto. When I need a message queue I use NATS and if I need guarantees I use NATS streaming. These are all just personal choices. I think at the end of the day unless you have extreme requirements any solution that is well thought out and tested will work just fine.Courage
C
22

The irony of the solution OP had to accept is, AMQP or other MQ solutions are often used to insulate callers from the inherent unreliability of HTTP-only services -- to provide some level of timeout & retry logic and message persistence so the caller doesn't have to implement its own HTTP insulation code. A very thin HTTP gateway or adapter layer over a reliable AMQP core, with option to go straight to AMQP using a more reliable client protocol like JSONRPC would often be the best solution for this scenario.

Cocainize answered 27/8, 2013 at 11:3 Comment(0)
B
1

Your thoughts on AMQP are spot on!

Furthermore, since you are transitioning from a monolithic to a more distributed architecture, then adopting AMQP for communication between the services is more ideal for your use case. Here is why…

Communication via a REST interface and by extension HTTP is synchronous in nature — this synchronous nature of HTTP makes it a not-so-great option as the pattern of communication in a distributed architecture like the one you talk about. Why?

Imagine you have two services, service A and service B in that your Django application that communicate via REST API calls. This API calls usually play out this way: service A makes an http request to service B, waits idly for the response, and only proceeds to the next task after getting a response from service B. In essence, service A is blocked until it receives a response from service B.

This is problematic because one of the goals with microservices is to build small autonomous services that would always be available even if one or more services are down– No single point of failure. The fact that service A connects directly to service B and in fact, waits for some response, introduces a level of coupling that detracts from the intended autonomy of each service.

AMQP on the other hand is asynchronous in nature — this asynchronous nature of AMQP makes it great for use in your scenario and other like it.

If you go down the AMQP route, instead of service A making requests to service B directly, you can introduce an AMQP based MQ between these two services. Service A will add requests to the Message Queue. Service B then picks up the request and processes it at its own pace.

This approach decouples the two services and, by extension, makes them autonomous. This is true because:

  • If service B fails unexpectedly, service A will keep accepting requests and adding them to the queue as though nothing happened. The requests would always be in the queue for service B to process them when it’s back online.
  • If service A experiences a spike in traffic, service B won’t even notice because it only picks up requests from the Message Queues at its own pace
  • This approach also has the added benefit of being easy to scale— you can add more queues or create copies of service B to process more requests.
  • Lastly, service A does not have to wait for a response from service B, the end users don’t also have to wait for long— this leads to improved performance and, by extension, a better user experience.

Just in case you are considering moving from HTTP to AMQP in your distributed architecture and you are just not sure how to go about it, you can checkout this 7 parts beginner guide on message queues and microservices. It shows you how to use a message queue in a distributed architecture by walking you through a demo project.

Blakeley answered 16/2, 2023 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.