Brian Rak
Brian Rak

Reputation: 5124

Azure Storage Tables: Is it okay to fire-and-forget async methods?

I have a method in my MVC web app that cleans up old data from Azure Table Storage like abandoned user sessions. I'm batching up my work into TableBatchOperation objects which I'm executing with CloudTable.ExecuteBatchAsync. I don't need to wait around for the results, and if the operation fails for some reason like network trouble, it doesn't matter because I can just try again later. Is it okay to just call the method without an await or anything and walk away?

That is to say, is this code going to cause problems?

List<TableBatchOperation> batches = { ... }
foreach (TableBatchOperation batch in batches) {
    table.ExecuteBatchAsync(batch);
}

// Method ends here

Or do I need to capture all the return objects and call Wait() on them?

Upvotes: 3

Views: 997

Answers (1)

usr
usr

Reputation: 171178

You can do this in the sense that it works and does what you want. You don't care about the potential for lost work.

You still probably want error handling and logging. Imagine you have set up the storage configuration wrongly and all work items fail. You probably want to know about that.

Wrap table.ExecuteBatchAsync(batch) in an async function where you catch and log exceptions. Discard the task returned by that function. (Alternatively, attach a continuation but I find that to be worse code.)

Also note, that resource use it unbounded. If this loop is iterated many times then many operations queue up or run concurrently. This can be a problem. If you expect low load it is valid to take this risk.

Upvotes: 2

Related Questions