How to Make a Robust System to Handle Bulk Requests
Let’s say you need to send order placed and payment confirmation receipts to millions of users. Challenging, right? You can't just run a loop on the server millions of times — that’s not scalable.
So, we think smart. Let’s create a bulk email worker/server that receives the order and payment confirmation messages, calls the Gmail API, and then sends notifications to users.
Cool? Perfect! That’s how it works…
But here's the challenge:
This is a synchronous process, meaning it depends on the Gmail API. If the Gmail API is down or slow, the worker will wait for the acknowledgement and won’t be able to process other payment events. That’s not scalable.
Enter AWS SQS — A Robust Solution
To solve this, we can use Amazon SQS (Simple Queue Service), which is very robust and decouples the system.
Here’s how it works:
- We create a queue of orders and payment confirmations.
- Our email worker servers will continuously poll the queue and ask: “Hey, do you have something to send?”
- If there are millions of messages, we can scale the email workers horizontally, i.e., spin up more workers to process them in parallel.
But Wait… Another Challenge
Even if you scale your email servers, you’re still dependent on Gmail, which may have rate limits — for example, only 5 emails per second per user/account.
To handle this, you can:
- Implement rate limiting logic in your workers.
- Use exponential backoff and retry mechanisms.
- Queue unsent emails in a Dead-Letter Queue (DLQ) for retry later.
Multi-Channel Notification? Use AWS SNS
In today’s world, you don’t just send emails. You also send WhatsApp messages, SMS, and push notifications. So how do you scale that?
Simple: Use AWS SNS (Simple Notification Service) — a broadcasting service.
Here’s how:
- You announce an event (like
"payment_successful"
) using SNS. - Multiple subscribers (email, SMS, WhatsApp, etc.) are attached to this SNS topic.
- SNS fan-outs the message to different channels, each connected to its own queue or service.
So if you have:
- An SQS queue for email
- A webhook for WhatsApp
- An SMS gateway for text messages
Each of them gets the notification independently.
What If Delivery Fails?
Good question.
SNS is fire-and-forget by default — no delivery confirmation.
But you can attach dead-letter queues (DLQs) or monitor failures.
So if a mail fails to send, it goes to a discarded queue, and you can retry later.
That’s why sometimes you see receipts 2–3 minutes after making a payment.
Summary
- Don’t run loops for millions of tasks — offload to queues.
- Use SQS for message handling and decoupling.
- Use SNS for multi-channel announcements.
- Add DLQs and retry logic to handle external API failures.
- Design the system to be asynchronous, scalable, and resilient.
✍️ Author: Omkar Sharma
📬 Feel free to connect on LinkedIn or explore more on GitHub
Top comments (0)