Many task libraries offer 2 kinds of message acknowledgements: early and late. Late is superior to early, but none is fool-proof and it comes with strings.

In this post

The contract

The exact terms of the broker’s contract are: I, the broker, will hold your message/task for as long as no worker acknowledges said message/task.

I think we all fully understand this statement. The thing we often miss is the natural consequence of this statement: once a worker acknowledges it, the broker deletes said message/task. “Deletes” as in actually deletes it. Unless we have put systems in place on our side to keep a trace of the task.

ack-early-late

The benefits and the drawbacks

  • Early ack: The task will run at most once. Could be 0×, could be ½×.
  • Late ack: The task will run at least once. Could be 2×, could also be 3× ½, 1× ¼, and 1× for real.

In a theoretical system, the task would run precisely 1× in both cases. It is the unpredictability of the real world, where workers dare to crash mid-work, that creates the need for re-runs.

The question you should ask yourself is: “do I prefer that my task occasionally doesn’t run at all or that it occasionally runs two times?”.

Late ack is the preferred solution

Most often we want the late ack.

If the task was supposed to send an email, and it didn’t, then let it try again. If the task was supposed to subscribe a user, and it didn’t, then let it run again. If the task was supposed to report the request time, and it didn’t, then let it run again (I think).

But what about a complex task? A task that subscribes the user, then sends an email, then reports some metrics. Maybe what we’d want in the ideal world is to acknowledge right after the email is successfully sent, because we never want to send 2 emails, even if we have to pay with missing metrics. But will we really maintain a bunch of tasks, where the ack can be in between any 2 lines? We’ll probably keep this power-feature for only very delicate tasks.

ack-in-the-middle

What we need to keep in mind for late ack is: the “other” runs will be partial. We aren’t talking about the task running twice from start to end. This usually changes the mental model people have.

The biggest overlooked consequence of late ack

If we want to truly use late ack, then we must make sure our tasks are truly idempotent. This means that running them once or running them five times must produce the same side effects.

This sounds like such a simple limitation, but it’s actually a can of worms.

Idempotency must not be checked only once, at the beginning of the task, but before every action.

There are levels to idempotency. But the deeper you go, the more you will fear you are over-engineering. It’s about balancing the code complexity with the frequency of worker-failure. Nothing in our system is 100% reliable nor can it be 100% reliable.

Idempotency level 1

If your task is simple, and it does only 1 thing, then theoretically that 1 thing can be in only 2 states: done or not done.

So, idempotency can be achieved with 1 simple if-statement: if not_done: do_it().

Even better, some operations (like SQL UPSERT, or the SQL INSERT + unique constraint) don’t even need the if, because they themselves are already idempotent.

idempotency-level-1

Idempotency level 2

But tasks quickly have more than 1 action. Now, we need to guard every action, because the previous run might have died anywhere in the chain!

It’s not enough to check the beginning of the task, if it has been run already. We must assume that some number of steps have already successfully completed, but the others have not.

idempotency-level-2

Idempotency level 3

What about tasks with an API call? Or any call to a separate system, like SMTP for sending emails.

Here, calling the API and recording that the call was made are 2 steps, 2 independent steps.

idempotency-level-3

There is no great solution here. We can save before sending and accept a rare missing email, or we can save after sending and accept a rare duplicate email. A duplicate welcome email is harmless, a duplicate payment email hurts the company’s reputation.

Or we can go crazier: if our email provider accepts an idempotency key, we can mark the email with some uniqueness key like welcome-email-user-42, and the provider will ignore a second request with the same key (for a while). This way, we can re-run the task and re-send the email and be pretty sure the user did not get a duplicate.

Idempotency level 4

Sometimes the timing matters. What if the user subscribed and then 15 mins later she unsubscribed, but our subscribe task was lost somewhere and is only now about to run?

idempotency-level-4

This means that our old subscribe-action-gate is inadequate. We need to add a check “is this action still needed?”.

We can’t modify the task that is waiting in some queue, but we can store the user’s actions separately, with a timestamp… making sure that the order of operations is correct or that one button click can cancel a previous button click.

