289

Under what conditions would one favor apps talking via a message queue instead of via web services (I just mean XML or JSON or YAML or whatever over HTTP here, not any particular type)?

I have to talk between two apps on a local network. One will be a web app and have to request commands on another app (running on different hardware). The requests are things like creating users, moving files around, and creating directories. Under what conditions would I prefer XML Web Services (or straight TCP or something) to using a Message queue?

The web app is Ruby on Rails, but I think the question is broader than that.

0

5 Answers 5

348

When you use a web service you have a client and a server:

  1. If the server fails the client must take responsibility to handle the error.
  2. When the server is working again the client is responsible of resending it.
  3. If the server gives a response to the call and the client fails the operation is lost.
  4. You don't have contention, that is: if million of clients call a web service on one server in a second, most probably your server will go down.
  5. You can expect an immediate response from the server, but you can handle asynchronous calls too.

When you use a message queue like RabbitMQ, Beanstalkd, ActiveMQ, IBM MQ Series, Tuxedo you expect different and more fault tolerant results:

  1. If the server fails, the queue persist the message (optionally, even if the machine shutdown).
  2. When the server is working again, it receives the pending message.
  3. If the server gives a response to the call and the client fails, if the client didn't acknowledge the response the message is persisted.
  4. You have contention, you can decide how many requests are handled by the server (call it worker instead).
  5. You don't expect an immediate synchronous response, but you can implement/simulate synchronous calls.

Message Queues has a lot more features but this is some rule of thumb to decide if you want to handle error conditions yourself or leave them to the message queue.

8
  • 2
    If SOAP/JMS binding is used, one also gains loose coupling at web services.
    – koppor
    Commented May 15, 2015 at 21:40
  • For multiple micro services at server side which should be preferred Web service or Queuing? Commented Aug 10, 2017 at 10:23
  • Dear @sw. , may I know if that means we can put MQ infront of normal Websites? Let's say I have Wordpress Website (LAMP stack). Can I use MQ infront of Apache so that requests come 1 after 1, instead of all coming at the same time. In this case, Clients (Browsers) are connected to MQ instead of Apache, am i right? But then, do Browsers keep the HTTP connection open and keep waiting for Apache to respond? Please help me understand a bit on this. Appreciate your kindness. Commented Oct 4, 2018 at 11:11
  • @夏期劇場 what is your use case? What are you trying to achieve that benefit the end user using the browser?
    – sw.
    Commented Oct 16, 2018 at 22:10
  • 1
    Aren't these concerns with client/server setups still concerns between the queue and the server and between the client and the queue? It seems like the client/server paradigm still exists and now in two places. Commented Nov 11, 2019 at 17:49
80

There's been a fair amount of recent research in considering how REST HTTP calls could replace the message queue concept.

If you introduce the concept of a process and a task as a resource, the need for middle messaging layer starts to evaporate.

Ex:

POST /task/name
    - Returns a 202 accepted status immediately
    - Returns a resource url for the created task: /task/name/X
    - Returns a resource url for the started process: /process/Y

GET /process/Y
    - Returns status of ongoing process

A task can have multiple steps for initialization, and a process can return status when polled or POST to a callback URL when complete.

This is dead simple, and becomes quite powerful when you realize that you can now subscribe to an rss/atom feed of all running processes and tasks without any middle layer. Any queuing system is going to require some sort of web front end anyway, and this concept has it built in without another layer of custom code.

Your resources exist until you delete them, which means you can view historical information long after the process and task complete.

You have built in service discovery, even for a task that has multiple steps, without any extra complicated protocols.

GET /task/name
    - returns form with required fields

POST (URL provided form's "action" attribute)

Your service discovery is an HTML form - a universal and human readable format.

The entire flow can be used programmatically or by a human, using universally accepted tools. It's a client driven, and therefore RESTful. Every tool created for the web can drive your business processes. You still have alternate message channels by POSTing asynchronously to a separate array of log servers.

After you consider it for a while, you sit back and start to realize that REST may just eliminate the need for a messaging queue and an ESB altogether.

http://www.infoq.com/presentations/BPM-with-REST

9
  • 14
    @tempire what about fault tolerance and so forth? REST is nice, but the developer ends up building middleware him/herself Commented Mar 21, 2011 at 1:48
  • 10
    @Yar Most questions about 'what about this' can be re-stated, 'how is it handled on the web?'. Fault tolerance could be handled via load balancers, or even manipulation of dns records. There are a couple more issues to work out, like horizontal scalability - the web doesn't inherently handle that well (ddos attacks, for example).
    – Tempire
    Commented Apr 7, 2011 at 3:15
  • 8
    @tempire, there is no guaranteed delivery on the Web, right? I hit submit and pray that the message gets to its destination. With an MQ, I know that if I get to the MQ I'm done. It will handle getting the message to the destination. Commented Apr 8, 2011 at 21:06
  • 10
    @Yar consider what 'guaranteed delivery' means, though. It's only as 'guaranteed' as the uptime of message queue. Replace the message queue with a REST server(s) that treats tasks and processes as resources, and you've got the same 'guarantee' as anything else. Essentially, you still have a message queue, but in a web standard accessible format, that can be monitored using any web tool.
    – Tempire
    Commented Apr 9, 2011 at 18:38
  • 18
    @Yar - I don't think many people understand the problem definition MQ is trying to solve enough to even consider such things. They understand the MQ, but that's different from understanding the problem space. It's a widespread issue, though, because I think most programmers & managers in the world have been trained to connect pieces instead of engineer solutions.
    – Tempire
    Commented Apr 16, 2011 at 18:52
46

Message queues are ideal for requests which may take a long time to process. Requests are queued and can be processed offline without blocking the client. If the client needs to be notified of completion, you can provide a way for the client to periodically check the status of the request.

Message queues also allow you to scale better across time. It improves your ability to handle bursts of heavy activity, because the actual processing can be distributed across time.

Note that message queues and web services are orthogonal concepts, i.e. they are not mutually exclusive. E.g. you can have a XML based web service which acts as an interface to a message queue. I think the distinction your looking for is Message Queues versus Request/Response, the latter is when the request is processed synchronously.

2
  • 3
    Yes, I was just thinking this: it's not whether they're blocking or non-blocking. It's whether they require longer and/or unpredictable times to process... Regarding them being orthogonal, it's also true that web services can be used to long requests (separate dropoff and pickup, of course). Yet if you have the luxury of a message queue, it could be a good idea. Commented Mar 5, 2010 at 1:33
  • My new question is, what if wanted the process to be synchronous, but asynchronous on timeout? Then perhaps web services would be a better fit. Commented Mar 5, 2010 at 1:34
30

Message queues are asynchronous and can retry a number of times if delivery fails. Use a message queue if the requester doesn't need to wait for a response.

The phrase "web services" make me think of synchronous calls to a distributed component over HTTP. Use web services if the requester needs a response back.

1
  • 1
    Thanks for that, yeah "guaranteed delivery," I'll have to think about whether that's important. I mean, sync vs. async is kind of a taste thing, in some sense. While there are clearly black and white cases, there's also a huge gray middle. Commented Mar 5, 2010 at 1:27
22

I think in general, you'd want a web service for a blocking task (this tasks needs to be completed before we execute more code), and a message queue for a non-blocking task (could take quite a while, but we don't need to wait for it).

1
  • Web services also offer one-way methods (@Oneway). For receiving the answer, a web service has to be offered by the client.
    – koppor
    Commented May 15, 2015 at 21:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.