postal icon indicating copy to clipboard operation
postal copied to clipboard

Postal tuning for sending out a massive email campaign (> 1 million)

Open vishnus opened this issue 8 years ago • 21 comments

Hi there,

We've been using Postal in production for last one year and we couldnt be happier with the performance. We recently built a list campaign manager inhouse in Golang, it can push over 10k emails in matter of seconds to the SMTP. But postal seems to throw timeout / connection pool error (ActiveRecord::ConnectionTimeoutError: could not obtain a connection from the pool within 5.000 seconds) maybe 2-3 times in a second. We think our config may not be right. We have 36 core, 68GB server where postal is running. Load is always less than 3. Currently the config looks like this:

workers: quantity: 60 threads: 80 main_db: host: 127.0.0.1 username: postal database: postal pool_size: 150

Mysql DB has max_connections set to 5000. Is this configuration ok? How can we use the system specs to the fullest? More than 50 workers its not spawning when we gave. The thread count and pool_size what is the relation we couldnt really understand. Can someone shed some light on this?

vishnus avatar Nov 06 '18 17:11 vishnus

It might be that your max_connections is being reached, based on https://github.com/atech/postal/issues/324#issuecomment-323538575 and your email volume, its likely that number needs to be much higher.

You should be able to use some database tools to monitor the number of active connections and see how quickly you use up the connections.

willpower232 avatar Nov 08 '18 09:11 willpower232

60 workers with a pool_size of 150 connections = 9000 connections . That is the right interpretation of this scenario?

rafael-leo-almeida avatar Nov 08 '18 17:11 rafael-leo-almeida

@rafaelsisweb same question even I have.. what would be the right max_connection required in Mysql config?

vishnus avatar Nov 09 '18 05:11 vishnus

I would suggest 9000 at minimum as calculated, maybe 10000 to give you more room.

You'd have to continue monitoring to see if this resolved your problem and increase further if necessary. If it doesn't have any effect or reduction in your issues at all, we'll have to consider something else.

willpower232 avatar Nov 09 '18 09:11 willpower232

Thanks @willpower232 .

Worker count is 50 not 60. When I give 60 it runs only 50. I've tweaked some config for 50 workers and its currently:

System: 36core, 68gb RAM

Mysql:

innodb_buffer_pool_size	= 2048M
innodb_flush_log_at_trx_commit = 2
max_connections		= 8000
wait_timeout		= 31536000
max_allowed_packet	= 256M

Postal:

workers:
  quantity: 50
  threads: 80

main_db:
  pool_size: 150

message_db:
  pool_size: 50

We are sending a bulk campaign only next week now, so can tell then if this works. Normal emails are going fine, ~100k a day happens seamlessly. We will know when we actually send out campaign email in bulk if this works. Any other thoughts on scaling, please feel free to post it here.. thanks

vishnus avatar Nov 09 '18 11:11 vishnus

Interesting, it might be that your next step is an additional server and to make a cluster out of your setup if you're starting to max out resources.

willpower232 avatar Nov 12 '18 09:11 willpower232

I hope to see the resources maxed out :)

On a different topic, if we set up another SMTP server, (not postal, just a simple golang email relay server), would it be possible to use that server also in parallel with postal? Means sharing the queue and everything with postal, but mail sending relay server will be postals inbuilt and another relay server..

vishnus avatar Nov 12 '18 10:11 vishnus

So went sent another set of emails. Upto 300k it was sent in an hour. Exactly after 58minutes, sending stopped completely. But emails were queuing up. Then in worker.log we could see this error:

ActiveRecord::StatementInvalid: Mysql2::Error: Lock wait timeout exceeded; try restarting transaction: UPDATE queued_messagesSETqueued_messages.locked_by= NULL,queued_messages.locked_at= NULL WHEREqueued_messages.id= xxxxx

Any thoughts? Theres over 20k emails in queue which needs to be sent before resuming the rest of the emails..

vishnus avatar Nov 15 '18 15:11 vishnus

Ok I manually cleared the locks following #645

Any reason why all of a sudden this would have happened? Load and RAM was fine. I'll rerun now and will increase mysql connections a bit more and see..

vishnus avatar Nov 15 '18 16:11 vishnus

