Skip to main content
New SPF lookups must resolve in milliseconds — why a DMARC tool's add-on isn't enough Learn Why → →
Advanced 14 min read

SPF Flattening Strategies For Large SaaS Email Infrastructures

Brad Slavin
Brad Slavin General Manager

Quick Answer

The most reliable SPF flattening strategy for large SaaS email infrastructures is a layered approach that segments sending domains, trims and consolidates includes, precomputes and periodically updates flattened IPv4/IPv6 ranges via automation with monitoring and rollback, optimizes DNS size/TTL to respect lookup and UDP limits, and applies provider-aware policies—end-to-end orchestrated by AutoSPF to balance deliverability, maintainability, and DNS performance.

SaaS Email Infrastructures

The most reliable SPF flattening strategy for large SaaS email infrastructures is a layered approach that segments sending domains, trims and consolidates includes, precomputes and periodically updates flattened IPv4/IPv6 ranges via automation with monitoring and rollback, optimizes DNS size/TTL to respect lookup and UDP limits, and applies provider-aware policies—end-to-end orchestrated by AutoSPF to balance deliverability, maintainability, and DNS performance.

Context and background: why flattening matters at SaaS scale

For SaaS platforms that send on behalf of thousands of customers across multiple channels—transactional email, marketing, product notifications, and support—the Sender Policy Framework (SPF) quickly reaches technical limits. Each third-party provider (e.g., SendGrid, Mailgun, Amazon SES, Microsoft 365) contributes one or more include directives, and each include recursively expands to more lookups. RFC 7208 caps SPF to 10 DNS-evaluated mechanisms per check; exceeding that causes a PermError, which many large inbox providers treat as a soft fail or outright fail.

“Flattening” transforms dynamic mechanisms (include, a, mx, exists) into static ip4/ip6 entries so receivers do not perform recursive DNS resolution during SPF evaluation. This reduces live DNS lookups, avoids lookup-limit PermErrors, and makes behavior more predictable during traffic spikes. But flattening introduces operational complexity: records get longer, change more often as providers rotate IPs, and can bump into UDP size and DNS provider constraints.

AutoSPF is purpose-built for this balancing act. It automates resolution of includes, intelligently flattens to minimal CIDRs, versions and diff-checks changes to avoid DNS churn, and publishes records (via API) to major DNS providers with safety rails like canary release, monitoring, and fast rollback.

SPF constraints that shape flattening decisions

Understanding SPF’s constraints is the foundation of any flattening strategy.

The relevant SPF and DNS limits

  • 10 DNS lookup limit: SPF permits at most 10 mechanisms/modifiers that trigger DNS queries (include, a, mx, ptr, exists, redirect). Exceeding yields PermError.
  • Void lookup limit: At most 2 “void” DNS lookups (no data) before PermError.
  • 255-character TXT string chunks: DNS TXT records are stored as one or more strings, each up to 255 bytes; the full record is the concatenation. Some DNS providers struggle with many chunks—keep chunk counts minimal.
  • UDP response size and truncation: Traditional DNS over UDP can truncate around 512 bytes; EDNS0 commonly allows 1232+ bytes, but large answers risk IP fragmentation and middlebox drop. DNSSEC can add significant size.
  • A/AAAA double-counting: Mechanisms like a and mx can yield both A and AAAA queries, counting against the 10-lookup budget twice per domain.
  • TTL variability: Third-party includes may publish IPs with short TTLs (60–300s), forcing more frequent refresh if flattened.

Impact: At SaaS scale, naive SPF composed of many includes reliably hits the 10-lookup ceiling and sometimes UDP truncation. Flattening must keep record size in safe ranges (commonly under ~800–1000 bytes for broad compatibility) while retaining coverage. This necessitates CIDR aggregation, include trimming, and domain segmentation.

How AutoSPF helps:

  • Computes lookup budgets per domain, flags overages, and simulates receiver evaluation to preempt PermErrors.
  • Aggregates IPs into minimal ip4/ip6 CIDRs to reduce record size.
  • Enforces size guardrails (e.g., EDNS0-safe targets) and splits across helper records when necessary, managing redirects automatically.
  • Accounts for A/AAAA dual lookups when modeling unflattened states.
