celery icon indicating copy to clipboard operation
celery copied to clipboard

1%-2% Data loss using apply_async

Open philejmath opened this issue 12 years ago • 3 comments

I'm processing anywhere from 200k-400K messages per day with a celery based pipeline. Everything runs very smoothly augmenting messages, inserting into elastic search and Cassandra. There are rarely errors in any of my logs files for Cassandra, Elastic Search, Celery, and RabbitMQ. Yet, every single day I end up dropping 2K - 4K records on the floor.

The tasks/queues I'm using look like this.

Persist -> Augment -> Index

Simple right? My logs show every message completes both the Persist and Augment tasks. The last log entry for every messages that gets dropped is the following (from Augment task) :

Result of pushing 1e4701e4a8c5ac80e0728fa632968e14 to index: f2791d16-69a3-4163-acd7-04c53364384f

Where 1e4701e4a8c5ac80e0728fa632968e14 is my internal id and f2791d16-69a3-4163-acd7-04c53364384f is the rabbit correlation_id/celery task id

Here's the very simple code of the task that writes that log entry. This works 100% of the time and result is never anything but a task_id. Yet 2% of these messages never make it to their destination.

augmented_message = augment(message)
result = index.apply_async(args=[augmented_message], queue='index')
augment.log.info("Result of pushing {0} to index: {1}".format(augmented_message['id'], result))

For messages that aren't dropped I can then look in my Rabbit trace log and find the correlation_id and see the internal id in the Index log has been pushed to ES.

For dropped messages there is no record anywhere of the correlation_id. It's just gone. The augment task completes successfully, tells me it's pushing the data on and then nothing. The messages don't make it to the index queue at all and there's no indication in any of the log files as to why.

I'm fairly new at this so I'm guessing there's something I just don't understand about Celery. Here's my config:

BROKER_HEARTBEAT = 10 CELERY_ACKS_LATE = True CELERYD_PREFETCH_MULTIPLIER = 1 BROKER_POOL_LIMIT = 500 CELERY_TASK_RESULT_EXPIRES = 30 * 60

The application I'm building cannot lose any message, ever.

What next steps can I take to figure out why these messages are being dropped?

Thank you!

philejmath avatar Nov 19 '14 19:11 philejmath

I would guess that the persistent connection is dropped for some reason, but the client is not notified of this, perhaps because of an intermediate router or proxy.

There's no quick solution for this.

Heartbeats can be used to detect that the connection was closed, but it would take some time for that to happen, and even so you would not be able to know what messages were completed to resend the lost.

Using publisher acknowledgements is the safest solution, but comes at a performance cost since you need to wait for confirmation after each message you send. See https://www.rabbitmq.com/confirms.html

Another option that would help for most cases (but not be 100% safe) would be to set BROKER_POOL_LIMIT=0, to create a new connection for every message sent. I'm not sure what has better performance, confirms or opening connections.

ask avatar Nov 19 '14 19:11 ask

thank you for your comment!

using both suggestions I was able to reduce my dropped documents to 0

Here's the config options I used.

BROKER_TRANSPORT_OPTIONS = {"confirm_publish": True} BROKER_POOL_LIMIT=0

thanks again!

philejmath avatar Nov 20 '14 14:11 philejmath

Great! Please keep in mind that confirm_publish only works with pyamqp://, so using amqp:// with librabbitmq installed means it will be silently disabled.

ask avatar Nov 20 '14 15:11 ask