The background task libraries don’t guarantee any particular order. Sure, the brokers line the messages in a FIFO queue, but once you have several workers taking from the same queue, the order gets muddled. Logs like Kafka guarantee order, but then again, only within one partition.

Now, I’m imagining a developer who gets the ticket “Implement unsubscribe API”. Are they going to think of this ordering problem? Probably only if they witnessed things, bad things. Who in their right mind will remember to raise the concern: “what if a rogue subscribe task is still out there”?

Coding projects are so vast that we can’t possibly keep their whole influence graph in our mind at once. We agreed decades ago that the best path forward is to split them into fully independent sub-projects, to keep the graph small and the focus limited.

In reality, of course, the flap of a butterfly’s wings can presumably trigger a tornado.

Sometimes the ack’s timing is off

The broker can’t completely leave it up to the worker and wait for ack forever. It needs to hold onto every task until the ack arrives.

The broker can’t tell a dead worker from a very busy, thus very slow, worker.

This is why there usually is a master time limit. If the ack doesn’t arrive within this limit, the broker assumes the worker has a problem and passes the task to the next free worker.

This adds another layer of complication to our tasks, because now the same task might be running on 2 workers simultaneously. Can our task handle that? Can we extend our idempotency guards to handle this?

ack-too-late

We once had to raise RabbitMQ’s limit to 24 hours, because some of our tasks ran for hours. But a long limit has its own price: if a worker really does get stuck, its task also waits that long before anyone picks it up again. Which brings us back to: just don’t have long-running tasks. :-P

How often do tasks die mid-run?

Seeing how re-runs are a difficult topic, let’s shift the focus to:

  • how often does a worker die?
  • why would a worker die?
  • why would a task die, if the worker lives on?

Deploys

Every time we deploy, we start new workers and shut down the old ones.

This is usually not done all too gracefully. It of course depends on your custom settings, but … I’d bet most of us are using the pattern send SIGTERM (=wrap up), wait 30s, then SIGKILL. The SIGKILL forces an immediate termination by the kernel. Any task that is mid-work gets killed.

We can increase the wait time, but then we increase the deploy time too, and old and new code run side by side for longer. It’s not realistic to increase this to more than a few minutes, but we probably have some long-running tasks that need more than an hour. We can never cover for those.

Deploys have to be fast, because sometimes we need to deploy a bug fix, and we can’t run the bad code for another 2h, because a report task is processing some data in the background.

Scaling down

This follows the same pattern, SIGTERM, then SIGKILL. This is arguably worse than a deploy,

  • because it isn’t triggered by a deliberate developer action,
  • because it can happen lots of times per day and
  • because the autoscaler can easily be misconfigured: maybe it focused solely on queue length, which is 0, while the worker is busy with a long running task.

Out of memory (OOM) kills

A task fetches too many rows from the database or loads a file that’s too big, the container hits the memory limit, and the kernel kills the worker.

Time limits

Some libraries have a time limit for how long a task is allowed to run. If it runs longer, it is assumed the task is stuck, so the task’s process gets killed.

Health checks

Our infrastructure (Kubernetes) does regular liveness checks. But if the worker is busy, like really busy, and doesn’t answer in time, then the infrastructure concludes the worker is dead and restarts it.

Real crashes

Funnily, this one is (usually) the least common, but it can happen. A segfault in some native library, or some bug that takes the whole process down with it.

why-tasks-die

As we see, these events aren’t as rare as we’d like them to be.

We can design our system with more resilience in mind, and make these events less frequent, but… some will still happen.

This is why late ack is still the more prudent way forward.

And in a log (Kafka)

A log (like Kafka) usually doesn’t ack single messages. Every consumer remembers how far it got (its offset) and commits that offset now and then. It’s an ack for a whole batch.

By default, it commits periodically, every 5s. However, this doesn’t change the underlying decision that needs to be made: we can only put the commit before the work or after it.

Next chapter

⏭️ To be continued...