I am designing a simple Serverless email reading and processign solution (reads mails using GMAIL APIs)-> Does some processing based on email content. The slight complexity is that the GMAIL API accepts reading only 1 inbox per “Get email message” call. Im trying to solve for pulling in emails from 100+ mail inboxes (abc@gmail.com, xyz@gmail.com etc). The solution would need to design a scalable architecture that can pull emails from these 100+ inboxes at a schedule (every 5 minutes). Each pull is a GET call per inbox so we’re looking at 100+ GET calls in 5 minute interval. Ideally we want to spread these 100+ calls across the 5 minute interval so as not to flood the GMAIL APIs with 100s of call simulataneously
AWS Proposal
Given this problem, I’m considering an Event Bridge+Step Function+Lambda based solution. I thought of spinning up 100+ Parallel tasks = number of inboxes I need to read (the inboxes could be stored in a simple config or dynamodb thats not important). We need the abilty to now to make these 100+ GET calls and process them independently. What Im not able to do in this design is how to “even out” the 100+ calls over a 5 minute period. Also looking at critique and if there’s a better serverless proposal than this
Instead of a parallel state, I would use a map state. First of all because the way that you’re intending to use a parallel state isn’t really how it’s intended to be used and would be a huge pain to set up and maintain. With a map state, you can just iterate over your list of accounts and spawn a new Lambda invocation for each. You can also configure the max concurrency for this state so that you’re not making too many calls at the same time
If you want to control the rate of flow per email address use a SQS FIFO queue with the mail address as the message group ID and control the rate of execution with each Lambda function invocation.
I would go for a lambda that gets triggered by an event that fires every 5 mins. Then this lambda pushes the inboxes to be read to an SQS queue (FIFO if you want/need) and then have a lambda make the actual get call triggered by SQS.
The nice thing is you can even out the calls a bit by limiting how many lambdas can be triggered by SQS and even more so to add a delay (possibly random) to the message in SQS. Additionally you could could split the inbox list into bins and every bin gets a delay assigned.
You could just publish messages to an sqs queue with a visibility timeout, and then allow your lambdas to process those messages. Each message would contain an identifier for the inbox to monitor. Upon processing the message a new message would be queued with the 5 minute visibility timeout. This would give you 5 mins between pings on an inbox, and create some entropy between the inboxes in terms of polling. Plus if you ever have to start monitoring more, you can just queue more messages and adjust your concurrency rather than having to change things in event bridge.
Good catch. Though overall folks are suggesting to go with a simple SQS queue based solution - you’re right in the fact that Map state does allow interations fluidly which is what this solution is about
How would I publish such custom messages to SQS though? Feels like I would still need to have something like EventBridge reading from a configuration of these inboxes and publishing the messages to the SQS queue
Very interesting approach. This will likely solve for an additional requirement that we need to be able to add (or remove) inboxes periodically. It might just come down to adding/removing the rules… would you agree?
I’ve typically used this pattern when I’ve had workloads stored in dynamodb. I have a dynamo db stream which triggers the initial lambda call and starts the timed processing. Then I have a dead letter queue that collects failed workloads and a lambda to process those and update the table.
Can you not set up the mailboxes to forward incoming email?
I would set up forwarding to a dedicated domain, configure SES to handle the incoming email, and deliver any incoming emails to SNS with a lambda function.
I’d just do event bridge → setup lambda → worker lambda
Where your event bridge rule fires every 5 minutes triggering the setup lambda which reads a config file and invokes the worker lambda for each inbox (in the setup you can delay invocations with a sleep call if need be).
I’m pretty sure Lambdas have built in queues in front of them for async calls so you don’t need an explicit sqs queue for this unless you want to pair a dlq with the worker lambda.