# Scaling with Workers - What happens if you force a cron script?

**URL:** <https://forum.mautic.org/t/scaling-with-workers-what-happens-if-you-force-a-cron-script/27854>\
**Category:** Product Support\
**Created:** [May 1, 2023, 6:07am UTC](https://forum.mautic.org/t/scaling-with-workers-what-happens-if-you-force-a-cron-script/27854 "2023-05-01T06:07:27Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![tornmarketing](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/tornmarketing/32/11603_2.png) [@tornmarketing](https://forum.mautic.org/u/tornmarketing)\
**Post date:** [May 1, 2023, 6:07am UTC](https://forum.mautic.org/t/scaling-with-workers-what-happens-if-you-force-a-cron-script/27854/1 "2023-05-01T06:07:27Z")

</div>

Working through how we config our Kubernetes at the moment

**Important question** - how can we safely split up the sending queue so we can multi thread it.  
What protective systems are in place to help prevent an email being sent twice?

Looking over our cron’s when I try to push through multiple request  
What does the bypass locking do?

```auto
Script with pid 4103645 in progress.
Script in progress. Can force execution by using --bypass-locking.

```

Any prior learnings or anything that comes to mind to help prevent us from doing anything silly would be appreciated

What is currently planned for scaling the cronjobs on mautic:

- Set the schedule to execute the cronjobs
- Let say we set to execute command mautic:emails:send every 5 minutes
- At the low email queue to be sent, say just 100 emails, it will be safe and will be finished under 5 minute, don’t need to spawn another worker
- At the high email queue to be sent, say it 10k-100k emails, it won’t be finished under 5 minute, so at the 5th minute it will spawn another worker and execute the same command to send the emails on parallel, and it will repeat at the 10th, 15th, and so on

---

<div class="post-metadata">

**Author:** ![tornmarketing](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/tornmarketing/32/11603_2.png) [@tornmarketing](https://forum.mautic.org/u/tornmarketing)\
**Post date:** [May 1, 2023, 6:49am UTC](https://forum.mautic.org/t/scaling-with-workers-what-happens-if-you-force-a-cron-script/27854/2 "2023-05-01T06:49:50Z")

</div>

Think I found our solution with lock names  
@joeyk - any other learnings here I gotta be mindful of?

> [@Mautic don\`t send emails](https://forum.mautic.org/t/mautic-don-t-send-emails/25490):
>
> Your software My Mautic version is: 4.4.2 My PHP version is: PHP FPM 8.1 My Database type and version is: 10.3.34-MariaDB-0ubuntu0.20.04.1 - Ubuntu 20.04 Your problem My problem is: My mautic has a record of 1.2 million contacts. I schedule sending to the segment that contains 400,000 people, and after sending about 250,000 emails, mautic stops sending with the following error: In ProcessEmailQueueCommand.php line 141: …

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [May 1, 2023, 7:01am UTC](https://forum.mautic.org/t/scaling-with-workers-what-happens-if-you-force-a-cron-script/27854/3 "2023-05-01T07:01:53Z")

</div>

Did you test with a real email send? For me lock names seemed to be buggy. Many emails were sent 2x

---

<div class="post-metadata">

**Author:** ![tornmarketing](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/tornmarketing/32/11603_2.png) [@tornmarketing](https://forum.mautic.org/u/tornmarketing)\
**Post date:** [May 1, 2023, 7:04am UTC](https://forum.mautic.org/t/scaling-with-workers-what-happens-if-you-force-a-cron-script/27854/4 "2023-05-01T07:04:57Z")

</div>

What quantity were you sending when it wigged out and double sent?

Really really keen to not have a repeat of my multiple sms headaches I had years ago haha

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [May 1, 2023, 7:06am UTC](https://forum.mautic.org/t/scaling-with-workers-what-happens-if-you-force-a-cron-script/27854/5 "2023-05-01T07:06:51Z")

</div>

I did a send of 10k.  
Batch size 100. 4 workers.  
When I ran the command errors popped up - workers tried to send emails that were not available. But anyway some emails were sent 2x

---

<div class="post-metadata">

**Author:** ![tornmarketing](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/tornmarketing/32/11603_2.png) [@tornmarketing](https://forum.mautic.org/u/tornmarketing)\
**Post date:** [May 1, 2023, 7:11am UTC](https://forum.mautic.org/t/scaling-with-workers-what-happens-if-you-force-a-cron-script/27854/6 "2023-05-01T07:11:09Z")

</div>

> [@joeyk](#):
>
> workers tried to send emails that were not available

Did you find the cause of them not being available?  
Did the server run out of resources or anything during the send?

My last headache was when I sent the sms, the server maxed out on resources, then it started double sending.

My solution would to cap the number of batches per worker and potentially a pause between each batch

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [May 1, 2023, 7:13am UTC](https://forum.mautic.org/t/scaling-with-workers-what-happens-if-you-force-a-cron-script/27854/7 "2023-05-01T07:13:00Z")

</div>

When sending emails the files are picked up from the spool folder. Missing files happened when 2 workers try to pick up the same file. This should not happen…

---

<div class="post-metadata">

**Author:** ![tornmarketing](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/tornmarketing/32/11603_2.png) [@tornmarketing](https://forum.mautic.org/u/tornmarketing)\
**Post date:** [May 1, 2023, 7:26am UTC](https://forum.mautic.org/t/scaling-with-workers-what-happens-if-you-force-a-cron-script/27854/8 "2023-05-01T07:26:53Z")

</div>

How do the workers work together and know which message to pick up?

is there anyway to put a prefix on the messages and allocate it to a worker?

Havent fully read what these name locks do either - is this the prefix?

Or we spin up a worker per batch once a worker has been a assigned a batch then kill the worker on completion of that batch … (donno how much the devops would like that haha)

This should limit the cron per worker  
mautic:broadcasts:send --min-contact-id=0 --max-contact-id=2500  
mautic:broadcasts:send --min-contact-id=2501 --max-contact-id=5000  
… etc

---

<div class="post-metadata">

**Author:** ![tornmarketing](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/tornmarketing/32/11603_2.png) [@tornmarketing](https://forum.mautic.org/u/tornmarketing)\
**Post date:** [May 1, 2023, 7:47am UTC](https://forum.mautic.org/t/scaling-with-workers-what-happens-if-you-force-a-cron-script/27854/9 "2023-05-01T07:47:26Z")

</div>

know of any cheaper alternatives to mailtrap?

> **[Email Testing Tool #1 - Mailtrap](https://mailtrap.io/email-sandbox/)**
>
> Inspect and debug your email samples before delivering them to your customers.

---

<div class="post-metadata">

**Author:** ![Yosu\_Cadilla](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/yosu_cadilla/32/6206_2.png) [@Yosu\_Cadilla](https://forum.mautic.org/u/Yosu_Cadilla)\
**Post date:** [May 1, 2023, 10:27am UTC](https://forum.mautic.org/t/scaling-with-workers-what-happens-if-you-force-a-cron-script/27854/10 "2023-05-01T10:27:02Z")

</div>

> [@tornmarketing](#):
>
> How do the workers work together and know which message to pick up?

They do not work together in the sense they are not aware of each other.  
Thy do work well in parallel and I am using the exact same method you just described.

Beware that as you add new contacts, you might need to adjust your worker’s settings.

I am running one specific instance on Mautic 4.4.6 with 7 email workers, sent millions of emails using this method, it works well with large amounts of emails, no problem there, but it consumes a lot of CPU and RAM.

Is Mailtrap cheaper than SES?

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [May 1, 2023, 10:57am UTC](https://forum.mautic.org/t/scaling-with-workers-what-happens-if-you-force-a-cron-script/27854/11 "2023-05-01T10:57:21Z")

</div>

Broadcast send is fixed already - i tested a PR 1 year ago. You can do multi thread on it

---

<div class="post-metadata">

**Author:** ![tornmarketing](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/tornmarketing/32/11603_2.png) [@tornmarketing](https://forum.mautic.org/u/tornmarketing)\
**Post date:** [May 1, 2023, 10:59am UTC](https://forum.mautic.org/t/scaling-with-workers-what-happens-if-you-force-a-cron-script/27854/12 "2023-05-01T10:59:35Z")

</div>

Hey mate

Testing was more to validate duplicates and ingess with stats n validation

Looking at a separate script to count contacts in db and contact id range on campaigns.

What specifically consumes the cpu/memory?

The email create or broadcast/send?  
Stats ill run through a horizontal rabbit

---

<div class="post-metadata">

**Author:** ![Yosu\_Cadilla](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/yosu_cadilla/32/6206_2.png) [@Yosu\_Cadilla](https://forum.mautic.org/u/Yosu_Cadilla)\
**Post date:** [May 1, 2023, 2:05pm UTC](https://forum.mautic.org/t/scaling-with-workers-what-happens-if-you-force-a-cron-script/27854/13 "2023-05-01T14:05:50Z")

</div>

> [@tornmarketing](#):
>
> What specifically consumes the cpu/memory?

It’s both, depending on the email size and complexity it will be the email creation or the sending.  
The increase in RAM demand is not negligible but not too much.  
The CPU requirements are relatively big.

If your emails are very heavy and take a while to get generated, you might want to use the queue and build them, let’s say overnight, send them in the morning. I’ve tried this and it worked wonderfully and was fast, however, there is a problem, if you have any emails that need sending immediately (like a password reminder or a welcome email), they might get stuck until you open the gates.

So right now I have the queue disabled and I regulate the sending speed (so I don’t get over the SES limit) with how many cronjobs I run in parallel.  
It’s all **“intumath” with very generous “padding”.**  
I can’t give you the numbers cause those depend on both your SES limits and the hardware you’re using.

> Looking at a separate script to count contacts in db and contact id range on campaigns.

That sounds very interesting!
