Lesson 8 of 9 · 25m
Edge Architecture Patterns: Gateway, BFF, Rendering, and Resilience
Recognize the useful edge patterns — gateway, backend-for-frontend, edge rendering, fan-out, queue offload — and the failure modes each introduces.
Architecture patterns are valuable when they name a trade, not when they decorate a diagram. At the edge, the same small compute primitive can be a policy gateway, a backend-for-frontend, a rendering layer, a routing brain, or a resilience shim. Those are different jobs. Mixing them accidentally produces a 300-line Worker that owns every outage in the company.
Pattern one: policy gateway
The gateway pattern centralizes cross-cutting request decisions: authentication token validation, bot policy, redirects, header hygiene, tenant resolution, and routing.
if (!validSession(request)) {
return Response.redirect(new URL("/login", request.url), 302)
}
return fetch(new Request(env.ORIGIN, request))The win is consistency. The risk is coupling: every application inherits gateway behavior whether appropriate or not. Keep policy explicit, versioned, and observable. A gateway that silently rewrites semantics becomes impossible to debug.
Pattern two: backend-for-frontend
A BFF adapts upstream service responses to one client’s needs. At the edge it can trim payloads, aggregate low-cost reads, normalize errors, and shape cache keys:
const [profile, entitlements] = await Promise.all([
fetchProfile(request, env),
fetchEntitlements(request, env),
])
return Response.json({
displayName: profile.display_name,
plan: entitlements.plan,
})This is useful when mobile bandwidth or unstable upstream APIs justify it. It becomes harmful when the BFF grows into a shadow monolith with duplicated business rules. Put adaptation at the edge, but keep authoritative business decisions in services that own them.
Pattern three: edge rendering and streaming
Edge rendering works well when most content is cacheable and personalization is bounded. A shell can render quickly while personalized fragments stream or hydrate later:
<main>
<h1>Usage dashboard</h1>
<section id="tenant-summary">Loading current usage…</section>
</main>The key is deciding which parts must be correct before first paint. Do not stream critical authorization decisions after rendering privileged navigation. Also remember that edge-rendered does not automatically mean cache-friendly; personalized HTML can fragment the cache into one copy per user.
Pattern four: offload and asynchronous completion
For expensive or failure-prone work, accept the request, validate it, durably enqueue work, and return a job handle:
POST /exports
202 Accepted
Location: /jobs/01JBQM7R1DThis protects user latency and absorbs upstream contention. It also creates a new product surface: job status, retry behavior, cancellation, expiry, and user communication. Async is not a hiding place for bad workflows; it is a commitment to designing workflow state.
Pattern five: resilience shim
The edge can serve stale content, controlled degradation, maintenance responses, or read-only modes during origin failure. This is powerful because failure responses are themselves traffic. Decide ahead of time:
origin timeout for catalog read -> serve stale up to 24h
origin timeout for checkout write -> fail closed
origin timeout for recommendations -> omit module
origin 5xx for marketing page -> static fallbackA resilience plan that treats every route the same either takes checkout risk or needlessly degrades safe reads.
Field notes
- Label the pattern in code ownership and documentation. “The Worker” is not an architecture description.
- Do not retry non-idempotent writes at the edge unless idempotency keys or downstream deduplication make retries safe.
- Every fallback needs freshness, security, and cache rules. Fallbacks are responses too.
- Resist the temptation to implement product logic in routing glue merely because deployment is easy. Easy deployment amplifies bad placement.
Was this lesson helpful?