Idempotent message sending

|

View as Markdown

A send request can time out, lose its connection, or fail with an error that leaves the outcome unclear. In that case, you cannot tell whether Infobip accepted the message, and a retry might send the message to the end user twice.

Idempotency lets you retry a send request when its outcome is unclear. When enabled for your channels, Infobip uses the messageId you assign to identify retries of a message it has already accepted. Retries with the same messageId within the configured grace period are rejected during processing, so they do not send another copy of the message.

NOTETo use idempotency, prepare your integration and then ask Infobip to enable it for your channels. See Enable idempotency.


How idempotency works

The messageId is your own identifier for a message. Infobip uses it to identify the message in API responses, delivery reports, and logs. When idempotency is enabled, it also uses this value to detect duplicates of previously accepted messages within the configured grace period.


Assign a message ID [#assign-a-message-id-how-idempotency-works]

Set a messageId for each independent message that you submit for delivery, and make sure that the value is unique for that message. If your system can make more than one delivery attempt for the same message, see New delivery attempts.

For requests with multiple destinations, assign a different messageId to each destination. Reuse that destination's ID for every retry of the same message.

A messageId can have up to 200 characters. Some APIs allow fewer characters. For the limit in your API, see the API reference.

These principles apply to all Infobip send APIs. The following request uses the Messages API as an example:

Request
1{
2 "messages": [
3 {
4 "channel": "SMS",
5 "sender": "12345",
6 "destinations": [
7 {
8 "to": "441134960001",
9 "messageId": "order-1234-shipped"
10 }
11 ],
12 "content": {
13 "body": {
14 "text": "Your order #1234 has shipped.",
15 "type": "TEXT"
16 }
17 }
18 }
19 ]
20}

First send attempt [#first-send-attempt-how-idempotency-works]

For the first request with a given messageId, the API responds as usual:

Response to the first request
1{
2 "bulkId": "17942320617291209332207",
3 "messages": [
4 {
5 "messageId": "order-1234-shipped",
6 "status": {
7 "groupId": 1,
8 "groupName": "PENDING",
9 "id": 26,
10 "name": "PENDING_ACCEPTED",
11 "description": "Message sent to next instance"
12 },
13 "destination": "441134960001"
14 }
15 ]
16}

Infobip then processes the message as usual. Use the delivery report to determine the delivery outcome.


Retry with the same message ID [#retry-with-the-same-message-id-how-idempotency-works]

If you do not receive the response and the message outcome is still unknown, retry the same request with the same messageId within the configured grace period. The API responds to the retry like any other request:

Response to the retry
1{
2 "bulkId": "37809234987230952302337",
3 "messages": [
4 {
5 "messageId": "order-1234-shipped",
6 "status": {
7 "groupId": 1,
8 "groupName": "PENDING",
9 "id": 26,
10 "name": "PENDING_ACCEPTED",
11 "description": "Message sent to next instance"
12 },
13 "destination": "441134960001"
14 }
15 ]
16}

The PENDING_ACCEPTED status confirms that the API accepted the retry for processing. Duplicate detection happens afterward, and the duplicate status is returned in a delivery report.

When Infobip processes the retry, it recognizes that a message with this messageId was already accepted, and it does not forward the retry for delivery. Instead, you receive a delivery report for the retry with the status REJECTED_DUPLICATE_MESSAGE_ID. The following shortened example shows only the relevant fields:

Delivery report for the retry (shortened)
1{
2 "results": [
3 {
4 "messageId": "order-1234-shipped",
5 "destination": "441134960001",
6 "status": {
7 "groupName": "REJECTED",
8 "name": "REJECTED_DUPLICATE_MESSAGE_ID",
9 "description": "Rejected duplicate message ID"
10 }
11 }
12 ]
13}

A retry rejected as a duplicate is not charged, and it does not appear in message logs. The delivery report is the only record of it.


Retry flow example [#retry-flow-example-how-idempotency-works]

The following SMS example shows a retry after the response to the first request is lost.

Sequence diagram, SMS example: the API response to the first request is lost and the message is delivered. A retry with the same messageId within the grace period is accepted by the API, then rejected as a duplicate during processing and not forwarded for delivery.
  1. Your application sends the message with messageId X. The API accepts the request, but its response is lost on the way back to your application.
  2. Infobip processes the message. It stores the messageId for the grace period and submits the message to the network, for example a mobile operator.
  3. The network delivers the message, and you receive a delivery report with DELIVERED_TO_HANDSET. This can happen before your application receives any API response.
  4. Within the grace period, your application retries the request with the same messageId. The API accepts the retry and responds with PENDING_ACCEPTED.
  5. During processing, Infobip recognizes the messageId and does not forward the retry for delivery. You receive a delivery report for the retry with REJECTED_DUPLICATE_MESSAGE_ID.

In this example, the end user received the message once, and you were charged once.

NOTEWith any asynchronous API, a delivery report can arrive before the API response, whether or not idempotency is enabled. Make sure that your system can handle a delivery report for a message that has no API response yet.


Deduplication rules

When idempotency is enabled, duplicate detection takes place during message processing, after the API accepts the send request. A retry can therefore receive PENDING_ACCEPTED before being identified as a duplicate. If Infobip has already accepted a message with the same messageId within the configured grace period, it does not forward the retry for delivery and returns REJECTED_DUPLICATE_MESSAGE_ID in a delivery report. This status applies to the retry and does not indicate whether the original message was delivered.


Grace period [#grace-period-deduplication-rules]

Infobip stores each accepted messageId for a configurable grace period, from 1 minute to 24 hours. Specify the grace period you need when you request activation. The grace period applies to your account, and you can request a different grace period for a specific channel. To change it later, contact your account manager or Support.

Choose a grace period that is at least as long as your longest retry window, and make sure that all retries happen within it. Infobip recommends 8 to 24 hours, but the right value depends on your retry policy.

After the grace period ends, Infobip treats a request with an earlier messageId as a new message and sends it.


Accepted messages [#accepted-messages-deduplication-rules]

Only previously accepted messages count toward duplicate detection. If your first request failed for a temporary reason and was not accepted, that failed attempt does not make the retry a duplicate.

Infobip records the messageId when it accepts the message. If delivery fails later, the messageId stays recorded for the grace period, so a retry with the same messageId is still rejected as a duplicate. To try delivery again, see New delivery attempts.


New delivery attempts [#new-delivery-attempts-deduplication-rules]

Idempotency detects repeated submissions of the same message. Because it relies only on the messageId, it also blocks a new delivery attempt that reuses the messageId of an accepted message.

Retrying a submission and making a new delivery attempt are different:

  • Retry of a submission: You do not know whether Infobip accepted the message. Reuse the same messageId, so that Infobip can detect the duplicate.
  • New delivery attempt: Infobip accepted the message, but delivery failed, and you want Infobip to try again. Use a new messageId.

For example, Infobip accepts message msg1234, but delivery fails with EC_ABSENT_SUBSCRIBER. This error is temporary, so you might want another delivery attempt. If you send the message again with msg1234 within the grace period, Infobip rejects it as a duplicate and does not attempt delivery.

If your system can make new delivery attempts for the same message, give each attempt its own messageId. For example, add the attempt number to the ID from your system: msg1234-1 for the first delivery attempt and msg1234-2 for the second. Infobip then deduplicates each delivery attempt independently and charges for each one. For when a new delivery attempt can succeed, see Act on a delivery error.


Uniqueness [#uniqueness-deduplication-rules]

Infobip relies on each messageId to represent what you consider one unique message. The following rules apply:

  • Account-wide check: Infobip checks each messageId against all accepted messages from your account, regardless of destination and content. Each sub-account is checked separately. If you reuse a messageId for a different recipient within the grace period, Infobip rejects the second message.
  • Multipart messages: If you submit the parts of a message as separate requests, Infobip deduplicates each part independently. Each part needs its own messageId.
  • Failover: In Messages API failover, Infobip checks only the first message in the failover chain. If that message is rejected as a duplicate, Infobip does not fail over to the next channel.


Prepare your integration

Before you ask Infobip to enable idempotency, make sure that your integration does the following:

  • Use the same ID for the same message: Every retry must have exactly the same messageId as the first request. Use different IDs for different messages and for different destinations. Do not reuse a messageId for a new message within the grace period, or Infobip rejects the new message as a duplicate. Use a new messageId for each new delivery attempt. See New delivery attempts.
  • Handle delivery reports that arrive before the API response: When a response is lost or slow, the delivery report can reach you first. Match each delivery report to its message by messageId, even if your system has no API response for that message yet. See Webhooks.
  • Retry unclear outcomes within the grace period: Retry on timeouts, connection errors, and 5xx responses, with a backoff between attempts. See Handling errors.
  • Preserve the original message status: A delivery report with REJECTED_DUPLICATE_MESSAGE_ID describes the retry, not the original message. Do not let it overwrite the status of the original message, for example DELIVERED_TO_HANDSET. The two delivery reports can arrive in either order. A duplicate rejection means that Infobip accepted the message earlier, not that the message was delivered.


Enable idempotency

When your integration is ready, contact your account manager or Support and ask them to enable idempotency. Include the following:

  • The channels that you use
  • The grace period that you need, from 1 minute to 24 hours



Was this page helpful?