14

I read slides from this year's UberConf and one of the speakers is making the argument that Spring JMS adds a performance overhead to your message queue system, however I don't see any evidence to support that in the slides. The speaker also makes the case that point-to-point is faster than the traditional "publish-subscribe" method because each message is sent only once instead of being broadcasted to every consumer.

I'm wondering if any experienced Java messaging gurus can weigh-in here and clarify a few technicalities:

  • Is there actually a performance overhead incurred by using Spring JMS instead of just pure JMS? If so, how and where is it introduced? Is there any way around it?
  • What actual evidence is there to support that P2P is faster than the pub-sub model, and if so, are there ever any cases when you would want to pub-sub over P2P (i.e. why go slower?!?)?

4 Answers 4

26

1) Primary, the overhead of Spring JMS is the use of JmsTemplate to send messages wihtout a caching mechanism underneath. Essentially, JmsTemplate will do the following for each message you send:

  • Create Connection
  • Create Session
  • Create Producer
  • Create Message
  • Send Message
  • Close Session
  • Close connection

This of could be compared to manually written code where you reuse things:

  • Create Connection
  • Create Session
  • Create Producer
  • Create Message
  • Send Message
  • Create Message
  • Send Message
  • Create Message
  • Send Message
  • Close Session
  • Close connection

Since the creation of connections, sessions and producers needs communication between your client and the JMS provider and, of course, resource allocation, it will create pretty large overhead for lots of small messages.

You can easily come around this by caching JMS resources. For instance use the spring CachingConnectionFactory or ActiveMQs PooledConnectionFactory (if you are using ActiveMQ, which you tagged this question with).

If you are running inside a full JavaEE container, pooling/caching is often built in and implicit when you retrieve your JNDI connection factory.

When receving, using spring Default Message Listening Container, there is a thin layer in spring that might add little overhead, but the primary aspects is that you can tweak the performance in terms of concurrency etc. This article explains it very well.

2)

PubSub is a pattern of usage, where the publisher does not need to know which subscribers that exists. You can't simply emulate that with p2p. And, without any proof at hand, I would argu that if you want to send an identical message from one application to ten other applications, a pub-sub setup would be faster than to send the message ten times p2p.

On the other hand, if you only have one producer and one consumer, choose the P2P pattern with queues instead, since it's easier to manage in some aspects. P2P (queues) allows load balancing, which pub/sub does not (as easily).

ActiveMQ also has a hybride version, VirtualDestinations - which essentially is topics with load balancing.

The actual implementation differs by different vendors, but topics and queues are not fundamentally different and should behave with similar performance. What you instead should check on is:

  • Persistence? (=slower)
  • Message selectors? (=slower)
  • Concurrency?
  • Durable subscribers? (=slower)
  • Request/reply, "synchronously" with temporary queues (= overhead = slower)
  • Queue prefetching (=impacts performance in some aspects)
  • Caching
1
  • 3
    Great answer, just a minor addition to topics vs queues (you are kind of mentioning it, but would be good to make it explicit). Topics guarantee that every online or offline-durable subscription to a topic would receive a message. Queues guarantee that one and only one subscriber would process a message. Besides that, topics can be used for 1-1 communication, and queues can have multiple publishers and load-balancing subscribers. It is though to do load-balancing with plain topics, and it is inefficient to do event distribution with queues. Horses for courses.
    – ddimitrov
    Aug 7, 2012 at 9:29
4

Are you talking about Mark Richards' slides? He posted source code for his benchmarks, so you can actually test his assertion about JmsTemplate performance. His benchmark code does use Spring's CachingConnectionFactory, and yet in spite of the caching it still demonstrates a significant performance penalty with JmsTemplate. I have executed, profiled and analyzed his code. The short answer is that the overhead from JmsTemplate is negligible and the measurable performance disparity in his code has to do with ActiveMQ's async vs sync sending modes. I posted my analysis here:

JmsTemplate is not evil

2

1) Spring templates open/close connections/sessions for each message sent/received. That's why it's slower. Most JMS implementations performs better when connections/session remain open so that they can use optimizations like message pre-fetching not to mention avoid the overhead of doing all the connection setup/tear down bits.

2) Topics are generally slower if they are copying/replicating data to more than one consumer. This is just a matter of physics. If 10 megs of messages are queue sent to a queue, then only 10 megs of data need to be transmitted to consumers. While on topic, if you have 10 consumers, and you send 10 megs of data to it, then 100 megs of data have to transmitted to consumers. So, for most JMS implementations:

  • adding a consumer to a topic can only slow your consumption rate.
  • adding a consumer to a queue usually helps increase the dequeue rate.
1
  • Regarding topic vs queue speed. It's two different use cases you are comparing which is not really comparable, either you want all data to go to all consumers or just to a single one. In any case, your answer is, of course, correct. Aug 7, 2012 at 13:45
1

I am not messaging guru, I wish you don't me sharing my thought here ;)

  1. There will always be overhead, because you have extra indirection. Even it is simply an extra level in call stack, it is still overhead. However, I believe such overhead is minimal. You may take a look in source code of JmsTemplate. There is not much extra things added by Spring during send. JmsTemplate is doing mostly what you need to do if you are using JMS anyway. You can always argue that those extra checking and deeper method call always takes more CPU cycle and memory. That's true, but I wonder how significant it is.

  2. PubSub and P2P (Topic and Queue in JMS terminology) are just two different models. I believe they are cannot replace each other. You cannot have the "send once and broadcast to multiple receiver" behavior by using Queue, and on the same time you cannot have the guaranteed-delivery behavior when using Topic (unless using Durable Subscriber, but that's another topic). So, choose the right type depends on what you are doing, instead of saying blindly that P2P is superior than PubSub (which I believe is non-sense)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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