Message Durability in ActiveMQ 5.X
An in-depth dive into message persistence in AMQ, a notoriously difficult minefield that is important to get right.
Join the DZone community and get the full member experience.Join For Free
i get asked quite a bit to explain the basics of how activemq works with respect to how it stores messages (or doesn’t in some cases). here’s the high-level explanation of it. note, the context is within jms. if you use activemq’s non-jms clients (e.g., stomp, amqp, mqtt, etc.) then the behavior may be different in some cases.
the jms durability guarantees are pretty strong in terms of not losing messages that are marked “persistent.” let’s see how that applies for activemq.
topics are a broadcast mechanism. they allow us to implement publish-subscribe semantics in jms land. however, what happens if we mark a message “persistent” and there are no subscribers? in any normal broadcast (e.g., i go downtown and start shouting about the awesomeness of activemq), if there are no subscribers (it’s 3am and there’s nobody around to hear me…. must’ve been a good night out if i'm out at 3am) then what happens? nothing. nobody hears it. and we move on. activemq doesn’t do anything with the message if you publish it (persistent or not persistent) and there are no subscribers (no live subscribers and no durable subscribers).
activemq will only store the message if there are durable subscribers (active or inactive). for an inactive durable subscription, activemq will store messages marked “persistent” into a non-volatile store and wait for a subscriber to rejoin the subscription. at that point it will try to deliver messages.
for queues, activemq treats “persistent” messages with a simple default protocol. we basically block the main producer thread and wait for confirmation that the broker has actually gotten the message:
- producer sends message
producer blocks, waits for ack from broker
- producer continues on if successful ack
- retries if nack or timeout or failover
- receives message
- stores message to disk
- sends back ack
for “non-persistent” sends, the flow is different. we send in a “fire and forget” mode. the main producer thread does not get blocked and any ack or other response happens asynchronously on the activemq connection transport thread:
- producer sends message
- producer continues on with its thread and does not block
- producer eventually gets ack on a separate thread than the main producer thread
we can increase performance of sends to the broker by batching up multiple messages to send at once. this utilizes the network as well as the broker storage more effectively. there’s an important distinction you must be aware of when sending transacted. the opening of the tx session and the closing of it (rollback/commit) are all synchronous interactions with the broker, however , the sends for each individual message during the tx window are all sent asynchronously . this is okay if everything works out because the broker batches these messages up. but what happens if there are transport errors? or the broker runs out of space to save these messages?
we need to set an exceptionlistener to watch for errors during these sends. we also need to (or should) set a client side sending “producer window” to allow us to enforce producer flow control when the broker runs out of resources. see activemq producer flow control for more.
changing the defaults
the interesting settings on the producer that can change these behaviors:
- useasyncsend - always wait for acks asynchronously, even in persistent sends and commits
- alwayssyncsend – force all sends (non-persistent or transactional sends included) to always wait for ack from the broker
using the defaults are generally what folks want.
for production usage of activemq, i recommend the shared-storage approach at the moment. in this case, we need to be aware of what’s happening at the storage layer to understand activemq’s guarantees.
activemq by default will implement jms durability requirements which basically states messages that get stored must survive crashes. for this, we by default will do a “fsync” on the filesystem. now what happens on each system will be dependent on what os, network, storage controller, storage devices, etc you use. this is the same you’d expect for any type of database that needs to persistently store messages and is not activemq-specific, per se.
when we write to the activemq transaction journal we need to ask the operatingsystem to flush the journal to disk with a call to fsync . basically what happens is we force the os to write back the page-file cache it uses to cache file changes to the storage medium. it also encourages the storage medium to do what it needs to do (depends on implementation) to “store” the data to disk:
some storage controllers have their own cache that needs to be flushed. the disk drives have their own caches, etc. some of these caches are backed by battery and may write-back at their own time intervals, etc. for you to understand the durability of your messages running through activemq, you should understand the guarantees of your storage layer.
finally the last piece of the puzzle is how we deliver/dispatch messages to consumers and how they acknowledge. the activemq jms libraries handle all of this for you, so you don’t need to worry about whether or not you’re going to lose messages.
messages get dispatched to consumers up to a certain “prefetch” buffer that lives on the consumer. this helps speed up message processing by having an available cache of messages on the consumer ready to process and then refill this cache as the consumer consumes them. in activemq these prefetched messages are denoted as “in flight” in the console. at this point it’s up to the consumer to process these messages and ack them. (this will depend on the ack modes… the default of auto ack will send the ack as the consumer gets the message.. for more important message processing you may wish to use “client” ack where the client explicitly says when to ack the message, i.e., after it’s completed some processing).
if the consumer fails for some reason, any of the non-ack’d messages will be redelivered to another consumer (if available) and follow the same processing as above. the broker will not remove the message from its indexes until it gets an ack. so this includes failures at both the consumer and network level. if there are errors at either of these levels even after a consumer as “successfully processed” (note, this is very use-case specific what “successfully processed” means), and the broker does not get the ack, then it’s possible the broker will re-send the message. in this case you could end up with duplicates on the consumer side and will probably want to implement an idempotent consumer. for scaling up messaging producers/consumers, you’ll want to have idempotent consumers in place anyway.
last thing to note: jms does not guarantee once and only once processing of a message without the use of xa transactions. jms guarantees once and only once delivery insofar it can mark messages as being “redelivered” and have the consumer check that, but the consumer is responsible for how many times it should be allowed to process (or filter out with idempotent consumer).
Published at DZone with permission of Christian Posta, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.