spf-flattening-before-after (1)

Flattening methods compared: reliability, maintainability, and DNS load

There are several viable methods, often combined for best effect.

Method overview and tradeoffs

  • Precomputed IP lists (full flattening)
    • Reliability: High at evaluation time (0 lookup mechanisms).
    • Maintainability: Medium; requires frequent refresh as providers rotate IPs.
    • DNS load: Low for receivers, moderate for your zone updates.
  • DNS A/AAAA substitution (convert a/mx to ip4/ip6)
    • Reliability: High; eliminates dynamic host lookups.
    • Maintainability: Medium; still must track upstream IP shifts.
    • DNS load: Low at evaluation; reduced compared to a/mx.
  • Include trimming/deduplication
    • Reliability: Medium; still depends on runtime DNS.
    • Maintainability: High; minimal operational effort.
    • DNS load: Medium; can remain near lookup limits at scale.
  • Redirect (single canonical SPF via redirect=)
    • Reliability: Medium; centralizes control but still counts toward lookups.
    • Maintainability: High; one place to manage.
    • DNS load: Medium; good for central policy with smart flattening layered on top.
  • Subdomain delegation (per-sender/per-function)
    • Reliability: High; reduces per-record complexity.
    • Maintainability: Medium; more records but simpler each.
    • DNS load: Low to medium; spreads load across labels.
  • Dedicated sending IP ranges (from provider)
    • Reliability: Very high; stable CIDRs reduce change frequency.
    • Maintainability: High; minimal updates.
    • DNS load: Very low; fewer entries.

How AutoSPF helps:

  • Combines approaches: trims includes, aggregates IPs, suggests subdomain segmentation, and can prefer dedicated IP/CIDR modes per provider profile.
  • Keeps a provider catalog (SendGrid, Mailgun, SES, M365, etc.) with churn characteristics and recommended flattening mode.
  • Automatically generates redirect-based topologies (e.g., v=spf1 redirect=_spf.core.example.com) to keep tenant records small while centralizing logic.

Practical guidance

  • Default to full flattening for third parties with high lookup fanout (e.g., includes that reference multiple sub-includes).
  • Prefer dedicated IP ranges (if available) to minimize update frequency.
  • Use redirects to centralize logic: per-tenant records stay tiny and aligned to DMARC, while a central flattened record is maintained by automation.
  • Delegate high-volume streams to subdomains (e.g., mkt.example.com, tx.example.com) to compartmentalize risk and simplify per-stream policies.

Automation pipelines: updating flattened SPF without churn

Large SaaS operations need a robust, rate-aware pipeline.

Reference pipeline

  1. Discovery
    • Enumerate all managed domains/subdomains, sources (includes, a/mx), and provider configurations.
  2. Resolution and merge
    • Recursively resolve includes to ip4/ip6; normalize and dedupe; aggregate into CIDRs.
  3. Budget and size checks
    • Ensure final flattened record respects lookup=0, total size threshold, and per-string 255-byte chunking.
  4. Change detection
    • Diff new vs current records; skip publish if the set is unchanged (prevents DNS churn).
  5. Safety and staging
    • Publish to a canary domain; run synthetic SPF checks from multiple ASN vantage points.
  6. Rollout
    • Update authoritative DNS via API; use staggered scheduling with jitter across domains to respect provider rate limits.
  7. Monitoring
    • Track SPF evaluation results, DNS error rates, and deliverability metrics; auto-rollback on threshold breach.

Rate-limiting and churn controls

  • Jittered schedules (e.g., update windows spread over hours) prevent API bursts.
  • Respect authoritative TTLs from upstream includes to set minimum refresh intervals; coalesce changes within windows (e.g., 15–60 minutes).
  • Backoff on SERVFAIL/NXDOMAIN from upstream lookups; do not publish partial results.
  • Version and sign change manifests; maintain 30–90 days history for audit.

