Send messages
Send messages to a queue under a routing key, once with an idempotency key, with a delay, or with an expiry.
A send adds messages to a queue. Use Send / send by default: it stores one or more messages under one routing key. Use SendIdempotent / sendIdempotent when you may send the same message again, such as on a retry: the queue then delivers it once. A message can also wait before it is delivered, or expire. The API has the send route, and the CLI cannot send. The examples use the queue demo/emails from Manage queues.
Send messages
A send stores its messages together, in the order given, or stores none of them. It returns once they are stored.
queue := c.Queue(client.Address{Bucket: "demo", Key: "emails"})
sent, err := queue.Send(ctx, "user-42",
client.Message{Body: []byte("welcome")},
client.Message{
Body: []byte("receipt"),
Headers: []client.Header{{Name: []byte("content-type"), Value: []byte("text/plain")}},
},
)
if err != nil {
return err
}
for _, s := range sent {
fmt.Println("sent", s.ID, "at", s.Seq)
}sent 01a204b1-decf-75df-b610-74ba173cbf03 at 0
sent 01a204b1-decf-75e3-a6f0-3129930e815d at 1Each result holds the message's id, the range that stores it, and its position (seq) there. A message keeps its id on every attempt, so a worker uses it to recognize a repeat. Its range and position change when Nova retries, delays, or replays it, because each of those stores a new copy. Set your own id to tie a message to a row of your database.
Routing keys
The routing key groups messages: those with the same key go to the same range. It is the unit the queue shares its workers fairly across, as fairness explains, and the unit of order on a key-ordered queue. Use a user, tenant, or account id. A send with an empty key goes to one range picked at random, and its messages count against no key's limit.
Send size
A send is one batch: at most 1,000 messages and 1 MiB of bodies and headers by default. The queue adds its own nova- headers to each message, about 80 bytes with a short routing key, and they count too. Through an SDK, a message holds at most 64 headers today, counting the 2 to 6 the queue adds, so with a routing key you can add 61 of your own. Over HTTP, keep to 64 of your own: through the Gateway, a message with more fails with 500. The SDKs refuse a message or a send that is too large before sending it, and nothing is stored. To send more, make several sends. The queue API lists the headers the queue adds.
Send with an idempotency key
A send whose outcome you did not learn may have been stored. To send it again safely, give it an idempotency key: a repeat with the same key within 5 minutes is the same message, and the queue delivers it once.
confirmation := client.Message{Body: []byte(`{"order":"981"}`)}
first, err := queue.SendIdempotent(ctx, "order-981-confirmation", "user-42", confirmation)
if err != nil {
return err
}
// A repeat under the same key, such as a retry, is the same message.
again, err := queue.SendIdempotent(ctx, "order-981-confirmation", "user-42", confirmation)
if err != nil {
return err
}
fmt.Println("same message:", first.ID == again.ID)same message: truequeue is the handle from the first example.
An idempotent send carries one message. The key is 1 to 256 bytes, and the message id is derived from it, so leave the id empty. The SDKs take the idempotency key first and the routing key second. Without a routing key, the idempotency key picks the range, so a repeat reaches the same range. The SDKs retry an idempotent send through every passing failure, because a repeat is harmless.
Dedup policies
The queue's dedup policy, its dedup setting, decides how far the promise holds:
memory (the default) | durable | |
|---|---|---|
| Cost | Nothing | One metadata write per idempotent send |
| A repeat after the range moved or split, or its node failed | Delivered again | Recognized |
| The 5 minutes count from | When the node first read the message | The first send |
| A repeat sent over HTTP or gRPC | Stored, then dropped before delivery | Not stored; the answer repeats the first one's, with "deduplicated": true |
| A repeat sent by an SDK | Stored, then dropped before delivery | Stored, then dropped before delivery |
You can change the policy on a running queue; see update a queue. Delivery is still at least once under both, so a worker must handle a repeat.
Delay a message
Set a not-before time, and Nova stores the message at once and delivers it at or after that time. A delay is not a failed attempt, so the message arrives as attempt 1.
sent, err := queue.Send(ctx, "user-42", client.Message{
Body: []byte("reminder"),
NotBefore: time.Now().Add(10 * time.Minute), // not delivered before then
})
if err != nil {
return err
}
fmt.Println("reminder stored at", sent[0].Seq)reminder stored at 4A key-ordered queue refuses a delay, because one delayed message would hold back every later message of its key. A delay longer than the message's expiry drops the message when the delay ends.
Let a message expire
Give a message a TTL, and Nova drops it instead of delivering it once it is older. Without one, the queue's message_ttl_ms applies: 4 days by default, and at most 14 days. Age counts from the first send, and a retry does not reset it. An expired message does not go to the dead-letter stream.
sent, err := queue.Send(ctx, "user-42", client.Message{
Body: []byte("flash sale ends soon"),
TTL: time.Hour, // dropped if not delivered within an hour
})
if err != nil {
return err
}
fmt.Println("expiring message stored at", sent[0].Seq)expiring message stored at 5Priority
Nova has no per-message priority, and a send that sets one is refused. To have urgent messages handled first, send each level to its own queue, such as emails-urgent beside emails. A worker then claims from the urgent queue first, and from the other only when the urgent one returns nothing. Each level also gets its own retry policy, claim TTL, and limits.
When a send fails
Nova does not retry a send for you. The SDKs retry one only while it cannot have been stored, and fail with an unknown outcome when it may have been. Then send it again and accept a duplicate, or use an idempotency key from the start:
welcome := client.Message{Body: []byte("welcome")}
_, err := queue.Send(ctx, "user-42", welcome)
if errors.Is(err, client.ErrUnknownOutcome) {
// It may be stored. Sending again can deliver it twice.
_, err = queue.Send(ctx, "user-42", welcome)
}
if err != nil {
return err
}
fmt.Println("sent")| Error: Go, TypeScript, HTTP | When | Stored | What to do |
|---|---|---|---|
ErrUnknownOutcome, UnknownOutcomeError, 503 | The connection broke after the send left | Maybe | Send again, or use an idempotency key |
ErrUnavailable, UnavailableError | No node took the send within the retry budget, 30 seconds by default | No | Send again later |
ErrRecordTooLarge, RecordTooLargeError, 400 | A message holds more than 1 MiB or more than 64 headers | No | Make the message smaller |
ErrBatchTooLarge, BatchTooLargeError, 400 | The send holds more than one batch | No | Split it into several sends |
ErrMessage, MessageError, 400 | A nova- header, or an idempotency key that is empty or over 256 bytes | No | Fix the message |
ErrDelayOnKeyOrdered, DelayOnKeyOrderedError, 400 | A delay on a key-ordered queue | No | Send it without a delay |
ErrStreamNotFound, StreamNotFoundError, 404 | No queue at the address | No | Create the queue |
Handle errors lists every queue error.