How to send email from Dream

I’ve written a short blog post about what I learned building simple email features for a web server written in the Dream framework. The accompanying source code is available here:

I’m interested in adding more examples and tutorials to the OCaml ecosystem and would be happy to get your feedback, positive or negative, on this write-up (here or via email/github/discord).

14 Likes

Thank you for the blog post and the example code—these kind of examples are very valuable!

In my tests, a successful API request typically took between 0.5 and 1 seconds. For comparison, the webserver can serve the email form in less than 200 microseconds, so the performance benefits to shifting email tasks to a background queue are nontrivial.

The way I read the example code, since the API request (to Mailgun) is asynchronous, that means it is not blocking the webserver from serving requests. But the other example you mentioned—rendering an image (using server’s CPU)—would be indeed blocking and have to be offloaded to a worker.

1 Like

Thanks for your feedback, I should clarify that in the post. In past products I’ve worked on, when we created custom emails with account-specific images, the background worker process was responsible for both generating the image and then sending the email. So you’re right, none of that workload should fall on the webserver.

1 Like

Hey, interesting post. I had a couple of code suggestions. May I send a PR with my changes to start a discussion? (Not necessarily for the purpose of merging.)

I decided to use RabbitMQ because I didn’t want to tie my implementation to one cloud provider and RabbitMQ is supported by amqp-client on OPAM

What do you think of using something simpler, like a table in a SQLite file?

1 Like

The common consensus for queues are:

  • RabbitMQ
  • Redis
  • Amazon SQS
  • … orthers

While SQL is possible, it’s often discouraged. As an article I would say keep the users on a best practice stand point.

You can read here a HN thread about SQL and queues here Do you really need Redis? How to get away with just PostgreSQL | Hacker News

3 Likes

Sure, please do open a pull request.

This is an excellent blog post, and I learned a lot from it.

Apart from benefitting as a reader, this also just considerably sped up my side of helping to design email support for Dream. Thanks for all the links inside the post, and alternatives mentioned!

4 Likes

Just to notice that I just made a release of colombe.0.4.2 which solves the issue discovered by @jsthomas.

4 Likes