Skip to content
MSO CloudDocumentation

ADR 0044 — Compiled render plan and the binding cache

On this page
  • Status: Accepted. Batches B1/B2 (2026-09-03), commits d334b80e, 4e1b872b.
  • Owners: Platform (site programme).
  • Extends: ADR 0029 (the "declare once, materialise, invalidate on write" precedent this ADR follows for layout), the existing dataset-memo.ts/ payload-cache.ts in-process cache family this ADR generalises.

Context#

Every dashboard render re-derives its grid placement from dashboard_widgets.position and re-runs every widget's data query, even though the layout arithmetic (planPackedRows/packDeckRows, deck-pack.ts) is pure and already shared by three callers, and one widget-data path (explore.dataset) already proved that a binding-keyed, TTL, in-process cache works well for this shape of value. Neither win reached the majority render path (explore.query, most chart widgets) or the layout itself.

Decisions#

D1 — dashboards.compiled_plan: the packer's output, written once at save

Migration 0190: dashboards.compiled_plan (jsonb, CompiledDeckPlan = {hash, entries: {widgetId, position}[]}, position-only, keyed by dashboard_widgets.id), compiled_plan_version (integer — see D2), compiled_at (timestamptz). SSR reads this column directly instead of calling planPackedRows on every request. The compiled artefact carries no locale-resolved label — a copy edit or an i18n string change never invalidates it; localizedText stays the one place a label resolves. Written in the same transaction as every save / template-apply / editor commit that changes dashboard_widgets.position or config (never an async recompile job — the compiler is pure and cheap enough to run inline). A theme change or an i18n label edit does NOT invalidate the compiled plan, stated explicitly so a future change does not wire an unnecessary recompile.

D2 — Staleness is compiler-version-checked, not a per-save counter

compiled_plan_version holds COMPILED_PLAN_VERSION (apps/web/src/server/deck-compiled-plan.ts) — a CODE constant, not an incrementing counter. A row whose stored version does not match the currently deployed compiler is stale (a packing-algorithm change shipped since the row was compiled), and the read path re-derives it live rather than serving a plan an old algorithm produced, repairing the column lazily on that same read. This is a deliberate departure from an earlier per-save-counter design: it lets a compiler ALGORITHM change (not just a data change) invalidate every stored plan at once, with no migration and no fan-out write across every tenant's dashboards.

D3 — Rollout is lazy, never a batch recompute

compiled_plan = NULL / compiled_plan_version = NULL for every pre-migration row — additive, no backfill script. The render path falls back to a live pack when the column is NULL or stale, and the next save (or the lazy read-time repair, D2) populates it. Observed in production: roughly 1 in 20 dashboards needed a lazy repair on first post-migration read, then never again. This avoids a fan-out write across every tenant's decks, which the platform's own operational constraint (no batch recompute of every org's dashboards in one pass) forbids.

D4 — Drift guard

deck-compiled-plan.test.ts asserts the stored plan reproduces byte-for-byte from re-running the packer over the current widget set — the same technique ADR 0029's v1-to-v2 upcast already used to pin parity.

D5 — resolveBinding: one cache wrapper, every widget-data resolver routes through it

apps/web/src/server/binding-cache.ts generalises createPayloadMemo (already used twice independently, payload-cache.ts) with a third instantiation. Key: (org, brand, datasetVersion, bindingHash, filterHash) — locale is deliberately not part of the key: a resolved result frame is numbers and dimension keys, never a rendered label, so a locale leg would only fragment the cache for no correctness benefit. explore.query's runMetricOrchestrated/resolveCubeMetric/resolvePillarMetric dispatch and explore.dataset (via a thin rewrite of dataset-memo.ts) both route through it — the cache boundary sits ABOVE every resolver, not inside any one of them. In-process, not Redis, for the same two reasons payload-cache.ts already states: the cached value is an expensive-to-serialise object graph, and the dominant win is coalescing concurrent in-flight loads into one query plan, which a distributed cache cannot do across app instances without its own locking protocol — accepted per-instance staleness, bounded by TTL, is never a correctness issue because every entry is deterministic by construction. TTL_MS (default 60s) and MAX_ENTRIES (default 512, up from dataset-memo.ts's 256 now that this memo also carries the majority of widget renders) are env-adjustable, not hardcoded.

D6 — Invalidation: TTL by default, datasetVersion bump on a shape change only

No active pub/sub bust-on-every-write. An ordinary data refresh (ingest, rollup recompute) is bounded by the TTL alone — the same posture dataset-memo.ts already accepted. A SHAPE change (a dataset field added or redefined, ADR 0040 D4) bumps the relevant datasetVersion scope (bumpDatasetVersion), making every cache entry keyed on the old version unreachable immediately rather than waiting out the TTL.

D7 — Cross-process invalidation via the event bus (B2)

Because the web process and the worker process each keep an independent in-process cache, a worker-side rollup refresh cannot bump the web process's own scope directly. The worker tails every refresh and publishes materialization.refreshed / dataset_rollup.refreshed on @yng/eventbus; the web process subscribes once and bumps the matching binding-cache scope (binding-cache-eventbus-hook.ts). A Redis outage logs once and falls back to the TTL alone — a publish failure must never fail a rollup. binding-cache-invalidation-wired.py statically guards both ends of this wiring at once: a worker refactor that keeps registering a listener but silently stops calling publish(...), or a web-side refactor that drops the subscription, degrades every refresh back to "wait up to 60s" with no test failure and no runtime error by design (the swallowed catch) — this audit is what catches the regression instead of a page.

D8 — Every resolveBinding key is built through one function

buildBindingCacheKey is the one place a key is assembled correctly (tenant prefix, datasetVersion, bindingHash); binding-cache-key-coverage.py statically requires every resolveBinding call site to build its key through it rather than a hand-rolled string, closing the class where a new resolver compiles, caches, and looks correct in a single-tenant dev session while being a cross-tenant collision or a stale-shape read waiting to happen.

Consequences#

  • The majority of widget renders (explore.query) gain the same coalescing and TTL cache explore.dataset already had, with zero duplicated cache logic.
  • A dashboard's layout survives past save without a live repack on every request, and a compiler ALGORITHM change (not just a data change) can invalidate every stored plan in one deploy, with no migration.
  • Two static audits (D6/D8 above) make both halves of the cross-process wiring and the key-shape discipline permanent rules instead of reviewer memory.

Implementation status (declared 2026-09-03)#

Decision Status Evidence
D1 compiled_plan column triad Landed migration 0190_dashboard_compiled_plan.sql; packages/schema/src/tables/dashboards.ts
D2 compiler-version staleness check Landed COMPILED_PLAN_VERSION, apps/web/src/server/deck-compiled-plan.ts
D3 lazy rollout, no backfill Landed prod observed ~1/20 dashboards lazily repaired on first read, then stable
D4 drift guard Landed deck-compiled-plan.test.ts
D5 resolveBinding wrapper Landed apps/web/src/server/binding-cache.ts; routes explore.query + explore.dataset
D6 TTL + datasetVersion bump Landed bumpDatasetVersion; dataset-rollup + eCom rollup hooks (binding-cache-dataset-rollup-hook.ts, binding-cache-rollup-hook.ts)
D7 cross-process eventbus invalidation Landed binding-cache-eventbus-hook.ts; binding-cache-invalidation-publisher.ts (worker); packages/eventbus
D8 key-coverage audit Landed scripts/audit/binding-cache-key-coverage.py in audit:all