<a id="queuing-subsystem"></a>

# Queuing subsystem

Halon provides control and flexibility on how messages are handled and delivered,
according to policies that you define. The MTA queue is central to this.

The default behavior of an MTA is to spool messages, which are being received, to disk,
then pick them up and deliver them by using the queuing subsystem. This allows the MTA to
accept messages even if the next hop is unavailable, or if a message has multiple
recipients on different next hop destinations.

<a id="activity-diagram-label"></a>

This figure illustrates the typical states and movements inside the queue.

```mermaid
flowchart LR
    smtp{{SMTP}} --> spool[(Spool)]
    http{{HTTP}} --> spool
    api{{API}} --> spool
    start((Startup)) --> spool
    spool --> spoolin(Spool-in)
    spoolin --> pre(Pre-delivery hook)
    spoolin -->|if now < next retry|defer[(Defer queue)]
    spoolin -->|if message on hold|hold[(Hold queue)]
    pre <--> defer
    pre <--> hold
    pre --> dns(DNS resolving)
    dns -->|DNS error|post(Post-delivery hook)
    dns --> active[(Active queue)]
    active-->|SMTP delivery attempt|post
    post --> defer
    post --> hold
    post --> done{{Message Done}}
```

<a id="queue-states"></a>

Each message in the queue can be in one of three states:

Active
: Messages that are not on hold, and are scheduled for delivery now (or in the past).
  Messages that are put in [queue()](https://docs.halon.io/hsl/archive/6.12-stable/eod.html#EODMailMessage.queue) for delivery end up directly in the
  active queue (unless the “delay” option is used). Messages are also moved automatically
  from the defer queue into the active queue on their scheduled time of delivery.
  The active queue is the by far most advanced part of the queuing subsystem, and contains
  many sub-states. This stems from the fact that concurrency, rate and connectinterval can be limited based on
  many dynamic parameters, essentially creating a virtually unlimited number of sub-queues.

Defer
: Messages that are not on hold, and are scheduled for delivery in the future. The default
  behavior, for every message that fails delivery (with a non-permanent error), is to be put
  in the defer queue; scheduled to be re-tried sometime in the future. It is a queue in the
  sense that messages are ordered by their scheduled delivery time.

Hold
: Messages that are on permanent hold.
  This is the most simple, and typically least used, state.
  It is not a queue; rather a collection of messages.
  It can be used to, for example, implement a spam/virus quarantine.
  Messages can be put in hold list directly during the reception of a
  message, using the end-of-DATA script [queue()](https://docs.halon.io/hsl/archive/6.12-stable/eod.html#EODMailMessage.queue) function’s “hold” option.

* [Active queue](queue_active.html.md)
  * [Active queue pickup](queue_active.html.md#active-queue-pickup)
* [Delivery settings](queue_delivery.html.md)
* [Queue policies](queue_policy.html.md)
  * [Available fields](queue_policy.html.md#available-fields)
  * [Loading policy](queue_policy.html.md#loading-policy)
  * [Policy counter thresholds](queue_policy.html.md#policy-counter-thresholds)
    * [Policy conditions](queue_policy.html.md#policy-conditions)
    * [Policy properties](queue_policy.html.md#policy-properties)
  * [Backoff policy](queue_policy.html.md#backoff-policy)
  * [Policies with *uniqueip*](queue_policy.html.md#policies-with-uniqueip)
  * [Policy precedence](queue_policy.html.md#policy-precedence)
  * [Policy counter groups](queue_policy.html.md#policy-counter-groups)
* [Cluster-aware policy](queue_cluster.html.md)
  * [Example architecture](queue_cluster.html.md#example-architecture)
  * [Delivery Orchestrator: `clusterd`](queue_cluster.html.md#delivery-orchestrator-clusterd)
  * [Writing policy for clusters](queue_cluster.html.md#writing-policy-for-clusters)
    * [Advanced usage: selective non-clustered properties](queue_cluster.html.md#advanced-usage-selective-non-clustered-properties)
  * [Observing clustered policy in operation](queue_cluster.html.md#observing-clustered-policy-in-operation)
  * [Moving messages between instances](queue_cluster.html.md#moving-messages-between-instances)
* [Message suspension](queue_suspend.html.md)
  * [Excluding IPs from pool](queue_suspend.html.md#excluding-ips-from-pool)
* [Changing message states](queue_changing.html.md)
  * [Messages on hold](queue_changing.html.md#messages-on-hold)
* [Spool startup](queue_load.html.md)
* [On-disk format](queue_load.html.md#on-disk-format)
* [Queue statistics via CLI/API](queue_cli.html.md)