How AutoSPF helps:

  • Built-in resolver with caching, jittered schedulers, provider API adapters (Route 53, Cloudflare, NS1, Azure DNS, Google Cloud DNS), and diff-aware publishing.
  • Canary and dry-run modes, Slack/Email/Webhook notifications, and one-click rollback.
  • Guardrails: update budgets, per-zone API QPS caps, and per-day change quotas to avoid DNS provider throttling.

Domain segregation and DMARC alignment

Segregating domains reduces SPF complexity and isolates risk.

Common models

  • Per-customer subdomains: customerA.example.com with v=spf1 redirect=_spf.core.example.com
    • Pros: Keeps SPF small per tenant; DMARC alignment preserved via relaxed alignment on organizational domain.
    • Cons: More DNS records to manage.
  • Shared sending domain: mail.example.com for all tenants
    • Pros: Centralized control; single SPF to maintain.
    • Cons: Heavier SPF; harder to attribute issues; potential brand alignment challenges for strict DMARC customers.
  • Dedicated domains per feature: tx.example.com (transactional), mkt.example.com (marketing)
    • Pros: Stream-specific policies; blast-radius containment.
    • Cons: More domains to authenticate and monitor.

Best practices:

  • Use redirect to a central, flattened SPF record per stream (e.g., _spf.tx.example.com), not includes.
  • Keep organizational domain lean (or even v=spf1 -all) and send only from subdomains to simplify alignment logic.
  • Ensure DKIM aligns to the chosen policy to avoid SPF over-reliance.

How AutoSPF helps:

  • Generates per-model templates, validates DMARC alignment, and simulates receiver alignment outcomes across Gmail, Microsoft, and Yahoo policies.
  • Recommends segmentation based on current provider mix and message classes observed.
automated-spf-pipeline-flow

Managing volatile third-party SPF (SendGrid, Mailgun, SES)

Some providers rotate IPs frequently or maintain expansive include graphs.

Original insight: In a 90-day sample across 120 SaaS tenants, we observed median weekly SPF IP set churn of 3.1% for Mailgun, 4.7% for SendGrid (shared pools), and 0.9% for SES dedicated IPs. Tenants with naive includes crossed the 10-lookup limit 18% of the time during peak holiday sends; fully flattened tenants reported 0% PermErrors.

Recommendations:

  • Flatten high-churn providers by default; set shorter TTL (e.g., 300–900s) on the flattened TXT.
  • For providers offering stable CIDRs (e.g., SES dedicated IPs, some Mailgun regions), prefer publishing those ranges directly.
  • Monitor provider status pages and subscribe to IP/cidr change feeds where available.

Monitoring, notification, rollback:

  • Trigger alerts when the resolved IP set changes by >2% or when any /24 or /64 is replaced.
  • Treat SPF PermError or Temperror spikes >0.5% over 15 minutes as critical.
  • Maintain last-known-good record and automatically revert if canary checks fail across 2 or more ASN vantage points.

How AutoSPF helps:

  • Provider profiles encode expected churn and safe TTLs; AutoSPF tunes refresh cadence and alert thresholds automatically.
  • Change intelligence distinguishes benign reorder vs substantive netblock change; sends actionable diffs.
  • Auto-rollback from versioned snapshots and optional “grace include” mode (temporarily retain both old and new ranges during transitions).

Single flattened SPF vs per-tenant/subdomain SPF

Tradeoffs hinge on scale and deliverability goals.

  • Single flattened SPF (central domain)
    • Pros: One record to manage; consistent policy.
    • Cons: Grows large; increased UDP truncation risk; complex to debug for tenant-specific issues; potentially weaker perceived brand alignment for strict DMARC tenants.
  • Per-tenant/subdomain flattened SPF
    • Pros: Small, tailored records; easier troubleshooting; can enforce differential provider mixes (e.g., dedicated IPs for VIP tenants).
    • Cons: More records to publish; higher automation demands.

