Skip to main content

Message and Batch Queues

Definition

This documentation offers an extensive guide to understanding the intricate mechanisms behind the message and batch queue logic.

Message Queue

The message queue incorporates a push function responsible for receiving events. It evaluates the tracking status and event priority. If tracking is enabled and priority demands, the queue triggers a flush action. This compels it to generate a batch from the received events and dispatch this batch to the Batch queue.

In cases where the priority is normal, the queue enters a waiting state until one of the following conditions is met: either the batch size limit is reached, the timeout duration is reached, or a flush is requested. At this point, the queue proceeds to create batches and dispatch them to the Batch queue.

Note: The flush request is only permissible after the initial timeout has been reached.

Batch Queue

The Batch Queue operates via its dedicated pushBatch function, responsible for receiving and queuing batches.

Upon initiation, the Queue conducts checks for enabled tracking and any stored batches in local storage. If batches exist, the service retrieves the records and queues them.

When a batch is pushed into the queue, the service verifies ongoing requests. If the total size of the ongoing request and the current batch size doesn’t exceed 60kb, a fetch request with a keepalive flag is made. Otherwise, the service sets an interval, awaiting resolution of the ongoing request to send the next batch. This process continues until the queue is emptied, and the interval is cleared when no batches remain. Upon new batch addition, the service establishes a new interval.

If the user switches tabs or closes the browser, the queue service will save the queued batches. If there's an opportunity for the service to transmit another batch to the server before this event occurs, it will update and eliminate the corresponding batch from the localStorage record.