That error message implies that you are now so aggressively using the database that all the updates involved can't complete properly. You can increase the wait timeout as suggested here: https://dba.stackexchange.com/questions/100984/mysql-lock-wait-timeout-exceeded-try-restarting-transaction

As time goes on and your mail volume grows, you may need to consider dividing your workload between multiple servers so you don't completely swamp out your server and it becomes unusable.

willpower232 avatar Nov 15 '18 19:11 willpower232

@vishnus is your setup running on a single server? I've replicate a similar setup by using a 3 mysql nodes (galera cluster) + a dedicate Rabbit server + 2 postal (32gb each + 8 cpu).. but it can't reach over 400 emails / min. This is using SMTP on each server with a load balancing between app and postals .

rbustos avatar Nov 25 '18 19:11 rbustos

@rbustos yes, we're running in one server. 400/min is very less. Just with 4 core, 16gb, we were able to send at 1500 emails/min. Is your email triggering application slow?

vishnus avatar Nov 26 '18 08:11 vishnus

Actually I sent all emails previously to a postfix queue and then it talk directly to postal (smtp.1). So with this I tried to avoid any bottleneck between them.

rbustos avatar Nov 26 '18 16:11 rbustos

@willpower232 I think we've cracked the specs and config! We sent out over 1.1 million emails in less than 3 hours (all delivered except for 2-3k bounced). We got a speed of over 6400 emails per minute, ie around 105 emails per second. We had zero timeouts, not even single email was lost in transit. We used the same hardware and the following config:

Mysql:

innodb_buffer_pool_size	= 3048M
max_connections		= 9000
connect_timeout		= 6
wait_timeout		= 31536000
max_allowed_packet	= 256M

Postal config:

workers:
  quantity: 50
  threads: 60

main_db:
  host: 127.0.0.1
  username: xxxx
  password: xxxxxx
  database: postal
  pool_size: 150

Hope this thread helps someone who is looking to scale their postal set up :)

Apart from these minor tweaks, the only difference from last times was that we did not constantly refresh the postal web. Probably that might have reduced some select queries unnecessarily hitting the db?

vishnus avatar Nov 27 '18 19:11 vishnus

Good to know some numbers.

Not refreshing the UI might have helped but you'd hope that the UI wouldn't block the workers from working. It might be easier to monitor the server from the cli, watching htop or any server manager so you can see when its quietened down again.

willpower232 avatar Nov 28 '18 14:11 willpower232

Those settings are applied at /opt/postal/config/postal.yml ?

wh4u avatar Dec 08 '18 10:12 wh4u

@willpower232 yes, i did check HTOP, system usage was still low, with load under 10 throughout.

@wh4u yes, postal configs goes here and mysql goes to /etc/mysql/my.cnf

vishnus avatar Dec 09 '18 07:12 vishnus

In case anyone interested, we've open-sourced the mailing list manager used for sending over a million emails in a couple of hours: https://listmonk.app - we use Postal SMTP.

https://news.ycombinator.com/item?id=20421243

vishnus avatar Jul 26 '19 09:07 vishnus

Hi @vishnus ,

Can you share details of your postal setup? Or even improve project doc with tips to handle this workload?

rafael-leo-almeida avatar Jul 26 '19 12:07 rafael-leo-almeida

@willpower232 I think we've cracked the specs and config! We sent out over 1.1 million emails in less than 3 hours (all delivered except for 2-3k bounced). We got a speed of over 6400 emails per minute, ie around 105 emails per second. We had zero timeouts, not even single email was lost in transit. We used the same hardware and the following config:

Mysql:

innodb_buffer_pool_size	= 3048M
max_connections		= 9000
connect_timeout		= 6
wait_timeout		= 31536000
max_allowed_packet	= 256M

Postal config:

workers:
  quantity: 50
  threads: 60

main_db:
  host: 127.0.0.1
  username: xxxx
  password: xxxxxx
  database: postal
  pool_size: 150

Hope this thread helps someone who is looking to scale their postal set up :)

Apart from these minor tweaks, the only difference from last times was that we did not constantly refresh the postal web. Probably that might have reduced some select queries unnecessarily hitting the db?

@rafaelsisweb you can check this config

vishnus avatar Jul 26 '19 15:07 vishnus