Heuristic:1jehuang Jcode Size Aware Prune
| Knowledge Sources | |
|---|---|
| Domains | Telemetry, Optimization |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
Emergency D1 pruning is triggered by observed database size after each insert, with a cooldown to prevent prune storms.
Description
The Size_Aware_Prune heuristic monitors D1 database size after each insert using the meta.size_after field from D1 results. When the database grows past the soft limit (8GB, just above the high-water mark), an emergency prune is triggered. A 10-minute cooldown (EMERGENCY_PRUNE_COOLDOWN_MS) prevents prune storms. The soft limit sits at 8GB — well below D1's 10GB per-database hard cap, leaving 2GB of headroom for the emergency prune to catch up before inserts start failing. A separate full-error handler prunes immediately if an insert fails with a full/limit error.
Usage
Use this heuristic when designing telemetry storage with a hard size cap. Monitor size after each write, trigger emergency pruning at a soft limit below the hard cap, and apply a cooldown between prunes to avoid repeated pruning cycles.
The Insight (Rule of Thumb)
- Action: Check
meta.size_afterafter each D1 insert; triggerpruneOldEventsif size >=D1_SOFT_LIMIT_BYTES(8GB). - Value: Soft limit at 8GB (2GB below hard cap), 10-minute cooldown between emergency prunes.
- Trade-off: Pruning deletes old raw events (30-day retention for most types) but preserves identity/revenue anchors.
- Compatibility: Best-effort per-isolate state (resets on isolate recycle, which is fine).
Reasoning
D1's hard cap is 10GB; without a soft limit, inserts would start failing at the cap with no time to prune. The 8GB soft limit gives 2GB of headroom for the prune to complete. The firehose (Analytics Engine) write happens before the D1 insert, so telemetry keeps recording even if D1 hits its size cap. Identity/revenue anchor events (install, feedback, subscription_activated, account_linked) are never pruned.
Code Evidence
Soft limit and cooldown from telemetry-worker/src/worker.js:88-103:
// 1. Size growth: every D1 result carries `meta.size_after`. If the file
// grows past the soft limit (just above the high-water mark), the free
// pool is exhausted and real growth has resumed; run an emergency prune.
const D1_SOFT_LIMIT_BYTES = 8_000_000_000;
const EMERGENCY_PRUNE_COOLDOWN_MS = 10 * 60 * 1000;
Health endpoint exposing size vs. limit from telemetry-worker/src/worker.js:500-518:
// Monitoring endpoint: database size vs the soft limit, so cap pressure
// is observable before inserts start failing.
if (request.method === "GET" && url.pathname === "/v1/health") {
return jsonResponse({
ok: true,
db_size_bytes: lastObservedDbSizeBytes,
db_soft_limit_bytes: D1_SOFT_LIMIT_BYTES,
over_soft_limit: lastObservedDbSizeBytes >= D1_SOFT_LIMIT_BYTES,
last_emergency_prune_at_ms: lastEmergencyPruneAtMs || null,
}, 200, cors);
}
Retention configuration from telemetry-worker/src/worker.js:963-966:
const RETENTION_DAYS = {
turn_end: 30,
session_start: 30,
onboarding_step: 30,