Recently, I launched a new DNS Lookup Tool on my personal website. While it looks like a simple web utility, the architecture behind it is a powerful case study in modern serverless computing. The entire backend for this tool runs directly on Cloudflare's edge network, which means it's incredibly fast, highly scalable, and—most importantly—costs me virtually nothing to operate, even with heavy traffic.

This article is a deep dive into the "why" and "how" of this project, breaking down the technical decisions and architecture. Let's explore the journey from a traditional server-based approach to a fully serverless, edge-computed application.

The Problem with a Traditional Backend

The first-generation version of my tool used a common and reliable architecture: a simple PHP script hosted on my web server. The flow was straightforward:

  1. A user enters a domain on the HTML page.
  2. The page's JavaScript sends a request to a dns-proxy.php file on my server.
  3. The PHP script uses the cURL library to securely call a public DNS-over-HTTPS (DoH) API, like Cloudflare's 1.1.1.1.
  4. The DoH API returns the DNS data to my PHP script.
  5. My PHP script forwards the JSON response back to the user's browser.

This works perfectly fine, but it has inherent drawbacks. Every single query from a user requires a round trip to my origin server, even if the query is for a common domain like google.com. This adds latency and consumes server resources.

The Serverless Solution: Moving to the Edge

The goal was to eliminate the middle-man (my PHP server) and move the logic as close to the user as possible. This is where **Cloudflare Workers** come in. A worker is a piece of JavaScript that runs on Cloudflare's global network of data centers, intercepting requests before they ever reach an origin server.

The New Architecture

The new flow is dramatically more efficient:

  • Step 1: A user on the HTML page makes a request to a custom API path on my own domain, like /api/dns-test.
  • Step 2: A **Cloudflare Worker Route** intercepts this request at the edge, preventing it from ever touching my origin server.
  • Step 3: The worker script executes directly on the edge node closest to the user. It parses the request, performs its own `fetch` call to Cloudflare's DoH API, and returns the JSON response directly to the user.
This architecture reduces latency by serving the request from a location geographically close to the user and completely offloads the work from my main web server.

Diving into the Code: The Cloudflare Worker

The core of the application is a single JavaScript file deployed to Cloudflare. It handles CORS, caching, and the core DNS lookup logic.

Handling Multiple Queries and Timeouts

One of the key features is the ability to look up multiple domains at once. To do this efficiently, the worker uses `Promise.allSettled()`. This allows it to send out requests for all domains in parallel and wait for all of them to complete, even if some of them fail or time out.

To prevent a single slow query from holding everything up, each `fetch` call is wrapped with an `AbortController` that enforces a 5-second timeout.

// A simplified version of the query logic
async function queryDns(host, type) {
    const controller = new AbortController();
    const timeoutId = setTimeout(() => controller.abort(), 5000);

    try {
        const response = await fetch(dohUrl, { signal: controller.signal });
        // ... process response
    } catch (error) {
        if (error.name === 'AbortError') {
            throw new Error('Query timed out.');
        }
        throw error;
    } finally {
        clearTimeout(timeoutId);
    }
}

The Final Result

By migrating from a traditional PHP backend to a serverless Cloudflare Worker, the tool gained several key advantages:

  • Lower Latency: Responses are served from the edge, closer to the user.
  • Infinite Scalability: The tool can handle a massive number of requests without impacting my origin server's performance.
  • Zero Cost: Cloudflare's generous free tier for Workers means this entire backend costs nothing to run.
  • Simplified Architecture: No server maintenance, no patches, no PHP versions to worry about. Just a single JavaScript file.

This project is a testament to the power of modern edge computing. It demonstrates that complex, high-performance applications can be built and deployed globally without the overhead of traditional server infrastructure.