Skip to content

@ogygia/federation

Caching and failover

Each peer handle shares fetches, can keep a stale-while-revalidate cache of page documents, fails over across replicas, and bounds every call with a timeout. A peer that fails costs one part of the page, never the page, and each failure is logged with its code.

On this page

Each peer handle carries its own transport: shared fetches, an optional document cache, replicas and a timeout. You set them on the peer in federate(), and every region, mount and hole that uses the peer gets them:

import { federate } from '@ogygia/federation';
import { env } from '$env/dynamic/private';

export const { cms } = federate({
  name: 'shell',
  key: env.SHELL_SIGNING_KEY,
  peers: {
    cms: {
      origin: [
        'https://cms-1.internal',
        'https://cms-2.internal'
      ],
      audience: 'cms.internal',
      key: env.CMS_PUBLIC_KEY,
    timeout: 800,
    cache: { ttl: 30_000, max: 1000 }
    }
  }
});

Shared fetches

Requests that ask a peer for the same page document at the same time share one call, whether or not cache is set. A page is the same when its path, query string and claims are the same. When every request waiting on a call has ended, the call is cancelled.

Document cache

cache: { ttl, max } keeps the page documents behind peer.page(), deferred page holes and mount() reads. Widget calls aren’t cached. ttl is in milliseconds, and max, 500 by default, caps the number of entries, dropping the least recently used.

  • What’s kept. Only answers with a status under 400 and no redirect. Entries are keyed by path, query string and claims, so each visitor with their own claims gets their own entry, and the cache helps most with anonymous calls or claims many visitors share.
  • Stale while revalidate. An entry older than ttl is still served, and the read that finds it starts one refresh in the background, so a slow peer never delays a cached read. A refresh that fails, or answers an error, keeps the good copy, and the next stale read tries again.
  • How entries leave. Not by age. An entry leaves when the cache is full, when a thaw notice names one of its tags, or when a POST through the peer, such as a mounted form, clears the whole cache.

The cache lives in each server process’s memory. A thaw or a POST reaches only the process that received it, so on several instances an entry can outlive a change by up to its ttl plus the time until the next read refreshes it.

A read never returns a page from before a change it waited across. When a thaw or a POST lands while a fetch is out, and the answer carries a moved tag, the reads waiting on it fetch again. When that happens 16 times in a row, the read gives up with a 504 and logs federation_peer_read_dropped: something is clearing the page’s sources on every request.

Replicas

origin takes a list of origins that run the same build. Reads, page documents and widgets alike, ask the first, and move on to the next when it gives no answer, times out, or answers 500 or above. An answer under 500 is the answer, a 404 included. A POST goes only to the first origin: sending a form to a second server after the first may have run it risks writing twice.

Each replica is signed for its own host unless the peer sets audience, as above (Audience). All the origins are also where the peer’s islands may load code from on your pages (Code origins).

Timeouts

timeout bounds each attempt of an uncached call, 5000 milliseconds by default. Each replica is a new attempt with its own timeout, so a peer with three origins can take up to three timeouts before it fails. A cached read, fresh or stale, never waits.

Failures

A call fails when the peer gives no answer in time, or answers with something your app can’t use. Your app logs it once with a code, and the part of the page that needed it fails, never the page:

CodeMeans
federation_peer_unreachableNo answer: the connection failed, the name didn’t resolve, or nothing came back within timeout. Check the peer is up and reachable at its configured origin.
federation_peer_answer_invalidAn error status, or a body that isn’t the peer’s JSON document. A 401 or 403 means the two apps’ keys or audience don’t match; a 404 on a widget, that the peer has no widget of that name; a broken body, that a proxy or CDN answered.
federation_peer_read_droppedA page read whose fetch was dropped by thaws or posts 16 times (above).

The status each failure turns into is the peer’s own 503 or 504 when it sent one, 504 for no answer, and 502 for anything else. A page read throws it, a hole answers it, and a static widget shows a card in its place. Remote regions and Mounting apps show what the visitor sees for each.

A page the peer doesn’t have isn’t a failure: the peer answers its own not-found page as a document with status 404.

HTTP caches

Don’t put a CDN or a shared cache in front of the /og/ paths. A fragment’s answer depends on the signed claims in the request’s headers, which a cache keyed by URL doesn’t see, and a cache would keep one visitor’s answer for the next. A deferred region’s hole answers with cache-control: no-store for that reason. The catalog at /og/fragment/__catalog is the exception: it’s the same for every caller.

Tracing

The W3C traceparent header continues through mounts, mount.kit() and deferred holes: the call to the peer is a new span of the incoming request’s trace, so one trace id runs from the browser through your app to the peer, and on down a chain. Static regions break it today: a static peer.page() starts a trace of its own, and a static peer.widget() sends no traceparent at all (#2311, item 15).

  • On a mounted page, the response carries Server-Timing: <label>;dur=<ms> for the call and <label>-render;dur=<ms> for the peer’s own render, with the peer’s label (its name by default), and x-og-trace with the peer render’s trace id. DevTools’ network panel then shows which team made the page slow. A streamed mount sends neither, since its headers leave before the peer answers (#2311, item 14).
  • On the peer, every fragment and widget answer carries Server-Timing: og-fragment;dur=<ms>, and its document carries trace and server_ms.

child_traceparent(incoming) from @ogygia/federation makes a new span of an incoming traceparent, or starts a trace when there’s none, for calls you make yourself.