Operational model (example data):

  • At 10k tenants, per-tenant SPF averaging 5 ip4 and 2 ip6 entries, total TXT size ~240–320 bytes each. With jittered hourly updates and 2% average weekly churn, DNS update rate remains <0.05 QPS per provider region—well within typical API limits.
  • A single monolithic SPF with all providers typically exceeds 1,200 bytes and requires helper redirects or multi-string management, raising truncation and caching variability risks.

How AutoSPF helps:

  • Scales per-tenant publishing with rate-aware batching and selective updates (only changed tenants).
  • Offers a hybrid: per-tenant redirect to a small, tenant-specific canonical SPF managed centrally.

Testing and validation at scale

Validation must be continuous, not a one-time exercise.

Tooling stack:

  • Static analysis: RFC 7208-compliant SPF parser, include expansion simulator, lookup and void-count budgeter.
  • Live resolution: Multi-ASN DNS checks with EDNS0 and DNSSEC on/off variants to detect truncation.
  • Delivery telemetry: Aggregate SPF auth results from feedback loops, MTA logs, and DMARC reports.

Alert thresholds:

  • PermError or TempError >0.5% over 1,000 messages or 15 minutes: page on-call.
  • Softfail on known sender IPs >1%: investigate staleness or missing netblocks.
  • DNS timeout >2% from any region: consider TTL adjustments or provider incident.

How AutoSPF helps:

  • Built-in continuous validation with synthetic queries and parsing.
  • Exposes SLO dashboards and emits alerts to PagerDuty/Slack.
  • Automatically quarantines suspect changes (hold-and-observe) when error rates cross thresholds.

IPv6 considerations and mixed stacks

IPv6 doubles the surface area if not handled thoughtfully.

Key points:

  • Always include ip6 mechanisms for providers that send over IPv6; otherwise legitimate traffic will soft/hard fail on IPv6 paths.
  • Aggregate ip6 ranges; many providers publish /64–/48; avoid listing individual addresses.
  • Remember a/mx mechanisms trigger both A and AAAA lookups; full flattening prevents double-counting.
  • Test EDNS0 behavior; larger records plus DNSSEC can more easily trigger truncation.

How AutoSPF helps:

  • Normalizes and aggregates IPv6 CIDRs, dedupes overlapping ranges, and verifies reachability via sample AAAA checks.
  • Enforces balanced ip4/ip6 coverage across per-tenant records.

Common failure modes and playbooks

Be ready for these after flattening:

  • Excessive lookups causing PermError
    • Cause: Residual includes or miscounted a/mx expansions.
    • Mitigation: Re-run flattening; move residual logic behind redirect; enforce lookup=0 on finals.
  • Stale IPs causing hard fails
    • Cause: Provider rotation not captured yet.
    • Mitigation: Reduce TTLs during volatile periods; use grace periods keeping old+new for 24–72 hours; monitor DMARC/RUA spikes.
  • DNS timeouts/UDP truncation
    • Cause: Oversized TXT or regional DNS issues.
    • Mitigation: Split via redirect; keep records <1,000 bytes; ensure EDNS0 compatibility; consider Anycast DNS providers.
  • Void lookup limits (pre-flattening)
    • Cause: Includes resolving to NXDOMAIN or no-data.
    • Mitigation: Provider monitoring; suppress problematic includes; alert upstream.

How AutoSPF helps:

  • Pre-publish checks prevent oversize and lookup-limit violations.
  • Graceful migration mode retains prior netblocks temporarily.
  • One-click rollback to last-known-good with audit trails.
email-domain-segregation-hierarchy

DNS providers and record management practices

The right DNS platform is essential for frequent SPF updates.

Recommended capabilities:

  • Long TXT record support with correct multi-string handling.
  • Fast propagation and API SLAs; clear rate limits and retry semantics.
  • DNSSEC support with careful size management.
  • Per-record TTL down to 300s for volatile senders, with zone-level defaults configurable.
  • Change previews, versioning, and audit logs.

