I have a custom built inbound mail server. It will be deployed in ECS Fargate behind NLB.
Processing inbound emails is a dns lookup intensive operation.
PTR lookup: 1 query
SPF lookup: up to 10 queries + 1 main query
DKIM lookup: 1 query typically
DMARC lookup: 1 query
RBL/DNSBL checks: several queries
This easily adds up to 10 to 20 DNS queries per email, and in high volume inbound mail processing scenarios, it could hit AWS Resolver’s 1024-packet limit very quickly.
My current plan is to use unbound at instance level and ElastiCache for centralized lookup.
So my goal is to use unbound as L1 cache, ElastiCache as L2 cache, if record doesn’t found there, then hit aws dns resolver, and update both L1 and L2.
Am I doing this correctly? Or is there a better way?
I understand the SES pricing very clearly by the way. I use SES only for outbound transactional mails like verify email, password reset, billing alerts etc.
If you point Unbound at the VPC resolver (.2), you’ll still hit the 1024-pps cap. The way around that is to run Unbound (or another full resolver) in recursive mode on EC2. In that setup it doesn’t forward to .2 at all for public lookups, it walks the DNS tree itself by querying root and authoritative servers directly. That avoids the VPC throttle and scales much better. You can still configure it to use .2 only when you need to resolve private Route 53 zones, but everything else should go through normal recursion.
Using Redis or ElastiCache as an L2 DNS cache means you’d have to re-implement all the tricky parts of DNS caching yourself: respecting TTLs, handling negative responses, DNSSEC flags, and dealing with NXDOMAIN vs NODATA. Unbound already does this efficiently in memory. Adding Redis just adds another network hop and potential coherence bugs without really reducing pressure on the resolver. In practice, a properly tuned recursive resolver fleet gives you the scale you need without the overhead of maintaining a custom cache layer.
Yeah, AWS resolver has a very large shared cache so chances of hitting cache are much higher, hence performance is top notch.
Unbound has local in-memory cache, so once a domain is warmed-up you’ll also have very good performance. If your app will be receiving emails from all sorts of domains there’s a high chance you’ll have Gmail, Outlook, Yahoo, etc cached real quick. If you’re restricted to non-free email providers (let’s say corporate addresses) then I guess you’ll hit the slow path more often.
If you know which domains you’re likely going to get email from you could have a warm up process that requests Unbound to resolve all the necessary records for those domains, thus moving them up the cache in a preventive manner.
Also, I am not aware of any supported shared cache for more than one Unbound instance, so if you’re going to run more than one Unbound instance for HA purposes you may have to warm up each record twice (or as many times as resolvers you deploy).
Is there a way to use .2 by default and do full recursion via unbound only when 1024 limit reached?
Yeah, actually you could configure .2 as the first nameserver in /etc/resolv.conf then Unbound. Linux will use the first and only try the second when getting SERVFAIL and other network errors (please look this up for details! also what AWS returns when >1024pps!). Tweak timeouts if AWS just drops the packet(s) for fast failover. Next packet will again go to .2 so if you’re hitting 1024pps you may see delay in many resolutions, not just one.
If you use systemd-resolved (I don’t think you can in Fargate!) you can configure it to mark .2 as unreachable for a little while, but then again we’re not sure how often are you going to hit that, and if you’re going to end up spending a lot of time tweaking this approach.
Of course it all depends on where you want to trade-off, but I think something like having Unbound in an EC2 has little trade-offs and it’s the simplest solution probably.
I’ve been using this for a while now. It’s good stuff. I’ve setup a purely serverless (no recurring fee per account, etc.) email system for myself with SES inbound email.
Indeed. The limit stems from the limited size of IP packets (UDP MTU in particular), and is a conservative safeguard to keep it working on almost any network.