Scheduled messages and delayed messages both defer delivery to consumers. The difference is how you specify the timing:
Scheduled message: you specify an absolute timestamp. The broker holds the message and delivers it at that exact point in time.
Delayed message: you specify a relative duration. The broker holds the message and delivers it after that duration elapses from the time it was sent.
The underlying mechanism is the same. The broker stores the message and releases it to consumers when the target time arrives.
Use cases
Order timeout handling: When a user creates an order in an e-commerce checkout flow, send a delayed message with a 30-minute delay. After 30 minutes, the consumer checks whether payment is complete. If not, it cancels the order. If payment succeeded, it ignores the message.
Timed notifications: Send a scheduled message set to a specific time -- for example, 9:00 AM the next day -- to trigger a push notification or reminder.
Set the delivery time
Both message types use the same parameter: msg.setStartDeliverTime, which accepts a millisecond-precision Unix timestamp.
Scheduled message: pass the absolute timestamp of the target delivery time.
Delayed message: pass
System.currentTimeMillis() + delayMillis, wheredelayMillisis the delay duration in milliseconds.
Code examples
Delay a message by 10 minutes from now:
// Calculate the delivery timestamp: current time + 10 minutes
long deliverTime = System.currentTimeMillis() + 10L * 60 * 1000;
msg.setStartDeliverTime(deliverTime);Schedule a message for a specific date and time:
// Schedule delivery for 2024-06-15 14:30:00 (UTC+8)
// Millisecond Unix timestamp: 1718433000000
long deliverTime = 1718433000000L;
msg.setStartDeliverTime(deliverTime);Rules and limits
| Rule | Details |
|---|---|
| Minimum delivery time | Must be later than the current timestamp. If the specified time is in the past, the message is delivered immediately. |
| Maximum scheduling window | 40 days from the current time. Messages that exceed this window fail to send. |
| Time unit | Milliseconds |
| Topic type requirement | The topic must be created with Message Type set to Scheduled/Delayed Message in the ApsaraMQ for RocketMQ console. A topic only accepts messages that match its configured type. |
| Message retention | Up to 3 days. For example, if a message is scheduled for delivery in 5 days and is not consumed, it is deleted on the 8th day (5-day delay + 3-day retention). |
Delivery precision
Scheduled and delayed messages are not guaranteed to arrive at the exact specified time due to the following factors:
Baseline latency: expect 1 to 2 seconds of delivery latency.
Message backlog: if accumulated messages exist in the queue, scheduled and delayed messages are queued behind them and may arrive later than the target time.
Clock skew: the
StartDeliverTimevalue is evaluated against the broker clock. If the client clock differs from the broker clock, the actual delivery time may not match the timestamp set by the producer.
Sample code
For complete producer and consumer examples, see the following guides organized by protocol and language.
TCP
| Language | Link |
|---|---|
| Java | Send and receive scheduled messages |
| Java | Send and subscribe to delayed messages |
| C++ | Send and subscribe to scheduled messages |
| .NET | Send and receive scheduled messages |