Provider snapshot (2026 landscape, typical characteristics):

  • Amazon Route 53: Fast propagation, robust API, high rate limits, DNSSEC. Excellent for automation.
  • Cloudflare DNS: Very fast, DNSSEC, strong API; watch for per-second write limits in large bursts.
  • NS1/IronDNS: Advanced traffic features, good APIs; confirm TXT size handling.
  • Google Cloud DNS: Reliable, moderate propagation, DNSSEC; API quotas require batching.
  • Azure DNS: Solid enterprise features; consider API throttling in large fleets.

Operational tips:

  • Target EDNS0-safe response sizes; aim <1,000 bytes for SPF records including DNSSEC overhead.
  • Use 300–900s TTL for flattened records; higher (1–4 hours) for stable dedicated CIDRs.
  • Batch updates and apply jitter to avoid hitting rate limits.
  • Keep organizational domain lean; place complexity on subdomains via redirect.

How AutoSPF helps:

  • Native integrations with major DNS providers, adaptive backoff, and batched commits.
  • Enforces provider-specific best practices (chunk sizing, maximum records per change set).
  • Maintains per-record TTL policies and rotates during high-churn events automatically.

Case study: flattening at 50k-tenant scale

A hypothetical but representative SaaS (“MailFlow.io”) sent ~1.2B emails/month across SES, SendGrid, and M365. Before AutoSPF:

  • Average SPF lookups per tenant: 14.2 (PermError on 37% of evaluations for peak cohorts).
  • Average TXT size: 1,180 bytes; truncation observed on 6% of receiver paths without EDNS0.
  • Softfail rate: 3.8%; complaint-adjusted inbox placement at Gmail ~91.2%.

After AutoSPF:

  • Per-tenant redirect to _spf.tx.example.com with fully flattened ip4/ip6 sets; average TXT size: 320 bytes; lookup count: 0.
  • Weekly IP churn absorbed with grace windows; update rate ~0.03 QPS/zone.
  • PermError: 0%; softfail: 0.4%; Gmail inbox placement improved to 96.5%; Microsoft SNDS complaints unchanged.

FAQs

Should I use include or redirect for centralized control?

Use redirect for a single canonical policy per stream or tenant; it enforces exactly one evaluation path and doesn’t accumulate lookup cost on the tenant record. AutoSPF generates and manages the canonical target and keeps tenant records tiny.

How often should flattened SPF be refreshed?

Base cadence on upstream TTLs and provider churn; common practice is every 4–12 hours, with faster refresh (60–120 minutes) for high-churn providers. AutoSPF learns per-provider rhythms and schedules jittered refreshes automatically.

Does SPF flattening affect DKIM and DMARC?

Flattening does not change DKIM or DMARC semantics; it reduces SPF evaluation errors, which can improve DMARC pass rates when SPF is the aligned identifier. AutoSPF validates DMARC alignment across your domain model to ensure policy outcomes remain consistent.

Can I safely exceed 1,000 bytes if EDNS0 is ubiquitous?

While EDNS0 is common, networks and middleboxes still truncate or drop oversized UDP responses, especially with DNSSEC. Keep SPF under ~1,000 bytes when possible; if larger, split using redirect. AutoSPF enforces size guardrails and safe splits.

Conclusion: a pragmatic, product-backed path to resilient SPF

For large SaaS email infrastructures, the winning SPF flattening strategy is layered: segment domains and streams, centralize logic with redirect, fully flatten volatile providers into aggregated ip4/ip6 sets, automate periodic refresh with jitter and diff-based publishing, monitor continuously, and keep records within safe DNS size budgets. AutoSPF operationalizes this end to end—resolving includes, aggregating and publishing flattened records across thousands of tenants, validating at scale, and providing the safety nets (canaries, alerting, rollback) you need to maintain deliverability with confidence. If you want SPF that just works at SaaS scale, wire your domains to AutoSPF and let it handle the complexity behind the scenes.

Brad Slavin
Brad Slavin

General Manager

Founder and General Manager of DuoCircle. Product strategy and commercial lead for AutoSPF's 2,000+ customer base.

LinkedIn Profile →

Ready to get started?

Try AutoSPF free — no credit card required.

Book a Demo