Thinning a Laravel queue: skip marketing email with a cheap filter before the slow AI step

From our own projects · Published 29 November 2026

In one of our Laravel apps, inbound mail is processed by a queued job that calls a local Ollama model. The model runs on a single CPU slot and takes one to five minutes per email. One morning the queue stood at 214 jobs and was growing by about 20 an hour. Most of the backlog was promotional mail.

Adding workers would not have helped. They would only have queued up behind the same Ollama slot. The fix was to stop sending mail to the model that did not need it.

The idea

Run a cheap check before the slow step. If the message is clearly marketing, record it as skipped and return. No model call, no wait.

In our job, the filter runs right after the message is read and before generation. A skipped message still gets a database row with status set to skipped, so nothing disappears silently and you can audit the decisions.

The filter

The class is small and has no AI call and no sender or domain blocklist. It looks at the subject, the sender, the first 8,000 characters of the body and two headers: Precedence (bulk, list or junk) and List-Unsubscribe.

public function reason(string $subject, string $from, string $body, array $headers = []): ?string
{
    // normalise to valid UTF-8 and lowercase; cap the body at 8000 characters
    // ...

    // Receipts, invoices, payment, password and security mail are never skipped.
    if (preg_match('/\b(receipt|invoice|order (?:confirmation|number|#)|payment|password|security alert|verification code|2fa)\b/u', $subject)) {
        return null;
    }

    // promotional subject + a mailing signal => skip
    // unsubscribe/bulk signal + promotional body => skip
    // otherwise null: process normally
}

It returns a reason string or null. A reason means skip. null means carry on to the model.

Three rules decide:

  1. A promotional subject (sale, discount, offer, newsletter, “limited time” and similar) plus a mailing signal: a List-Unsubscribe header, bulk precedence, or a sender like newsletter@ or promotions@.
  2. A List-Unsubscribe or bulk signal plus a promotional body (shop now, buy now, special offer).
  3. A narrow rule for one kind of ticket promotion.

Why it is deliberately conservative

An unsubscribe header does not prove that a message is marketing. Receipts, account notices and many personal replies from services carry one. So the filter needs two signals together, and the transactional subject check runs first and wins. A wrongly skipped receipt costs more than a promo email that reaches the model.

Normalise the text to valid UTF-8 before matching. Invalid bytes make preg_match with the u flag fail and return false, and that failure should never decide whether a message is skipped. We shipped the filter together with a UTF-8 scrub fix for this reason.

Result

After deployment, the queue dropped from 260 to 114 in six minutes, according to our own notes from that day. That is one measurement on one workload, not a benchmark.

Test it

The filter is a pure function of its inputs, which makes it easy to cover with a data provider. Include cases that must not be skipped: a receipt with an unsubscribe footer, a security alert from a no-reply address, a personal reply with a bulk header. Add a real promotional sample for each rule. Review the skipped rows now and then to catch false positives.

When this approach fits

Use it when the expensive step is the bottleneck and a clear share of input does not need it. If the slow step is needed for everything, you need more capacity or a smaller model, not a filter.

Our network