Difference between FireAndForget and Async behavior for publishing

There’s a main difference comparing fire and forget vs calling an async operation and not awaiting it.

Fire and forget means that not only you’re not waiting for the result but you don’t care if it works or not, while an async operation may throw an exception once it has ended if something goes wrong.

In the other hand, when you issue a fire and forget command, StackExchange.Redis doesn’t try to retrieve the command result internally, which is better if you just want the so-called fire and forget behavior when issuing commands.

You may check this difference if you open ConnectionMultiplexer source code and you see how ExecuteAsyncImpl / ExecuteSyncImpl methods are implemented:

// For example, ExecuteAsyncImpl...
if (message.IsFireAndForget)
{
  TryPushMessageToBridge(message, processor, null, ref server);
  return CompletedTask<T>.Default(null); // F+F explicitly does not get async-state
}
else
{
  var tcs = TaskSource.CreateDenyExecSync<T>(state);
  var source = ResultBox<T>.Get(tcs);
  if (!TryPushMessageToBridge(message, processor, source, ref server))
  {
    ThrowFailed(tcs, ExceptionFactory.NoConnectionAvailable(IncludeDetailInExceptions, message.Command, message, server));
  }

  return tcs.Task;
} 

Answer to some OP comment

Hi. Thanks for your answer. We know the purpose of the commands, what we would like to know is if it has any differrence internally or any risk of behaving differently (execution order etc.)

Since the async operation won’t be finished when you publish the message on the Redis channel, it can happen that you publish the message and the operation gets executed never. You lose a lot of control.

When you send a fire and forget command, it mightn’t be executed too, but you know that the try was done before you publish the channel’s message. Therefore, you shouldn’t use async operations to implement fire and forget pattern when using StackExchange.Redis.

You may check this other related Q&A: Stackexchange.redis does fire and forget guarantees delivery?

Leave a Comment