RabbitMQ change queue parameters on a production system
Asked Answered
P

1

24

I'm using RabbitMQ as a message queue in a service-oriented architecture, where many separate web services publish messages bound for RabbitMQ queues. Those queues are in turn subscribed to by various consumers, which perform background work; a pretty vanilla use-case for RabbitMQ.

Now I'd like to change some of the queue parameters (specifically, I'd like to bind queues to a new dead-letter exchange with a certain routing key). My problem is that making this change in place on a production system is problematic for a couple reasons.

Whats the best way for me to transition to these new queues without losing messages in a production system?

I've considered everything from versioning queue names to making a new vhost with the new settings to doing all the changes in place.

Here are some of the problems I'm facing:

  1. Because RabbitMQ queues are idempotent, the disparate web services have been declaring the queues before publishing to them (in case they don't already exist). Once you change the queue parameters (but maintain the same routing key), the queue declare fails and RabbitMQ closes the channel.

  2. I'd like to not lose messages when changing a queue (here I'm planning on subscribing an exclusive consumer that saves the messages and then republishes to the new queue).

  3. General coordination between disparate publishers and the consumer base (or, even better, a way to avoid needing to coordinate them).

Pillsbury answered 12/8, 2014 at 21:15 Comment(0)
S
30

Queues bindings can be added and removed at runtime without any impact on clients, unless clients manually modify bindings. So if your question only about bindings just change them via CLI or web management panel and skip what written below.

It's a common problem to make back-incompatible changes, especially in heterogeneous environment, especially when multiple applications attempts to declare same entity in their own way (with their specific settings). There are no easy way to change queue declaration at the same time in multiple applications and it highly depends on how whole working process organized, how critical your apps are, what is your infrastructure and etc.

Fast and dirty way:

While the publishers doesn't deals with queues declaration and bindings (at least they should not do that), you can focus on consumers. Wrapping queues declaration in try-except block may be the fast and dirty choice. Also most projects, even numerous can survive small downtime, so you can block rabbitmq user in one shell, alter queue as you wish (create new one and make your consumers use it instead of old one) and then unblock user and let consumers works as before (your workers are under supervisor or monit, right?). Then migrate manually messages from old queue to new one.

Fast and safe solution:

Is is a bit tricky and based on a hack how to migrate messages from one queue to another inside single vhost. The whole solution works inside single vhost but requires extra queue for every queue you want to modify. Set up Dead Letter Exchanges on source queue and point it to route expired messages to your new target queue. Then apply Per-Queue Message TTL to source queue, set x-message-ttl=0 (to it's minimal value, see No Queueing at all note about immediate delivery). Both actions can be done via CLI or management panel and can be done on already declared queue. In this way your publishers can publish messages as usual and even old consumers can work as expected for the first time, but in parallel new consumers can consume from new queue which can be pre-declared with new args manually or in other way.

Note, that on queues with large messages number and huge messages flow there are some risks to met flow control limits, especially if your server utilize almost all of it resources.

Much more complicated but safer approach (for cases when whole messages workflow logic changed):

Make all necessary changes to applications and run new codebase in parallel to existing one, but on the different RabbitMQ vhost (or even use separate server, it depends on your applications load and hardware). Actually, it may be possible to run on the same vhost but change exchanges and queues name, but it even doesn't sound good and smells even in written form. After you set up new apps, switch them with old one and run messages migration from old queues to new one (or just let old system empty the queues). It guaranties seamless migration with minimal downtime. If you have your deployment automatized, whole process will not takes too much efforts.

P.S.: in any case above, if you can, let old consumers to empty queues so you don't need to migrate messages manually.

Update:

You may find very useful Shovel plugin, especially Dynamic Shovels to move messages between exchanges and queues, even between different vhosts and servers. It's the fastest and safest way to migrate messages between queues/exchanges.

Skirret answered 13/8, 2014 at 9:31 Comment(6)
Thanks for the answer. The vhost solution has felt like the cleanest to me as well, but it introduced one problem: all the distributed publishers have to keep track of where they are publishing and be updated accordingly every time we make a change. Is there an easy way to make it so that they can just point to the same place and somewhere else we can route it to the correct Vhost? I don't think publishers should have to concern themselves with the those details if it is avoidable. But maybe this is more of a system admin question than a RabbitMQ question.Pillsbury
Usually vhost is configurable option (or at least should be), so changing it should not be a problem. Note, that if you doesn't want (or you cant afford it) downtime, just run new publishers and consumers in parallel and then stop original one migrate messages from old vhost to new. But if you can, make your app survives after queue declaration failure at first stage and then just apply new changes to producers and consumers.Skirret
I've added a solution (Fast and safe solution) which may come in handy to make hot changes to queues declaration, for example, set it to durable or auto-delete or exclusive, etc. But make sure you understand what you are doing before following this way.Skirret
If you not sure about any cases you can double check the solutions by asking the same question in rabbitmq user group - groups.google.com/forum/#!forum/rabbitmq-discuss and point a link to our discussion.Skirret
I updated my answer with shovel plugin note, hope it will help someone.Skirret
Note that x-message-ttl can be 0, for immediate delivery.Daudet

© 2022 - 2024 — McMap. All rights reserved.