Help: Sending emails to multiple users

Hey! I'm working on a project using Cloudflare Workers to automatically send monthly account statements to our users, keeping them updated on their activities within our platform. However, I've hit a bit of a roadblock, and I could really use some help Objective: To automatically send an account statement to each user every month, summarizing their activities on our platform. Requirements: The function should run automatically on a monthly basis without any manual intervention (im using cron triggers). It should be able to access user data securely to generate personalized account statements for each user( im using R2). The Problem I'm Facing: During the implementation, I'm encountering an issue where the Cloudflare Workers server runs out of memory resources before the task can be completed. What i mean by this is that the worker runs out of memory before all the mails are sent What I've Tried So Far: I've optimized the code to make it as efficient as possible. I've reviewed my data processing to minimize any unnecessary memory consumption. Although i think the problem might be because the numbers of users and i need to run a couple of calculations for each of them
3 Replies
Tin Cap
Tin Cap11mo ago
Sounds like you're trying to generate and send a lot of emails in a single worker invocation. You could split your system into two workers: Worker #1: Quickly determines who to email (e.g. getting all account ids). And publishes a message to a message queue for each account id. Worker #2: A queue consumer that pulls messages off that queue and generates and sends a single email.
Pato
Pato11mo ago
that sounds like something that might work
Tin Cap
Tin Cap11mo ago
It's good practice to do something like this. Otherwise, you'll just hit memory/runtime issues again in the future as you scale up.