Sub topic 4
Acknowledging messages
This chapter assumes you know what a producer, a consumer, and a broker are, and that you can read basic Python. It covers how a consumer tells the broker that a message is finished, and what goes wrong when it does that at the wrong time.
What an acknowledgement is
An acknowledgement (ack) is a signal from the consumer to the broker that says "I handled this message, you can delete it." Until the ack arrives, the broker keeps the message and treats it as in flight. If the consumer's connection closes before the ack, the broker puts the message back on the queue and delivers it again.
This is how a queue survives consumer crashes. Without acks, the broker cannot tell a finished job from a lost one.
Ack after processing
The safe order is to process first and acknowledge second. This example uses RabbitMQ through the pika library.
import pika
def process(body):
print("handling", body)
def handle(ch, method, properties, body):
try:
process(body)
ch.basic_ack(delivery_tag=method.delivery_tag)
except Exception:
ch.basic_nack(delivery_tag=method.delivery_tag, requeue=False)
conn = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
ch = conn.channel()
ch.queue_declare(queue="tasks", durable=True)
ch.basic_qos(prefetch_count=10)
ch.basic_consume(queue="tasks", on_message_callback=handle, auto_ack=False)
ch.start_consuming()Four details matter here:
auto_ack=Falseturns off automatic acknowledgement. WithTrue, the broker deletes each message as soon as it sends it, so a crash mid-processing loses the message.delivery_tagidentifies one delivery on this channel. You must ack on the same channel that received the message.basic_nackis a negative acknowledgement. Withrequeue=False, the broker does not put the message back on the queue. If the queue has a dead-letter policy, the message goes there instead. Retries and dead-letter queues are covered in a later chapter.basic_qos(prefetch_count=10)limits the consumer to 10 unacknowledged messages at a time.
Common errors
| Mistake | Result |
|---|---|
| Ack before processing | A crash after the ack loses the message. |
| Never acking | Messages stay in flight, then are redelivered when the connection closes. The queue's unacked count keeps growing. |
| Nack with `requeue=True` on a permanent failure | The message returns to the queue and fails again, in a loop. |
| Ack on a different channel | The broker raises an error and closes the channel. |
Other brokers
The idea is the same elsewhere, but the names differ. Amazon SQS uses a visibility timeout. A received message is hidden for a set time, and you delete it to acknowledge. If you do not delete it in time, it becomes visible again. Kafka consumers commit an offset, which marks every earlier message in a partition as done.
Exercise
A consumer reads a message, sends the ack, and then crashes while resizing an image. What happens to the job, and which ordering would have prevented it?
Show answer
The job is lost, because the broker already deleted the message when it received the ack. Processing first and acking after would prevent the loss. The cost is that a crash after processing but before the ack causes a redelivery, so the work may run twice.