Delay Policy
Field-level reference for DelaySeconds, the QueueMessage property that configures delivery delay in the C# SDK.
Which to use
For the task-oriented how-to, see Delayed Messages. This page focuses on the DelaySeconds delay-policy property itself — its evaluation point and interaction with redelivery.
Overview
DelaySeconds is the property on QueueMessage that defers when a queued message becomes visible to consumers — set it before sending, and the broker excludes the message from delivery until the countdown expires. It starts the moment the broker accepts the message, not when the client sends it, and is evaluated once, at send time.
Gotchas: the delay is a floor, not a guarantee — the message becomes eligible when the timer expires, but actual delivery still waits for a consumer to poll, so don't rely on it for precise scheduling. It's one-shot: there's no recurrence or cron-like behavior, so long or repeating delays need application logic on top. And it's independent of redelivery — a delayed message that's later nacked or times out after delivery follows normal visibility-timeout/retry rules, not the original send-time delay.
Prerequisites
- KubeMQ server running on
localhost:50000 - C# SDK installed (
dotnet add package KubeMQ.SDK.CSharp)
Code
// KubeMQ .NET SDK — QueuesStream: Delay Policy
//
// This example demonstrates sending a queue message with a delivery delay
// and receiving via the downstream receiver API. The message becomes visible
// to consumers only after the delay expires.
//
// Prerequisites:
// - KubeMQ server running on localhost:50000
// - dotnet run
using KubeMQ.Sdk.Client;
using KubeMQ.Sdk.Queues;
using System.Text;
await using var client = new KubeMQClient(new KubeMQClientOptions
{
ClientId = "csharp-queues-delay-policy-client",
});
await client.ConnectAsync();
Console.WriteLine("Connected to KubeMQ server");
// Send a message with 5-second delay
var sendResult = await client.SendQueueMessageAsync(new QueueMessage
{
Channel = "csharp-queues.delay-policy",
Body = Encoding.UTF8.GetBytes("Delayed notification"),
DelaySeconds = 5
});
Console.WriteLine($"Sent delayed message (5s delay): {sendResult.MessageId}");
await using var receiver = await client.CreateQueueDownstreamReceiverAsync();
// Immediate receive — should not find the message yet
var immediate = await receiver.PollAsync(new QueuePollRequest
{
Channel = "csharp-queues.delay-policy",
MaxMessages = 1,
WaitTimeoutSeconds = 1,
AutoAck = true,
});
Console.WriteLine($"Immediate receive: {(immediate.HasMessages ? "found" : "empty (expected)")}");
// Wait for delay to expire, then receive again
Console.WriteLine("Waiting 6 seconds for delay to expire...");
await Task.Delay(6000);
var delayed = await receiver.PollAsync(new QueuePollRequest
{
Channel = "csharp-queues.delay-policy",
MaxMessages = 1,
WaitTimeoutSeconds = 5,
AutoAck = false,
});
if (delayed.HasMessages)
{
Console.WriteLine($"Delayed receive: {Encoding.UTF8.GetString(delayed.Messages[0].Body.Span)}");
await delayed.AckAllAsync();
}
Console.WriteLine("Done.");
How It Works
DelaySeconds = 5onQueueMessagesets a server-side delivery delay. The broker holds the message and makes it visible to consumers only after the delay expires.- The first
PollAsyncruns immediately withWaitTimeoutSeconds = 1— it times out and returns empty because the message is still hidden. - After
Task.Delay(6000)the delay has expired, and the second poll finds and consumes the message. DelaySecondsis evaluated from the time the message is received by the broker, not from when it is sent by the client.
Related
- Delayed Messages — task-oriented walkthrough for sending delayed messages
- Pattern overview
- C# SDK Reference
- Send & Receive
- Ack All
Was this page helpful?