Azure Service Bus .NET SDK Deep Dive – Scheduling

Explains scheduling of messages into the future, for more posts in this series go to Contents.

What if we could write a letter to our future self? It turns out with Azure Service Bus we can! With the SDK, it is possible to schedule a message to be due at a certain point in time, and the broker will make sure to deliver it back into the queue when the date is due. With that trick, we can store durable timeouts on the broker to kill our batch jobs or implement things like invoice reminders.

 await using var sender = serviceBusClient.CreateSender(destination);
 var due = DateTimeOffset.UtcNow.AddSeconds(10);
 await sender.ScheduleMessageAsync(
   new ServiceBusMessage($"Deep Dive + {due}"), due); 

The above code snippet shows how to send a message ten seconds into the future by using the ScheduleMessageAsync method on the ServiceBusSender. When the above code is executed for a period of roughly ten seconds, the message is not “consumable” in the destination queue. Only after the due time has elapsed the message becomes visible in the destination queue and is ready for consumption.

Scheduling messages into the future is not something you should use to implement some sort of real-time requirement. Because effectively scheduling just means that the message will not be visible up to the due time defined by the sender of the message, but that doesn’t mean that the receiver will process it after the due time. For example, if we schedule a message to be delivered within ten seconds to the destination queue but the destination queue has 20 messages already enqueue that represent work of one minute. Scheduling a message then simply means that after the due date, the message is added to the back of the queue and will not be processed until all the other already enqueued messages are processed, which in this example would mean after one minute. Of course, this assumes that the receiver is continuously online and not shut down for maintenance purposes.

What if, for some reason, we need to cancel an already scheduled message? Only the sender of a scheduled message can cancel the message. ScheduleMessageAsync returns a sequence id of type long that can be used to cancel the message that is represented by the sequence id

 var sequenceId = await sender.ScheduleMessageAsync(
   new ServiceBusMessage($"Deep Dive + {due}"), due);
 await sender.CancelScheduledMessageAsync(sequenceId);

You can imagine that if you want to leverage cancellation of scheduled messages you need to make sure to store the sequence id on the sender side for future use should you wish to cancel a scheduled message. Let’s see the code in action.

The video shows how a message is scheduled ten seconds into the future, and a second message that is scheduled is canceled shortly after. The video uses time lapse to speed things up. Only one scheduled message arrives after the due time is over.

Updated: 2021-03-23 to use the new SDK

About the author

Daniel Marbach

1 comment

By Daniel Marbach

Recent Posts