Chapter 3. Global DNS and Edge Networks (Advanced: Route 53 & Cloudflare)
Learning objectives
- Distinguish the DNS hierarchy and core Route 53 record types (
A,AAAA,CNAME,Alias,TXT,CAA), and explain why standardCNAMErecords are forbidden at the root domain (Zone Apex) and howAliasrecords solve that constraint. - Combine Route 53 routing policies (Simple, Weighted, Latency, and Failover) with health checks to design canary rollouts and automated disaster recovery failover.
- Explain why zero-downtime domain or server migrations require lowering the DNS TTL at least one full old-TTL window before the cutover timestamp.
- Contrast Cloudflare DNS-only (grey cloud) and proxied (orange cloud) modes, explain why the
FlexibleSSL/TLS mode causes infinite redirect loops or plaintext exposure, and configureFull (Strict)with Origin CA certificates. - Design WAF, rate limiting, and bot control rules with targeted exceptions and cryptographic HMAC signature verification so legitimate payment and authentication webhooks are never blocked by browser challenges.
- Configure CDN cache rules to prevent cache poisoning and response buffering on personalized API responses (
/api/*,Authorization,Set-Cookie) and real-time LLM streams (text/event-stream). - Combine Cloudflare Workers, Queues, and AI Gateway (observability, caching, rate limiting, and multi-provider fallbacks) into a resilient edge architecture for AI applications.
Introduction
Your application code passes every test and your database is healthy, yet users see ERR_TOO_MANY_REDIRECTS in their browsers; or you migrate your application to a new cloud server and half your customers keep hitting the decommissioned old server for the next six hours; or—worst of all—a user signs in and sees a complete stranger's account profile and private AI chat history rendered on their screen. You can read through your application code for days without finding a bug, because the culprit lives outside your codebase: in your DNS and edge network (CDN and reverse proxy) configuration.
From the instant a user types https://aidan.agentcat.app into their browser address bar until your application server returns its first byte, the request first queries authoritative DNS (Amazon Route 53 or Cloudflare DNS) to resolve an IP address, and then passes through a global edge proxy (Cloudflare) that terminates the TLS handshake, evaluates WAF rules, checks the edge cache, and executes edge Workers before finally reaching your Origin server. In AI services, a second edge path flows in the opposite direction: outbound requests from your backend to external LLM providers (OpenAI, Anthropic, Google) pass through an AI Gateway.
This chapter covers the nine essential architectural principles every senior infrastructure engineer must master—spanning from the internet's address book in Route 53 to proxy modes, TLS security, caching, serverless Workers, Queues, and AI Gateway across Cloudflare's global edge network.
Key concepts
The DNS resolution path and core record types: Why you cannot put a CNAME at the Zone Apex
The Domain Name System (DNS) is a hierarchical, distributed database that translates human-readable domain names (aidan.agentcat.app) into IP addresses (192.0.2.1 or 2001:db8::1) that network routers understand. When a user's device (stub resolver) asks an ISP or public Recursive Resolver (such as 1.1.1.1 or 8.8.8.8) for a domain, the recursive resolver walks the hierarchy—from a Root (.) nameserver to the Top-Level Domain (.app) nameserver and finally to the domain's Authoritative Nameserver (such as Route 53 or Cloudflare)—to fetch the final record, and then caches that answer for the duration of the record's TTL (Time To Live, measured in seconds).
The table below summarizes the core DNS record types you work with in production.
| Record type | Value stored | Primary purpose | Key design consideration |
|---|---|---|---|
A |
IPv4 address (203.0.113.10) |
Maps a domain directly to a static IPv4 server | Must be updated manually whenever the target server IP changes |
AAAA |
IPv6 address (2001:db8::10) |
Maps a domain to an IPv6 endpoint | Recommended alongside A records for modern mobile and cloud networks |
CNAME |
Another domain name (cname.vercel-dns.com) |
Points a subdomain (www.example.com) to a canonical target hostname |
Forbidden at the root domain (Zone Apex, example.com) by DNS standards! |
Alias (Route 53) / CNAME Flattening (Cloudflare) |
AWS resource (ALB, CloudFront, S3) or target hostname | Connects a root domain (Zone Apex) to a dynamic-IP cloud endpoint | The authoritative nameserver resolves the target into standard A/AAAA IPs at query time, making it valid at the Zone Apex |
TXT |
Arbitrary text string | Domain ownership verification and email authentication (SPF, DKIM, DMARC) |
Watch out for quotation marks, length limits, and accidentally overwriting existing TXT records |
CAA |
Authorized Certificate Authorities (letsencrypt.org, etc.) |
Prevents unauthorized CAs from mis-issuing TLS certificates for your domain | Subdomains inherit parent CAA records; you must list every CA used by your platforms (Vercel, Cloudflare, AWS) or auto-renewal will fail |
The single most frequently tested DNS principle on infrastructure exams is why standard CNAME records are forbidden at the root domain (Zone Apex, such as example.com) under RFC 1034 and RFC 2181.
According to the DNS specification, if a node in the DNS tree has a CNAME record, it is not allowed to have any other resource records of any type alongside that CNAME. However, the root of a DNS zone (the Zone Apex, example.com) must always contain an SOA (Start of Authority) record and NS (Name Server) records that define the zone itself. Because a CNAME cannot coexist with SOA and NS records, you can freely place a standard CNAME on a subdomain like www.example.com or api.example.com, but placing a standard CNAME on example.com violates the DNS protocol and breaks zone resolution(including MX email routing).
Cloud load balancers (such as AWS ALB) and CDNs (such as CloudFront or Vercel) provide dynamic hostnames rather than fixed IP addresses—so how do you point your root domain (example.com) to them if CNAME is illegal there?
- Amazon Route 53
Aliasrecords: A Route 53-specific extension. You configure anAliasrecord atexample.compointing to your ALB, CloudFront distribution, S3 website endpoint, or another record in the same hosted zone. When an external DNS resolver queries Route 53 for theArecord ofexample.com, Route 53 internally looks up the target's current IP addresses and returns a standardAorAAAAresponse. This satisfies the DNS specification at the Zone Apex, tracks IP changes automatically, and incurs zero Route 53 query charges when pointing to eligible AWS resources. - Cloudflare CNAME Flattening: When you enter a
CNAMEat the Zone Apex in Cloudflare DNS, Cloudflare's authoritative nameservers automatically follow the CNAME chain behind the scenes and return the final resolvedAorAAAAIP addresses to querying resolvers.
Route 53 routing policies and health checks for zero-downtime traffic control
Rather than mapping a hostname to a single static IP, Route 53 provides Routing Policies that dynamically steer DNS responses based on traffic weights, network latency, geographic location, or endpoint health.
| Routing policy | How it works | Classic production use case |
|---|---|---|
| Simple | Returns one or more IP addresses in a single record (chosen at random by the client) | Simple single-region services that do not need traffic splitting (cannot attach health checks directly) |
| Weighted | Distributes responses across records according to assigned numeric weights (for example, 90 vs. 10) | Canary deployments and gradual blue-green infrastructure migrations |
| Latency-based | Directs the user to whichever AWS region offers the lowest network round-trip latency from their location | Active-active global deployments running simultaneously in Seoul (ap-northeast-2) and Virginia (us-east-1) |
| Failover | Routes all traffic to a Primary record under normal conditions, and automatically switches to a Secondary record when the Primary health check fails | Active-passive disaster recovery (DR) and automatic maintenance/status page failover |
| Geolocation | Routes queries to fixed endpoints based on the requester's country or continent | Country-specific data sovereignty compliance and localized payment/content delivery |
A critical architectural detail to remember is that a routing policy by itself does not know whether your target server has crashed. For Weighted, Latency, or Failover routing to stop sending users to a dead server, you must create a Route 53 Health Check and attach it to the record (or enable Evaluate Target Health on an Alias record).
How you implement your application's health check endpoint (/api/health) matters just as much. If /api/health is a shallow check that blindly returns 200 OK without checking whether the database connection pool is alive, Route 53 will keep routing traffic to an instance whose database connection is dead and throwing 500 errors on every real user request. Conversely, if /api/health triggers an expensive live LLM API completion every few seconds when global health checkers ping it, your token bill will skyrocket. A well-designed health check verifies local process readiness and lightweight critical dependencies (such as running SELECT 1 against the database pool) and returns 503 Service Unavailable when the node cannot serve traffic.
Zero-downtime domain and server migrations: The law of lowering TTLs in advance
Let's examine one of the most common operational blunders during a server migration.
Suppose a team has been running their production A record (pointing to old IP 198.51.100.10) with a standard TTL of 86,400 seconds (24 hours). They schedule a migration to a new server (IP 203.0.113.50) for Sunday at 02:00 a.m. At 02:00 a.m. sharp on Sunday, an engineer logs into the DNS console, lowers the TTL to 60 seconds, changes the IP address to the new server at the same time, and shuts down the old server at 02:10 a.m.
What happens for the rest of Sunday? Over half of the company's customers cannot reach the site at all! Why didn't lowering the TTL to 60 seconds at 02:00 a.m. make the cutover take effect in 60 seconds?
Because ISP and public recursive DNS resolvers around the world that queried the domain at 01:50 a.m. cached the old IP (198.51.100.10) along with the old TTL instructions: "Keep serving this IP from cache for the next 24 hours (86,400 seconds) without asking the authoritative nameserver again!" Until that 24-hour cached timer expires on each recursive resolver, those resolvers never even contact your authoritative nameserver—so they have no way of knowing that you changed the IP or lowered the TTL at 02:00 a.m.
A true zero-downtime DNS migration must therefore follow a strict five-stage timeline:
- Advance preparation (at least 24–48 hours before cutover—strictly longer than the previous TTL): Keep the record pointing to the old IP address, and lower only the TTL to a short value (such as 60 to 300 seconds).
- Wait for old caches to expire: If the previous TTL was 24 hours (86,400 seconds), wait at least a full 24 hours. Once that window passes, every recursive resolver on the internet has expired the old 24-hour entry and is now refreshing the record every 60–300 seconds.
- Run old and new environments in parallel: Bring up the new server environment and verify that both the old server and the new server can safely handle live requests simultaneously (for example, talking to the same primary database).
- Execute the DNS cutover: Update the DNS record value to point to the new server IP (or new target hostname). Because every resolver is already honoring the 60–300 second TTL, global traffic shifts to the new server within 1 to 5 minutes.
- Observe traffic drain, decommission the old server, and restore the TTL: Watch the access logs on the old server until incoming requests drop to zero, safely decommission the old server, and finally raise the DNS TTL back to a normal operational value (such as 3,600 seconds) to reduce DNS resolution latency and query volume.
Cloudflare proxied (orange cloud) vs. DNS-only (grey cloud) modes and hiding the origin IP
When you manage a domain in Cloudflare, every DNS record has a cloud toggle switch next to it. Flipping that switch changes the entire network path of your traffic.
| Mode | Icon | What DNS queries return | Network traffic path | Active edge features |
|---|---|---|---|---|
| DNS Only | Grey cloud | Returns the origin server's real IP address or raw CNAME target verbatim | User → connects directly to the Origin server | Pure DNS resolution only (WAF, CDN caching, Workers, and L7 DDoS protection are inactive) |
| Proxied | Orange cloud | Replaces the answer with Cloudflare Anycast edge IP addresses | User → Cloudflare Edge → Origin server | WAF, Rate Limiting, CDN caching, SSL/TLS termination, Workers, AI Gateway, and origin IP masking all active |
When operating with the orange cloud (Proxied) enabled, you must watch out for two practical pitfalls:
- Third-party CNAME verification records and non-HTTP protocols often require Grey Cloud (DNS Only). When an external email provider (verifying a DKIM CNAME) or an application platform (such as Vercel or AWS Certificate Manager verifying a domain validation CNAME) queries DNS to check whether your CNAME points to their target string, leaving the orange cloud turned on causes Cloudflare to hide the CNAME and respond with Cloudflare
A/AAAAIP addresses instead—causing the external provider's verification check to fail. Furthermore, Cloudflare's standard orange-cloud proxy only forwards HTTP and HTTPS traffic (on supported ports such as 80 and 443), so attempting to run raw SSH (port 22) or PostgreSQL (port 5432) directly through a standard orange-cloud hostname will fail (use Cloudflare Tunnel or Spectrum instead, as covered in Chapter 4). - Turning on the orange cloud does NOT protect your origin server if its firewall still allows
0.0.0.0/0! If an attacker discovers your origin server's real IP address via historical DNS databases (such as SecurityTrails), a misconfigured DNS-only subdomain, or an IPv4 port scan, they can send HTTP requests with aHost: app.example.comheader directly to your origin IP—completely bypassing Cloudflare's WAF and rate limiting. To close this loophole, you must lock down the origin firewall so it accepts port 443 traffic only from Cloudflare's published edge IP ranges, enforce Authenticated Origin Pulls (mTLS client certificate verification), or remove all public inbound ports using Cloudflare Tunnel.
SSL/TLS encryption modes: Why you must never use Flexible and why Full (Strict) is the standard
When Cloudflare's proxy (orange cloud) sits in front of your site, every HTTPS connection is split into two distinct legs: Leg 1 is [User Browser ↔ Cloudflare Edge], and Leg 2 is [Cloudflare Edge ↔ Your Origin Server]. Cloudflare's SSL/TLS encryption mode setting controls how Leg 2 (Edge ↔ Origin) is connected.
| SSL/TLS mode | Browser ↔ Cloudflare | Cloudflare ↔ Origin Server | Critical flaw or operational behavior |
|---|---|---|---|
| Off | Plaintext HTTP | Plaintext HTTP | Zero encryption anywhere (never use) |
| Flexible | Encrypted HTTPS | Plaintext HTTP (port 80) | 1) Exposes the Edge-to-Origin leg in plaintext over the internet<br>2) Triggers an infinite redirect loop (ERR_TOO_MANY_REDIRECTS) if the origin redirects HTTP to HTTPS! |
| Full | Encrypted HTTPS | Encrypted HTTPS (no certificate verification) | Blindly trusts expired or self-signed origin certificates, leaving the origin leg vulnerable to active MITM attacks |
| Full (Strict) | Encrypted HTTPS | HTTPS + strict verification against a trusted public CA or Cloudflare Origin CA | The only recommended production standard. Guarantees end-to-end encryption and authenticates the origin server |
One of the most famous incidents on both infrastructure exams and real-world on-call shifts is the infinite redirect loop (ERR_TOO_MANY_REDIRECTS) caused by Cloudflare's Flexible SSL mode.
Why does that loop happen? Trace the steps:
- A user opens
https://app.example.comin their browser over HTTPS. - Because Cloudflare is set to
Flexible, Cloudflare strips the encryption and forwards the request to your origin server (Vercel, Nginx, AWS ALB, etc.) over plaintexthttp://app.example.comon port 80 (withX-Forwarded-Proto: http). - Your modern origin server or web framework has an automatic security rule enabled: "Whenever a request arrives over plaintext HTTP, redirect the client to HTTPS!" It responds with
301or308 Redirect to https://app.example.com. - The browser follows the redirect and requests
https://app.example.comagain → Cloudflare forwards it to the origin overhttp://again → the origin replies with a redirect tohttps://again—looping endlessly until the browser gives up and displaysERR_TOO_MANY_REDIRECTS!
The rule is simple and absolute: Never use Flexible mode; always use Full (Strict). If you manage your own origin server or load balancer and do not want the overhead of rotating short-lived public Let's Encrypt certificates on the origin, you can generate a free Cloudflare Origin CA certificate (valid for up to 15 years and trusted specifically by Cloudflare's edge), install it on your origin server or ALB, and set the mode to Full (Strict).
WAF, rate limiting, and bot controls while protecting legitimate webhooks
At the Cloudflare edge, three defensive layers filter out malicious traffic before it ever touches your origin server:
- WAF Managed Rules: Inspect incoming HTTP requests to block SQL injection, cross-site scripting (XSS), command injection, and newly disclosed framework CVEs.
- Rate Limiting Rules: Throttle or block clients (
429 Too Many Requests) that exceed a safe request threshold within a short window—such as brute-force login attempts against/api/auth/login(more than 10 requests per minute) or abusive calls to an expensive AI generation route/api/chat(more than 20 requests per minute). Because hundreds of legitimate users inside a corporate office, university campus, or mobile carrier NAT gateway often share a single public IPv4 address, rate limiting on authenticated routes should key on session cookies, user IDs, or API tokens rather than raw IP addresses alone to avoid false positives. - Bot Management (Bot Fight Mode / Super Bot Fight Mode): Presents JavaScript challenges or Turnstile CAPTCHAs to automated scrapers and headless browsers.
A classic production outage occurs when Bot Fight Mode or an overly aggressive WAF challenge rule intercepts legitimate server-to-server webhooks from payment providers (Toss, Stripe, Lemon Squeezy), GitHub, or Slack, flags them as "automated bot traffic," and returns an HTML JavaScript challenge page—causing completed customer payments to be silently dropped! External webhook servers are backend HTTP clients, not web browsers; they cannot execute JavaScript challenges or solve CAPTCHAs.
To protect webhook endpoints (such as /api/payments/webhook) properly, combine two controls:
- Configure a targeted WAF custom rule / skip rule for the exact webhook path (
/api/payments/webhook) so interactive browser challenges (JS Challenge / Managed Challenge) are skipped for that route, while keeping protocol and payload sanity checks active. - In your application (or Edge Worker) handler, cryptographically verify the sender's HMAC signature header (such as
X-SignatureorStripe-Signature) and timestamp against your webhook signing secret before processing the event, rejecting any unsigned or replayed request with401 Unauthorized.
CDN cache rules and preventing cache poisoning on dynamic APIs and streams
Edge CDN caching is wonderful for speeding up static assets (/static/*, fonts, and public images) and offloading your origin server. However, misconfiguring Cache Rules so that the CDN caches dynamic, user-specific responses triggers one of the worst data breaches in web engineering: Cache Poisoning / Web Cache Deception.
Imagine what happens if an administrator tries to reduce origin server load by adding a Cloudflare Cache Rule matching URI Path starts with /api/ (or the entire site *) with Cache Everything and Ignore Cache-Control enabled:
- Alice signs in to her account and fetches
/api/me(or a server-side rendered/dashboardpage). - The origin server responds with Alice's full name, email address, private AI consultation history, and even a
Set-Cookie: session=alice_secretresponse header. - Because
Cache Everythingis active, the Cloudflare edge node saves Alice's entire response under the shared cache key forhttps://app.example.com/api/me! - One second later, Bob (an unrelated user—or even an unauthenticated visitor) requests
/api/me. Without even contacting the origin server, Cloudflare serves Alice's private profile JSON and Alice's active session cookie straight from the edge cache into Bob's browser!
To prevent catastrophic cache leaks and broken streams in AI and web services, enforce the four caching rules in the table below without exception.
| Traffic category | Correct caching design (Cloudflare + Origin) | What goes wrong if misconfigured |
|---|---|---|
Personalized API routes (/api/*), session checks, carts, account pages |
Cloudflare Cache Rule: Bypass Cache + Origin header: Cache-Control: private, no-store |
Leaks one user's private account data and session cookies to other users |
Pages that vary by Authorization header or session cookie |
Bypass the shared cache (or strictly partition cache keys and Vary headers; never cache responses containing Set-Cookie in a shared cache) |
Replicates the first visitor's personalized view to every subsequent visitor |
Real-time LLM streaming responses (Content-Type: text/event-stream, SSE) |
Bypass Cache + disable response buffering (Cache-Control: no-cache, no-transform, X-Accel-Buffering: no) |
The edge buffers chunks for 30 seconds until generation finishes before flushing all at once, breaking real-time token streaming and causing timeouts |
Content-hashed immutable static bundles (/_next/static/*, /assets/*.a1b2c3.js) |
Cache aggressively at the edge and browser with Cache-Control: public, max-age=31536000, immutable |
Wastes bandwidth and adds latency by re-fetching unchanged static bundles from the origin |
Cloudflare Workers and Queues: Edge computing and decoupling asynchronous work
Cloudflare provides two serverless building blocks that complement your core application server: Cloudflare Workers, a lightweight edge compute runtime built on V8 isolates, and Cloudflare Queues, an asynchronous message broker.
- Cloudflare Workers: Instead of booting heavy virtual machines or containers, Workers spin up isolated V8 execution contexts in a few milliseconds, achieving near-zero (0 ms) cold starts across hundreds of cities worldwide. They excel at lightweight front-door tasks right at the edge: verifying JWT signatures before invalid requests ever reach your origin, routing users to regional payment currencies or languages based on the
CF-IPCountryheader, or injecting security headers (Strict-Transport-Security,Content-Security-Policy). Keep in mind that Workers have strict CPU time limits per invocation, and if a Worker has to make multiple sequential queries to a single centralized database on the other side of the world, it faces the exact same network RTT physics discussed in Chapter 1 (unless paired with Smart Placement or edge caches). - Cloudflare Queues: AI applications frequently perform heavy background work that cannot finish within a 1-second interactive HTTP request—such as chunking and embedding a 100-page PDF, sending bulk notification emails, or reconciling complex billing webhooks. Running those tasks synchronously inside the user's HTTP request leads to timeouts and lost work whenever a downstream API hiccups. Instead, a front-door Producer Worker validates the request, pushes a job message onto Cloudflare Queues, and immediately returns
202 Acceptedto the client. Behind the queue, a Consumer Worker pulls messages in controlled batches to process them at a steady pace, smoothing out traffic spikes (buffering). If a batch fails due to a transient error, Queues retries automatically with backoff; messages that still fail after the maximum retry count are routed to a Dead Letter Queue (DLQ) so zero jobs are lost and engineers can inspect and replay them safely.
Cloudflare AI Gateway: Observability, caching, rate limiting, and fallback for LLM traffic
When your backend application calls external LLM APIs—such as OpenAI, Anthropic, or Google Gemini—directly, three operational headaches quickly emerge:
- Because multiple features and agents call different model providers independently, you lack a unified view of how many tokens and dollars each feature consumes, or what its Time to First Token (TTFT) and error rate look like.
- Users repeatedly trigger identical deterministic prompts (such as classifying a standard template or translating a fixed UI snippet), burning paid LLM tokens and waiting several seconds every single time.
- Whenever your primary LLM provider suffers a regional outage (
502/503) or hits an organization rate limit (429), your entire AI product goes down with it.
Cloudflare AI Gateway sits between your application backend and external LLM provider APIs as a dedicated LLM reverse proxy and control plane. By changing just the SDK baseURL in your application code to point to your AI Gateway endpoint, you gain five powerful infrastructure controls without rewriting your application logic.
| AI Gateway feature | How it works and its operational benefit |
|---|---|
| Unified observability (Analytics & Logging) | Tracks request volume, input/output token counts, estimated cost, latency, and error codes (429/5xx) across multiple LLM providers in one dashboard |
| Exact and semantic caching (Caching) | Serves identical deterministic prompt requests directly from the edge cache in tens of milliseconds at zero token cost (must be disabled or scoped carefully for requests containing private user data) |
| Rate limiting and budget protection (Rate Limiting) | Caps the number of requests per minute/hour at the gateway so runaway agent loops or traffic spikes are throttled before running up huge LLM bills |
| Automated retries and multi-provider fallback (Universal Endpoint / Fallbacks) | If the primary model (for example, OpenAI) returns a 5xx outage error or times out, automatically retries or fails over to a backup provider (such as Anthropic Claude or Google Gemini) to keep the service up |
| Centralized authentication and key management (Authenticated Gateway / BYOK) | Enforces a gateway authentication header (cf-aig-authorization) so only authorized backend services can send traffic through your gateway |
Going deeper
DNS weighted routing vs. HTTP edge routing in zero-downtime blue-green migrations
Suppose you are executing a major platform migration and want to run a canary release that sends 5% of live traffic to the new infrastructure (Green) while keeping 95% on the existing infrastructure (Blue). Should you split traffic using Route 53 Weighted Routing (at the DNS layer) or Cloudflare Workers / Load Balancing (at the HTTP edge proxy layer)? The two layers behave very differently in granularity and rollback speed.
| Comparison criterion | Route 53 Weighted Routing (DNS layer) | Cloudflare Edge Routing (HTTP proxy layer) |
|---|---|---|
| Unit of traffic distribution | Per recursive DNS resolver query (tens of thousands of users behind one ISP resolver may all receive the same IP) | Per individual HTTP request or user session/cookie (can split exactly 5.0% of requests or users) |
| Emergency rollback speed | Even after setting weight to 0, some traffic continues hitting the broken environment until DNS TTLs and client OS caches expire (minutes to tens of minutes) |
Takes effect globally within 1–2 seconds for 100% of subsequent HTTP requests as soon as the edge rule updates |
| Session stickiness | If a resolver re-queries and receives the other IP, an active user may flip between Blue and Green mid-session | Can hash a session cookie or user ID so a given user stays pinned consistently to either Blue or Green |
| Best suited for | Macro-level migrations across different cloud or CDN providers, or non-HTTP protocols | Fine-grained application canary rollouts and deployments that require instant 1-second rollback |
In practice, the two layers complement each other: use the HTTP edge proxy layer (Cloudflare Workers or Load Balancing) for fine-grained canary traffic splitting and instant circuit breaking between application origins, and use DNS routing policies (with pre-lowered TTLs) for macro-level disaster recovery across regions or CDN providers.
Origin IP leak paths and complete Origin Lockdown
Even if you spend thousands of dollars a month on enterprise Cloudflare WAF rules, your WAF is a paper tiger if an attacker can discover your origin server's IP address—or its default platform hostname (such as my-app.vercel.app or an AWS ALB's *.elb.amazonaws.com hostname)—and send HTTP requests straight to the origin around Cloudflare.
In real-world audits, origin addresses leak through a surprising variety of paths:
- Historical DNS records from before Cloudflare was enabled remain indexed in public DNS history databases.
- Mail records (
MX,mail.example.com) or a forgotten staging subdomain (dev.example.com) point to the same server IP with the grey cloud (DNS Only) enabled. - An application feature—such as fetching a user-supplied avatar URL (SSRF) or sending outbound webhook/email traffic directly from the web server—reveals the origin server's public egress IP.
- A managed platform's default domain (such as
aidan-mu.vercel.app) is left publicly reachable without a redirect or Deployment Protection.
True edge security therefore requires completing Origin Lockdown so that "any request that did not pass through our authorized Cloudflare zone is rejected at the origin door":
- Network firewall layer: Restrict inbound port 443 on your origin Security Group from
0.0.0.0/0to Cloudflare's published IPv4/IPv6 CIDR ranges, or close all inbound firewall ports completely and connect via Cloudflare Tunnel (Chapter 4). - Mutual TLS layer (Authenticated Origin Pulls, mTLS): To prevent an attacker from creating their own free Cloudflare account, pointing their domain at your origin IP, and piggybacking through Cloudflare's IP ranges, enable Zone-Level or Custom Certificate Authenticated Origin Pulls (mTLS) so your origin server cryptographically verifies that the incoming TLS client certificate belongs specifically to your Cloudflare zone.
- Managed platform domain layer: When using a managed platform like Vercel as your origin, configure default
.vercel.appproduction domains to return a308 Permanent Redirectto your canonical custom domain (https://aidan.agentcat.app), or verify a signed custom origin secret header before executing expensive routes.
Workplace scenarios
Byeolmuri Commerce: Infinite redirect loops and shopping cart cache poisoning on Cloudflare launch day
To prepare for Black Friday traffic spikes and overseas credential-stuffing bots, AI gift-recommendation store Byeolmuri Commerce decided to place Cloudflare in front of its existing AWS Application Load Balancer (ALB). On Friday evening, an engineer switched the domain's authoritative nameservers to Cloudflare and changed two settings: first, to avoid touching certificates on the origin, they set Cloudflare's SSL/TLS mode to Flexible; second, hoping to slash origin CPU usage, they created a Cache Rule matching * (all URLs) with Cache Level: Cache Everything.
Five minutes after the cutover, the entire store went down. First, half of the shoppers could not even load the home page because their browsers failed with ERR_TOO_MANY_REDIRECTS. The AWS ALB had an existing listener rule redirecting all HTTP (port 80) requests to HTTPS (port 443), which collided head-on with Cloudflare's Flexible mode forwarding every HTTPS request to the ALB over plaintext HTTP port 80.
When the engineer hastily disabled the HTTP-to-HTTPS redirect rule on the ALB, the site loaded—only to trigger a far worse disaster ten minutes later. Customer support was flooded with panicked calls reporting: "When I open my shopping cart and saved shipping addresses, I see a complete stranger's name, phone number, home address, and private AI gift consultation chat!" Because of the blanket Cache Everything rule, the JSON responses for /api/cart and /api/chat/history requested by the first signed-in shopper had been cached at the Cloudflare edge and served verbatim to every subsequent visitor—a textbook cache poisoning data breach. On top of that, the real-time AI chat stream (/api/chat/stream) stopped typing word by word, freezing for 20 seconds before dumping the entire paragraph at once.
An infrastructure architect paged into the incident resolved the crisis within 15 minutes by executing three steps in order:
- Immediate full cache purge (
Purge Everything) and dynamic route bypass: They purged the entire Cloudflare edge cache immediately to wipe out the cached personal data. Next, they replaced the broken cache rule so that/api/*,/auth/*,/checkout/*, and any request carrying aCookieorAuthorizationheader are strictly set toBypass Cacheat the highest priority. On the origin server, they enforcedCache-Control: private, no-storeon all personalized responses andContent-Type: text/event-streamwithCache-Control: no-cache, no-transformon SSE streaming routes. - Switching to
Full (Strict)and restoring HTTPS redirects: Because the AWS ALB already had a valid AWS Certificate Manager (ACM) TLS certificate attached on port 443, they switched Cloudflare's SSL/TLS mode toFull (Strict)and re-enabled the ALB's HTTPS redirect rule—eliminating the redirect loop while encrypting traffic end to end. - Post-incident containment and notification: By cross-referencing 12 minutes of edge access logs, the team identified the 18 user sessions whose shipping details could have been viewed by another visitor, invalidated all active session tokens, and worked with the privacy officer and legal team to complete mandatory breach notifications.
Narae AI Lab: Surviving an OpenAI outage with AI Gateway fallbacks and migrating DNS with advance TTL reduction
Narae AI Lab, which runs an enterprise meeting-minutes summarization platform, faced two high-stakes infrastructure projects: migrating their authoritative DNS and API endpoints from a legacy domestic registrar to Amazon Route 53 with zero downtime, and eliminating the single point of failure (SPOF) that caused their entire summarization pipeline to halt whenever a single LLM vendor had an outage.
For the DNS migration, on Thursday morning—three full days (72 hours) before the scheduled Sunday cutover—the team lowered the TTL on every DNS record at the existing registrar from 36,000 seconds (10 hours) down to 60 seconds. By Sunday at 03:00 a.m., every recursive resolver worldwide had long since expired the old 10-hour cache and was re-querying every 60 seconds. The team configured their Route 53 hosted zone using an Alias record at the Zone Apex (narae.ai) and health-checked Failover records on the API subdomain (api.narae.ai), then executed the cutover. Thanks to the advance TTL reduction, traffic shifted completely to the new infrastructure in under two minutes with zero dropped requests, after which the team restored the TTL to 3,600 seconds.
Next, the team placed Cloudflare AI Gateway and Cloudflare Queues into their LLM summarization pipeline.
| Pipeline bottleneck | Previous architecture | Hardened Edge & AI Gateway architecture |
|---|---|---|
| Long summarization jobs after audio transcription completes | Webhook handler called the LLM synchronously, causing frequent HTTP timeouts and dropped retries | Transcription webhook verifies HMAC signature, pushes a job onto Cloudflare Queues, and returns 202 Accepted immediately → Consumer processes in batches with a DLQ for failed jobs |
Transient outages (503/429) at a single LLM provider |
Whenever OpenAI experienced an outage, the entire meeting summarization service failed | Configured AI Gateway Universal Endpoint: calls the primary model first, and on consecutive 5xx/timeout errors automatically fails over to a secondary provider (Anthropic Claude) |
| Repeated classification calls on identical public meeting templates | Paid full token cost and waited 1.5 seconds on every repeated call | Enabled AI Gateway caching (1-hour TTL) strictly on deterministic template-classification prompts containing zero personal data, cutting token spend by 38% |
Two months later, when their primary LLM provider suffered a 40-minute global API outage, Narae AI Lab's pipeline automatically failed over to the backup model through AI Gateway within two seconds—passing through the entire outage window with zero customer support tickets.
Common mistakes
- "You can put a standard
CNAMErecord on the root domain (example.com) just like you do onwww.example.com." By DNS protocol standards (RFC 1034 and RFC 2181), aCNAMErecord cannot coexist with any other record type at the same node name. Because the Zone Apex (example.com) must always holdSOAandNSrecords to define the zone, placing a standardCNAMEthere is forbidden; you must use an Amazon Route 53Aliasrecord or Cloudflare CNAME Flattening. - "If I lower the DNS TTL to 60 seconds at the exact same moment I change the server IP on migration day, the whole internet will switch within 60 seconds." Recursive DNS resolvers around the world that queried your domain prior to the cutover keep serving the old IP address from cache for the full duration of the previous TTL (for example, 24 hours) without contacting your authoritative nameserver again. You must lower the TTL at least one full old-TTL window before the migration date and wait for old cached entries to expire first.
- "Setting Cloudflare SSL/TLS mode to
Flexibleis a safe and convenient way to avoid installing a certificate on my origin server."Flexibleforwards traffic between the Cloudflare edge and your origin server over unencrypted plaintext HTTP (port 80), and if your origin server redirects HTTP to HTTPS, it triggers an immediateERR_TOO_MANY_REDIRECTSinfinite loop in users' browsers. Always install an origin certificate (such as a Cloudflare Origin CA certificate) and useFull (Strict). - "Once the Cloudflare orange cloud (Proxied) is turned on, leaving my origin server's security group open to
0.0.0.0/0is completely safe." The orange cloud only changes DNS responses to return Cloudflare edge IP addresses. If an attacker discovers your origin server's real IP address through DNS history logs or scanning, they can send HTTP requests directly to that IP and bypass your WAF completely—unless you enforce Origin Lockdown using a Cloudflare IP allowlist, Authenticated Origin Pulls (mTLS), or Cloudflare Tunnel. - "Turning on CDN
Cache Everythingfor/api/*routes is a great way to reduce backend database load." Caching endpoints that return user-specific JSON data orSet-Cookie/Authorizationheaders in a shared CDN cache causes severe cache poisoning, serving the first authenticated user's personal profile and session tokens to strangers. Personalized APIs and SSE streams (text/event-stream) must always be configured toBypass Cache. - "When an external platform gives me a CNAME record to verify domain ownership or issue a TLS certificate, I should turn on the orange cloud (Proxied) for extra security." Turning on the orange cloud causes Cloudflare to respond with its own
A/AAAAIP addresses instead of the target CNAME hostname string, causing the external platform's CNAME verification check to fail. External domain-verification CNAME records must be left on grey cloud (DNS Only).
Summary at a glance
| Core topic | Key takeaway for the exam and production |
|---|---|
| Zone Apex & Alias | Standard CNAME is forbidden at the root domain (example.com); use Route 53 Alias records or Cloudflare CNAME Flattening instead |
| Routing policies & health checks | Attach dependency-aware health checks to Weighted (canary), Latency, and Failover (DR) records so traffic never routes to unhealthy origin nodes |
| Zero-downtime DNS cutover | Lower the TTL to 60–300 seconds at least one full old-TTL window before cutover so old recursive resolver caches expire before you change the IP |
| Orange cloud vs. Grey cloud | Orange cloud (Proxied) routes traffic through the Cloudflare edge (WAF, CDN, Workers); Grey cloud (DNS Only) exposes the raw origin IP or CNAME target |
SSL/TLS Full (Strict) |
Flexible leaves the origin leg in plaintext HTTP and causes infinite redirect loops. Always pair an origin certificate (Origin CA, etc.) with Full (Strict) |
| Origin Lockdown | Block direct-to-IP WAF bypasses by enforcing Cloudflare IP firewall allowlists, Authenticated Origin Pulls (mTLS), or Cloudflare Tunnel |
| Webhooks vs. WAF/Bot rules | Exempt server-to-server webhook paths from interactive JS challenges/CAPTCHAs, and authenticate webhook payloads in code via cryptographic HMAC signatures |
| Preventing cache poisoning | Set personalized /api/* routes (Authorization, Set-Cookie) to Bypass Cache + private, no-store, and disable caching/buffering on SSE (text/event-stream) |
| Workers, Queues & AI Gateway | Use Workers for low-latency edge auth/routing, Queues for async buffering and DLQs, and AI Gateway for LLM observability, rate limits, and multi-provider fallbacks |
Check the official docs
- AWS Route 53 — Choosing between alias and non-alias records: Zone Apex constraints and how
Aliasrecords compare toCNAMErecords. - AWS Route 53 — Choosing a routing policy & Health Checks: Simple, Weighted, Latency, and Failover routing policies and integrating health checks.
- Cloudflare — Proxy status (Orange vs Grey Cloud): How orange-cloud proxying works and when CNAME verification requires DNS-only mode.
- Cloudflare — SSL/TLS Encryption modes (
Full (Strict)) & Origin CA: WhyFlexiblecauses redirect loops and how to configureFull (Strict)with Origin CA certificates. - Cloudflare — Cache Rules & Preventing Cache Poisoning: Bypassing cache for dynamic content and designing safe cache keys.
- Cloudflare — AI Gateway Documentation: Unified LLM logging, caching, rate limiting, and Universal Endpoint multi-provider fallback routing.
Self-check
3-1. Explain why the DNS specification permits CNAME records on subdomains (www.example.com) but forbids CNAME records at the root domain (Zone Apex, example.com), and name the record feature used in Amazon Route 53 to connect a root domain to an AWS Application Load Balancer (ALB).
3-2. When planning a zero-downtime server IP migration for a domain whose current DNS TTL is 86,400 seconds (24 hours), explain in terms of recursive DNS resolver caching why you must lower the TTL to 60–300 seconds at least 24 hours in advance rather than on the day of the migration.
3-3. When an origin web server is configured to redirect all plaintext HTTP requests to HTTPS with a 301 status code, explain what browser error occurs (and why) if Cloudflare's SSL/TLS encryption mode is set to Flexible, and state the correct SSL/TLS mode to use instead.
3-4. Even after you enable the orange cloud (Proxied) and configure comprehensive WAF rules in Cloudflare, explain how an attacker who discovers your origin server's real IP address can bypass the WAF completely, and list at least two controls you can implement on the origin side to block direct access.
3-5. For a /api/profile endpoint that returns personalized user data and a /api/chat/stream (text/event-stream) endpoint that streams real-time LLM tokens, describe the specific incidents that occur if a CDN applies blanket caching and response buffering to both routes, and state the correct Cache-Control headers and CDN cache rule settings for each.
3-6. Design task: A customer-support summarization service that relies on a single external LLM API (such as OpenAI) frequently goes down due to transient provider 503 outages and runaway agent loops that spike hourly request counts. Explain how introducing Cloudflare AI Gateway solves both problems by naming three of its core features.
Hands-on tasks
Exercise
Write a zero-downtime domain migration timeline and Route 53 / Cloudflare record table (45 minutes)
- Assume the domain
service.kris currently hosted on a legacy DNS provider with a TTL of 43,200 seconds (12 hours) and must be migrated with zero downtime to a new Route 53 and Cloudflare architecture at 03:00 a.m. on Sunday. Write an hour-by-hour migration runbook covering D-2 days, D-Day 02:30, 03:00, 03:30, and D+1 day. - Build a table for the following five records specifying (a) the record type (
A/AAAA/CNAME/Alias/TXT/CAA) and (b) the Cloudflare proxy state (Orange Cloud vs. Grey Cloud) along with the technical justification for each:- Root web domain
service.kr - API subdomain
api.service.kr - External email provider DKIM verification record
s1._domainkey.service.kr(CNAME) - Certificate Authority restriction record on
service.kr(CAA) - Internal SSH bastion hostname
ssh.service.kr
- Root web domain
Completion criteria: The TTL reduction step occurs strictly more than 12 hours before the Sunday 03:00 cutover, and the table accurately explains Zone Apex handling as well as why external CNAME verification and non-HTTP SSH records require Grey Cloud (DNS Only) or specialized tunnels.
Exercise
Create an edge security, caching, and AI Gateway architecture checklist (50 minutes)
- Create a table with five rows representing the key routes of an AI web application behind Cloudflare:
/_next/static/*,/,/api/user/me,/api/payments/webhook, and/api/ai/stream. - For each route, fill in four columns: (a) Cloudflare Cache Rule policy (
Cache Everythingvs.Bypass Cache), (b) the originCache-Controlresponse header value, (c) how WAF / Bot challenges and Rate Limiting should be applied, and (d) additional application-layer verification (session validation, webhook HMAC signature verification, etc.). - Write pseudocode or a JSON routing array showing a Cloudflare AI Gateway Universal Endpoint configuration for
/api/ai/streamthat calls a primary model first and automatically fails over to a secondary model from a different vendor on error or timeout.
Completion criteria: Personalized APIs, webhooks, and SSE streams are completely excluded from shared edge caching (Bypass Cache), the webhook route is exempted from interactive JS challenges while protected by HMAC signature verification, and the AI Gateway configuration defines cross-provider fallback.