KV Cache Snapshotting
Relevant source files
The following files were used as context for generating this wiki page:
KV Cache Snapshotting is a core architectural pattern in StatusBeam designed to provide high-performance, edge-native status page rendering. The system decouples heavy time-series data storage (managed in Cloudflare D1) from the read-heavy display layer by aggregating current status information into lightweight JSON snapshots stored in Cloudflare KV.
This approach ensures that the status page remains resilient and fast, even during outages of the monitored services. By reading from a pre-aggregated KV snapshot, the system avoids expensive database joins on every visitor request and prevents client-side rate limits commonly found in status page generators that rely on external APIs.
Sources: ARCHITECTURE.md:14-25, README.md:14-25
Architecture and Data Flow
The snapshotting process operates as a bridge between the Check Layer and the Display Layer. The Check Worker probes services and writes raw results to D1, then executes a writeSummary() function to aggregate this history into a SiteSummary[] snapshot. This snapshot is then persisted to Cloudflare KV.
System Interaction Diagram
The following diagram illustrates the lifecycle of a status check from probe to snapshot storage and subsequent retrieval by the web application.
Sources: ARCHITECTURE.md:86-103, README.md:46-75
Snapshot Storage Structure
Snapshots are stored in Cloudflare KV using specific keys. The system uses a dedicated KV namespace bound to the variable STATUS_KV. This namespace holds the configuration, the site summary (snapshot), and the incident timeline.
Key-Value Store Schema
| Key | Format | Description |
|---|---|---|
config | YAML | The status.config.yml document containing site definitions and theme settings. |
summary | JSON | A serialized SiteSummary[] containing 90-day uptime, response time, and current status. |
incidents | JSON | A serialized Incident[] containing the history of Investigating to Resolved updates. |
Sources: apps/worker/src/env.ts:59-67, apps/web/src/lib/data.ts:46-56
Read Path and Resilience
The web application (Display Layer) utilizes a specific read path to retrieve snapshots. The function getSummary() is the primary entry point for fetching the dashboard state.
Fallback Mechanism
To support local development and ensure the page remains functional even if Cloudflare bindings are missing (e.g., during astro dev), the system implements a deterministic sample data fallback.
Snapshot Aggregation and Integrity
The snapshot generation (Check Layer) is responsible for converting complex D1 time-series data into the flattened SiteSummary format. This includes:
- Uptime Calculation: Aggregating D1 history (GROUP BY slug/day) to produce 90-day uptime bars.
- Status Derivation: Determining the current state (Up, Degraded, Down) based on status codes and
maxResponseTime. - Response History: Collecting the last 48 response time samples (every 30 minutes) for sparkline charts.
If the KV config key is missing or the YAML is malformed, the system is designed to degrade gracefully. For example, the locale will fall back to English and the page name will fall back to "statusbeam" to avoid breaking the edge render.
Sources: ARCHITECTURE.md:92-97, apps/web/src/lib/data.ts:81-115
Management and Provisioning
Snapshots are managed via the StatusBeam CLI and setup scripts. The statusbeam setup command handles the idempotent provisioning of the KV namespace.
Provisioning Logic
- Lookup: The CLI checks if a KV namespace ending in
STATUS_KValready exists. - Creation: If absent, it creates a new namespace using
wrangler kv namespace create. - Binding: The resulting KV ID is written to
wrangler.jsonc. - Initial Upload: The
status.config.ymlis uploaded to theconfigkey to bootstrap the system.
Sources: scripts/setup.sh:109-122, packages/cli/src/lib/wrangler.ts:63-79, DEPLOYMENT.md:14-25
Conclusion
KV Cache Snapshotting serves as the performance engine for StatusBeam. By transforming persistent D1 records into optimized KV snapshots, the system achieves sub-second response times at the edge while maintaining total decoupling from the monitoring infrastructure. This architecture ensures that the status page remains a "static-first" asset that is resilient to database latency and high visitor traffic.