How to Calculate Your Message Queue Length

September 07, 2021
Written by
Reviewed by
Alan Klein
Twilion

Message Queue Length Header

We’ve seen quite a few reasons where it’s useful to monitor message queue lengths outside Messaging Insights in the Console. However, most of the use cases we’ve seen fit into a couple of categories.

First, ISVs might not want to provide their customers access to the Twilio Console. However, understanding queue health in real-time will allow customers of ISVs to strategize and take full advantage of their messaging platform.

Second, direct customers might prefer not to give all employees access to the Twilio Console. For example, marketing team members responsible for managing campaigns might not have access to the Twilio Console and won’t then have access to the Messaging Insights dashboard. They may need the information for planning purposes.

In this post, we’ll look at how to calculate the message queue length so you can expose it without giving access to Message Insights in the Console.


How many messages can I send in X minutes?

Let’s assume we are using a regular short code to send outbound messages. Generally, a short code has a throughput of 100 message segments per second (MSPS). For more information on what a segment is, reference our post What the heck is a Segment?

Let’s calculate how many message segments can be sent using a short code in 5 minutes.

5 minutes = 5 * 60 seconds = 300 seconds

Assume we are using a short code which supports 100 messages per second (MPS) to send these messages. In that case, in 300 seconds we can send:

300 seconds * 100 segments = 30,000 segments

Customers can procure a combination of Sender IDs (A2P 10 digit long codes, toll-free, high throughput toll-free, short codes, alphanumeric) to achieve optimal delivery rates. Twilio’s Messaging Service allows customers to create a pool of phone numbers that handles scaling issues and optimizes the throughput needed for business outcomes.

Reference the Messaging scaling best practices guide to see how to plan for queueing and latency during high traffic events such as Black Friday.

The total throughput of a Messaging Service is dependent on the Sender IDs added to the Messaging Service. This throughput should be used to calculate the number of messages you can send in X minutes.

In an alternate scenario, imagine you’re using US A2P 10DLC Long Codes to send messages in the US – you will need to calculate the throughput based on the campaign throughput visible in your messaging service. If it is 30 message segments per second, then all the US Long Codes in that Messaging Service will in total be able to send 30 segments per second.

For more information on A2P 10DLC throughput, reference Message Throughput (MPS) and Trust Score for A2P 10DLC in the US.


How many message segments are in the message body? 

A single API request can have multiple message segments in the message body.  ISVs can use the message-segment-calculator codebase to calculate the number of message segments in the message body. Twilio’s Message Segment Calculator was built on top of the codebase.

Additionally, when your application makes a POST API request to Twilio to send a message, Twilio returns a num_segments property value in the HTTP 201 (Created) Response. This property (num_segments) can be used to determine the total number of message segments in the body of the message. You may choose to store the number of segments per message in a database.

To learn more about how to build custom usage reports, reference How To Track and Report Your Twilio Usage.

To learn more about the property values included in the HTTP Response from Twilio, reference our Programmable SMS Quickstart and How to calculate num_segments when sending SMS using Messaging Service?

The total number of segments enqueued is the sum of the number of segments in each successful API Request.


How do we calculate the queue length?

Twilio provides visibility into the messages that are queued in Twilio’s platform. Twilio's Messaging Insights Latency report in the Twilio Console gives customers access to the messages currently waiting in the queue to be sent.

Calculation of message queue length in the Twilio Console

Customers of our ISVs probably do not have access to the Twilio Console, however, these data points could be used to prioritize how to queue messages or used to plan a sending strategy. Some examples include:  

  • Scheduling/Planning for high traffic events such as Elections or Black Friday
  • Prioritizing 2FA code delivery over Marketing messages
  • Batching messages to avoid delays in delivering high priority messages

ISVs can build logic to calculate these metrics and share them with their customers.    

Number of Messages in the Queue

You can calculate the number of messages currently in the queue by subtracting the number of messages of status “Sent” and the number of messages of status “Failed” from the total number of API calls made to send the messages.

Below is the formula that can be used to determine the length of the queue in real time.

