DEV Community

Sergey Shinder
Sergey Shinder

Posted on

RabbitMQ took our booking job back after thirty minutes and gave it to the other consumer

Every evening at six our transport planning system publishes one message to RabbitMQ: book tomorrow's trunk collections. A consumer in our carrier adapter takes it, reads the plan and books a collection for each of about three hundred depot pairs through the carrier's API. Each booking takes eight to ten seconds while the carrier checks capacity, so the job runs for forty to fifty minutes. The consumer acknowledges the message after the last booking, so a crash halfway would bring the job back.

In October we moved to a new RabbitMQ cluster on a current version. The next morning several depots had two lorries arrive for the same collection, and three had three. The carrier's API accepts duplicates, because two collections from one depot in a day are sometimes what we want.

The log told the story. Thirty minutes after delivery the broker closed the channel with PRECONDITION_FAILED, delivery acknowledgement timed out, timeout value used 1800000 ms. The consumer kept booking, because a closed channel does not stop code that is already running, and its acknowledgement at the end failed. The message, unacknowledged on a closed channel, had gone back on the queue and been delivered to the adapter's second instance, which started booking from the top.

RabbitMQ has a consumer timeout, and the default is thirty minutes. If a delivery is not acknowledged within it, the broker treats the consumer as stuck, closes its channel and requeues the message. Our old cluster ran a version from before the timeout existed, so for three years a fifty minute hold had been fine. On the new one the message came back every thirty minutes for as long as the work took longer than that.

We cancelled the duplicates by phone that morning. The adapter now acknowledges within seconds, after writing a booking run to its database with the plan date as a unique key, and works from that table. Each depot booking carries its own reference, and the adapter checks the carrier's existing bookings before it creates one. A second message for the same date finds the run already there and does nothing. We also alert on any redelivered message, because a redelivery means something happened to a consumer.

We had used an unacknowledged message as a lock that lasted as long as the work. The broker had its own view of how long that should be.

– Sergey Shinder

Top comments (0)