DEV Community

Sergey Shinder
Sergey Shinder

Posted on

Our label workers scaled in on an empty queue and stopped the one instance still building the evening manifest

Every evening our label workers build the manifest for our largest carrier: one file listing every parcel collected that day, which has to reach the carrier before half past six or the collection is not booked. The workers are EC2 instances in an auto scaling group, consuming jobs from a RabbitMQ queue. A small exporter publishes the queue depth to CloudWatch, and a target tracking policy keeps roughly one instance per twenty waiting jobs, with a minimum of one.

The manifest job is the last one of the day and takes about forty minutes. On a Tuesday in November it was picked up at ten to six by one of four running instances. A few minutes later the policy scaled the group in to one, and the instance it chose to remove was the one building the manifest.

The exporter published messages ready, the count of jobs nobody had taken yet. A job a worker is processing is unacknowledged, and does not count as ready. Once the manifest was taken, the queue looked empty, the metric said zero, and the policy did exactly what it was told. The auto scaling group chooses which instance to terminate by its own termination policy, balancing availability zones first. Nothing in it knows which instance is busy.

When the instance shut down, its connection to RabbitMQ closed and the broker returned the unacknowledged job to the queue, as it should. The remaining worker started the manifest from the beginning at four minutes past six. It finished at thirteen minutes to seven, and the carrier booked our collection for the next morning.

Three changes followed. A worker now sets scale in protection on its own instance when it takes a job and removes it after acknowledging the job, so the group will not choose a busy instance when it scales in. The scaling metric is now ready plus unacknowledged messages, the work that exists rather than the work waiting. The manifest job writes a checkpoint every five hundred parcels, so a restart continues from the last one. A lifecycle hook also gives a worker that is being terminated time to finish or checkpoint its current job before it stops.

An autoscaler can only see the number it is given. Ours was given the queue, and the most important work of the day was not in it.

– Sergey Shinder

Top comments (0)