Message Segments Currently in the Queue =  Total number of successful Message Request API calls made - Number of Messages 'Sent' - Number of Messages Segments 'Failed’

A status callback should be configured to track the status (Sent, Failed etc.) of the messages in real time.

Number of Message Segments in the Queue

You can calculate the number of message segments currently in the queue by subtracting the number of message segments of status “Sent” and the number of message segments of status “Failed” from the sum of the number of segments in each successful API call. 

Below is the formula that can be used to determine the length of the queue in real time:

Message Segments Currently in the Queue =  Sum of Number of Segments returned by Twilio in the response - Number of Message Segments 'Sent' - Number of Message Segments 'Failed’

A status callback should be configured to track the status of the messages in real time. To determine the number of segments in the message that are ‘Sent’ or ‘Failed’, you can either make a GET request to the Message API resource for that message, or query your database if available. Reference the “How many message segments are in the message body” section of this blog post for more information on how to calculate the number of segments.


Example calculation of messages in the queue

Let’s consider an example:

  • Your application makes 1200 API calls to send 1200 messages with 1 segment each at 1:00pm using a toll-free number. As of now, a regular toll-free number has a throughput of 3 Message Segments Per Second (MSPS).
  • Your application makes API calls to Twilio to deliver these message segments. Twilio will receive the API requests and start queueing the message segments.
  • The messages go through multiple stages while Twilio processes the request.
  • You can configure a status callback to track the status of the messages.

Twilio message statuses flow chart

A message segment is considered to be out of the queue once you receive a status callback of “Sent” for a message. If a message segment fails to queue, you will receive a “Failed” status callback.

To calculate the number of message segments currently in the queue, we subtract the number of message segments with “Sent” or “Failed” status from the total number of API calls made.

Assuming that we used a toll free number to send 1200 message segments, with a throughput of 3 MSPS (message segment per second):

API Calls made  = 1200

Status callback webhook received:

Message Segments ‘Sent’ = 28 and

Message Segments ‘Failed’  = 2

Using the formula above,

Message segments currently in the Queue at 1:00:10 pm = 1200 - 28 - 2 = 1170 segments

Example calculation of total queue capacity

The total message segments that you can fit in a queue are dependent on the throughput of your phone number or the total throughput of messaging service. Each queue can hold up to 4 hours' worth of message segments. To learn more, reference Understanding Twilio Rate Limits and Message Queues.

For our example above, we used a Toll Free number (3 MPS) to send messages. The total available queue length can be calculated as shown below:

Queue Capacity = 3MPS * 60 Seconds * 60 mins * 4 Hrs = 43200 segments

Continuing our example from above, we have 1170 segments currently in the queue. Therefore, the unused queue length is:

Unused Queue Length = Queue Capacity - Message segments currently in the Queue

Unused Queue Length = 43200 - 1170 = 42030 segments

If you are using a Messaging Service with a combination of Sender IDs then you must identify the total combined throughput and use that to calculate the queue length.


Queue Length (time)

To calculate the amount of time it will take for the message segments to leave the queue, use the formula below:

Total number of messages currently in the queue (calculated above)/Total Throughput

1170 segments/3 MPS=  390 seconds

If you are using a Messaging Service, Total Throughput is calculated by adding the throughput of all the phone numbers in the Messaging Service. Please note that different phone numbers (A2P 10DLC, Toll Free, Short Code, AlphaNumeric) have different throughput limits.

Calculating message queue length outside the Twilio Console

Now you’ve learned how to calculate your message queue length without having to share – or log into – the Twilio Console. For a more straightforward solution to scaling messaging, check out Twilio’s Messaging Service which handles scaling issues and optimizes your throughput automatically.

Diksha Chawla is a Senior Solutions Engineer. She loves being a trusted advisor and is focused on helping customers build creative and scalable solutions on Twilio. Her Uber rating as of writing this post is 4.84. You can reach her at dchawla [at] twilio.com.

Binesh Dhami is a Solutions Engineer for the Growth team. He loves to ask probing questions and understand the customers set up, and above all doesn’t settle with superficial answers. You can reach him at bdhami [at] twilio.com