Send messages

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)
}
Output
sent 01a204b1-decf-75df-b610-74ba173cbf03 at 0
sent 01a204b1-decf-75e3-a6f0-3129930e815d at 1

Each 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.

BodyDefaultEmpty
The message's bytes. Over HTTP, text in body or base64 in body_b64
HeadersDefaultNone
Your name and value pairs. A name that starts with nova- is refused
IDDefaultA new time-ordered UUID
The message's id
NotBeforeDelayDefaultNone
Do not deliver before this time; see delay a message
TTLExpiryDefaultThe queue's message_ttl_ms, 4 days
Drop the message if it is not delivered within this long; see let a message expire

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)
Output
same message: true

queue 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
CostNothingOne metadata write per idempotent send
A repeat after the range moved or split, or its node failedDelivered againRecognized
The 5 minutes count fromWhen the node first read the messageThe first send
A repeat sent over HTTP or gRPCStored, then dropped before deliveryNot stored; the answer repeats the first one's, with "deduplicated": true
A repeat sent by an SDKStored, then dropped before deliveryStored, 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)
Output
reminder stored at 4

A 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)
Output
expiring message stored at 5

Priority

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, HTTPWhenStoredWhat to do
ErrUnknownOutcome, UnknownOutcomeError, 503The connection broke after the send leftMaybeSend again, or use an idempotency key
ErrUnavailable, UnavailableErrorNo node took the send within the retry budget, 30 seconds by defaultNoSend again later
ErrRecordTooLarge, RecordTooLargeError, 400A message holds more than 1 MiB or more than 64 headersNoMake the message smaller
ErrBatchTooLarge, BatchTooLargeError, 400The send holds more than one batchNoSplit it into several sends
ErrMessage, MessageError, 400A nova- header, or an idempotency key that is empty or over 256 bytesNoFix the message
ErrDelayOnKeyOrdered, DelayOnKeyOrderedError, 400A delay on a key-ordered queueNoSend it without a delay
ErrStreamNotFound, StreamNotFoundError, 404No queue at the addressNoCreate the queue

Handle errors lists every queue error.

See also

On this page