Skip to content

Global Edge Network & Sub-10ms Redirects

Why Shaf is architected 100% on Cloudflare Workers edge nodes with D1 and KV read-through caching.

Global Edge Network & Sub-10ms Redirects

Traditional URL shorteners run in centralized cloud regions (such as AWS us-east-1). When a visitor in Tokyo, Frankfurt, or São Paulo clicks a short link, the TCP/TLS handshake and HTTP request traverse intercontinental fiber back to a single data center, incurring 200–500ms of latency before redirecting.

Shaf eliminates geographic serialization by executing link resolution entirely within Cloudflare’s Anycast edge network across 300+ global points of presence (PoPs).


Centralized Latency vs. Edge Resolution

flowchart LR
    subgraph Centralized["Legacy Shortener (Centralized)"]
        V1["Visitor in Tokyo"] -->|"250ms WAN RTT"| CentralApp["us-east-1 Application"]
        CentralApp -->|"15ms DB Query"| CentralDB[("Primary Database")]
        CentralDB --> CentralApp
        CentralApp -->|"250ms WAN RTT"| R1["HTTP Redirect (500ms+ Total)"]
    end

    subgraph EdgeArch["Shaf Global Edge (Anycast)"]
        V2["Visitor in Tokyo"] -->|"< 5ms Anycast"| PoP["Tokyo Edge Node (NRT)"]
        PoP -->|"1ms KV Hit"| KV[("Edge KV Cache")]
        PoP -->|"< 1ms Rules"| Evaluator["Smart Routing Engine"]
        Evaluator -->|"Instant 308"| R2["HTTP Redirect (5-10ms Total)"]
    end

Edge Request Sequence Lifecycle

Every short link redirect follows an asynchronous, non-blocking lifecycle. The client redirect fires before analytics telemetry is dispatched to background workers:

sequenceDiagram
    autonumber
    actor Visitor as Visitor Device
    participant Edge as Cloudflare Anycast PoP
    participant KV as Workers KV Cache
    participant D1 as D1 Database (SQLite)
    participant Engine as Analytics Engine

    Visitor->>Edge: GET https://shaf.is/:slug
    Edge->>KV: Read link cache (Key: domain:slug)
    alt KV Cache Hit (~1-2ms)
        KV-->>Edge: StoredLink payload
    else KV Cache Miss (~3-5ms)
        Edge->>D1: Query SELECT WHERE slug = ? AND domain = ?
        D1-->>Edge: StoredLink record
        Edge->>KV: Asynchronously prime KV cache
    end

    Edge->>Edge: Evaluate smart routing (Device, Geo, Query string)
    Edge-->>Visitor: HTTP 308 Permanent Redirect (Location: destinationUrl)

    Note over Edge,Engine: Asynchronous dispatch via ctx.waitUntil()
    Edge-)Engine: writeDataPoint (Zero-PII telemetry)

Latency Breakdown Matrix

Resolution Phase Edge Subsystem Typical Latency Architectural Mechanism
Anycast Routing & DNS Cloudflare 1.1.1.1 Anycast 1–5 ms Routes client directly to nearest metropolitan edge node.
Edge TLS Termination Cloudflare Edge SSL 1–3 ms 0-RTT TLS 1.3 resumption; terminating at edge.
Key-Value Cache Lookup Cloudflare Workers KV 1–2 ms Globally distributed in-memory/NVMe edge key-value store.
Relational D1 Fallback Cloudflare D1 3–5 ms Read-replicated SQLite edge query on KV cold cache miss.
Smart Routing Evaluation V8 Runtime Isolate < 0.5 ms In-isolate regex and header checks (OS, Country, Query).
Telemetry Dispatch Cloudflare Analytics Engine 0 ms Non-blocking execution scheduled outside critical path via ctx.waitUntil().
Gross Redirect Latency 5–10 ms Median global time-to-first-byte (TTFB).

Two-Tier Hybrid Storage: D1 + Workers KV

Distributed systems must reconcile consistency against read latency. Shaf implements a two-tier hybrid data architecture:

Cloudflare D1: Authoritative Store

Edge-native relational SQL database built on SQLite with immediate consistency for writes:

  • Authoritative source of truth for short links, workspaces, custom domains, and RBAC rules.
  • Transactional integrity managed via Drizzle ORM schemas.
  • Replicated globally across primary and read-replicas for fast fallback reads.

Workers KV: Ultra-Fast Read Cache

Globally replicated key-value store optimized for high-volume read workloads:

  • Write-through caching: link writes in D1 immediately write through to KV.
  • Cache invalidation executed in milliseconds upon link updates or deletions.
  • Absorbs millions of concurrent clicks without degrading relational database connections.

Non-Blocking Asynchronous Telemetry

Many analytics systems introduce latency by writing access logs synchronously before returning the HTTP redirect response. Shaf decouples analytics logging from response delivery:

  1. The Worker constructs the HTTP 308 redirect response.
  2. The Worker registers the telemetry payload with Cloudflare’s runtime execution lifecycle:
    event.waitUntil(
      analyticsEngine.writeDataPoint({
        blobs: [domain, slug, country, deviceClass, browserEngine, refererDomain],
        doubles: [1],
        indexes: [orgId],
      })
    );
  3. The HTTP redirect is returned to the visitor immediately.
  4. Cloudflare’s edge runtime flushes the telemetry buffer to ClickHouse columnar storage in the background without holding up the visitor connection.

Type to search…

↑↓ navigate↵ selectEsc close