System State & Health Architecture
SveltyCMS state machine, phased initialization, self-healing watchdog, and request gating middleware with hardware-aware rate limiting.
On this page
SveltyCMS employs a Svelte 5 Runes-based state machine to manage the application lifecycle, enforce security boundaries during bootstrap, and recover autonomously from failures.
1. System States
| State | Description | Requests |
|---|---|---|
IDLE |
Waiting for setup or initialization | Blocked (except Setup Wizard + Health) |
INITIALIZING |
Database + core services starting | Blocked (requests wait for completion or timeout at 60s) |
WARMING |
Cache pre-warming, widget reconciliation in flight | Blocked (SSR warming page shown) |
WARMED |
Background services fully ready | Allowed (full dashboard available) |
SETUP |
Setup wizard active, no DB configured yet | Blocked (except Setup Wizard + Health) |
RECOVERY |
Autonomous healing after service failure | Blocked (503 + Retry-After) |
READY |
Normal operation | Allowed |
DEGRADED |
Operational, non-critical services failing | Allowed (warning logged, degraded services injected into locals) |
MAINTENANCE |
Scheduled maintenance window | Blocked (503 + maintenance page) |
FAILED |
Critical failure (DB down) | Blocked (503) |
2. Reactive State with Svelte 5 Runes
The system state is managed by SystemStateContainer (src/stores/system/state.svelte.ts) using $state and $derived runes:
- Zero-Tax Access: Direct
system.statereads — noget()helper overhead - Fine-Grained Reactivity: Components re-render only when the specific service they watch changes
- Derived Intelligence:
overallStateis a$derivedrune — zero computation when idle - Hot-Path Cache:
isSystemReady()uses a module-level cache invalidated only on state transitions — zero-overhead forhandleSystemStateand all downstream hooks - Single Source: Consolidated in
state.svelte.ts— no dual-state file duplication
3. Phased Initialization
Instead of a monolithic boot, the system targets “Minimum Viable Services” based on request intent:
| Phase | Services | Result | Required For |
|---|---|---|---|
| SETUP | Database connectivity | State → SETUP |
Setup Wizard |
| CORE | Auth, Security, Critical Settings | State → READY |
Login screen |
| FULL | Cache Pre-Warming, Widget Reconciliation, Background Jobs, Behavioral Engine | State → WARMED |
Dashboard |
Thundering Herd Prevention: Only the first request triggers initialization. Subsequent requests wait on the shared
dbInitPromise— preventing connection storms.
Zero settling delay:plugin-registry.tsanddb-init.tshave nosetTimeoutbetween CORE and WARMED. Proxy registrations are synchronous; ready plugins boot in parallel as soon as dependencies are met.
Logical vs. Technical: Lazy Holders enforce physical code isolation, preventing server-only modules from leaking into client bundles.
4. Request Gating Middleware
handleSystemState (src/hooks/handle-system-state.ts) intercepts every request:
- Fast-Path Resolution: Static assets, health checks,
.well-known/paths bypass immediately - Bootstrap Host Validation: During restricted states, only authorized hosts (localhost,
ORIGIN, or Demo) can access bootstrap routes — prevents SSRF and DNS rebinding during setup - Setup Completion Gating: After setup completes, all
/api/setupendpoints return 403 — enforced at both middleware and handler levels - Initialization Wait: Non-bootstrap requests pause while
INITIALIZING(or timeout at 60s →FAILED) - State-Specific Blocking: Returns 503 with state-specific messaging for
SETUP,FAILED, andRECOVERYstates - Degradation Injection: If
DEGRADED, injectslocals.degradedServicesfor UI awareness
5. Self-Healing Watchdog
The Autonomous System Watchdog (src/services/system/watchdog.ts):
| Feature | Detail |
|---|---|
| Heal-on-Failure | Automatically triggers targeted re-initialization on critical service failure |
| Exponential Backoff | Recovery attempts use 5s → 10s → 20s progressive delays |
| Drift Detection | Continuously compares internal health map against reality — demotes to RECOVERY if DB is dead while state claims READY |
| Timeout Safety | 60-second hard limit on initialization — enters FAILED to protect infrastructure |
6. Route Resource State Machine & Cache Synergy
The Route Resource State Machine (src/services/core/route-resource-state-machine.ts) classifies every URL into precise operational resource profiles in < 10 microseconds, orchestrating with CacheService (src/databases/cache/cache-service.ts) for sub-millisecond route latency:
| Resource Lane | Routes | Required Cache Categories | Memory Profile | Middleware Bypass |
|---|---|---|---|---|
bootstrap |
/login, /setup, /auth/* |
SYSTEM, AUTH |
< 2MB RAM |
Skips media, preferences, scim, collaboration |
media |
/mediagallery, /media/* |
MEDIA, SYSTEM |
Focused Media RAM | Skips scim, collaboration |
collection |
/collections/*, /content/* |
SCHEMA, AUTH, SYSTEM, CONTENT |
Schema & Field RAM | Skips scim |
graphql |
/api/graphql |
SCHEMA, AUTH, SYSTEM, CONTENT |
JIT Executor RAM | Skips media, preferences, scim, collaboration |
api |
/api/* (REST endpoints) |
SCHEMA, AUTH, SYSTEM |
Streaming JSON RAM | Skips media, preferences, scim, collaboration |
dashboard |
/dashboard, /admin |
SYSTEM, SETTINGS |
Layout Metrics RAM | Skips scim |
settings |
/settings, /config |
SYSTEM, AUTH |
Config RAM | Skips media, collaboration |
Cache Service Integration
- Adaptive TTLs: Configured per category in
CATEGORY_TTL_SECONDS(schema: 3600s,setting: 1800s,content: 120s,api: 30s). - Targeted Tag Invalidation: When database mutations occur,
cacheService.invalidateTag(collection)purges only the modified collection’s cache entries without affecting other route lanes. - Negative Bloom Cache: Bypasses dead DB lookups for non-existent records in
< 1µs.
7. SvelteKit Remote Functions & Offline Resilience Engine
The Offline Draft Sync Engine (src/utils/offline-draft-sync.ts):
- SvelteKit Remote Functions & Progressive Enhancement: Wraps Remote Function calls (
wrapRemoteFunction) with optimistic Svelte 5$stateupdates and graceful fallback on cellular drops. - Persistent Local Storage Drafts: Automatically saves in-progress form inputs via
saveLocalDraft(collection, entryId, data, tenantId)so edits survive page reloads or device restarts. - Replay Handler Registry & Background Sync: Registers RPC handlers (
registerHandler) and automatically replays pending mutations upon reconnection (window.addEventListener("online")) in FIFO order with exponential retry management (MAX_SYNC_RETRIES = 3). - Draft Cleanup & Conflict Protection: Clears local storage drafts (
clearLocalDraft) only upon verified database transaction commit, preventing data loss on flaky edge connections.
8. Hardware-Aware Rate Limiting
The SystemMonitor utility (src/utils/system-monitor.ts) adjusts request costs based on real-time pressure:
| Condition | Multiplier | Effect |
|---|---|---|
| Event Loop Lag > 80ms | 2.0x | Requests cost double — throttles traffic |
| High CPU Load | 1.5x | Gradual throttling |
| Idle / Low Load | 0.8x | Capacity boost — higher throughput |
| Heap > 90% | Mutations rejected | 503 with compressed payload |
9. Boot Telemetry
Every phase is measured with performance.now() precision, exposed via health API and MetricsService:
| Metric | Description |
|---|---|
boot:phase:setup |
Database handshake latency |
boot:phase:core |
Auth/settings hydration latency |
boot:phase:full |
Content/widget reconciliation latency |
recoveryCount |
Successful self-healing events since start |
initDuration |
Total time to READY state |
securityViolations |
Untrusted host attempts, restricted route access |
10. Client State Architecture: $app/state vs. Rune Stores
To maximize resource efficiency and eliminate memory duplication, SveltyCMS separates state into two distinct tiers:
1. The Zero-Duplication Principle (page.data)
Server-loaded data (settings, user, theme, systemLanguage, contentLanguage, contentNodes, collectionSchema, entries, pagination) is delivered directly to components via $app/state (import { page } from '$app/state'):
- Zero Memory Duplication: Data is NOT mirrored into global client stores. Components read
page.data.<key>directly. - Zero Sync Effects: Eliminates
$effect.preand$effectloops that previously synchronized server loads into store singletons. - SSR-Hydration Consistency: Ensures the rendered DOM matches the server payload byte-for-byte with zero hydration mismatch.
2. High-Performance Rune Stores (src/stores/)
Client stores are strictly reserved for transient, device-local, or cross-component UI state that does not originate from server loaders:
| Store | Pattern | Purpose |
|---|---|---|
ui-store.svelte.ts |
$state class |
Panels, sidebars, route context, global search, editor-wizard transient state |
screen-size-store.svelte.ts |
$state class |
Responsive breakpoints (screen, ScreenSize, BREAKPOINTS) |
theme-store.svelte.ts |
$state class |
Dark/light/system persistence, OS preference listener, optimistic userThemePrefs |
toast.svelte.ts |
$state class |
Toast queue with responsive positioning and promise toasts |
loading-store.svelte.ts |
$state class |
Global loading priorities, progress, and cancellation |
active-input-store.svelte.ts |
$state class |
Focus coordinates and target input for token insertion |
collection-store.svelte.ts |
$state.raw / $state |
Form input buffer (activeValue) and local mode transitions (view/edit/create) |
3. Resource-Saving Guidelines
- Use
$state.rawfor large reference trees: Read-only data trees (such as complete schemas, public config, or static content hierarchies) use$state.rawinstead of deep$stateproxies. Deep$statewraps every nested property in a JavaScript Proxy; using$state.rawwith immutable reference updates (this.state = { ...this.state, [key]: value }) reduces V8 heap allocation by ~60% on complex schema sets and completely eliminates proxy trap dispatch latency. - Never create legacy
svelte/storebridges: Do not export.subscribe(),writable(), orderived()wrappers. Fine-grained Svelte 5 runes ($state,$derived,$props.id()) provide immediate direct access with zero event emitter overhead. - Use Native Reactive Collections: When tracking sets of items (such as active token IDs, translated field keys, or concurrent operation queues), use
SvelteSetandSvelteMapfromsvelte/reactivityinstead of plain arrays or generic objects. This provides $O(1)$ lookups and fine-grained change notifications without re-creating arrays. - Server-to-Client Zero-Copy Pipeline: Data loaded by server hooks and loaders (
+layout.server.ts/+page.server.ts) is delivered directly into SvelteKit’s serialized payload. Client components read directly from$app/state(page.data), completely bypassing redundant client store singletons. - Direct-to-Wire Serialization: Bypasses intermediate object hydration across all 4 database engines (
sqlite,postgresql,mariadb,mongodb), formatting JSON directly in the database driver or wire stream, saving GC cycles and reducing tail latencies (p95 < 0.5ms). - Keep single-consumer stores colocated: Feature-specific stores live alongside their UI component rather than in
src/stores/(e.g.plugins/cookie-consent/consent-store.svelte.ts,components/image-editor/image-editor-store.svelte.ts).
11. Incremental Service Refresh
reinitializeSystem performs surgical updates without destructive resets:
- Config Hot-Reload: Re-parses
private.tswithout dropping active DB connections - Dynamic Settings Sync: Automatically re-synchronizes in-memory cache with database
- Zero-Downtime Transitions: Minimizes the blocked-request window during updates
Related
- Server Hooks
- Database Core
- API Security & Token Hardening
- Login Resilience & Recovery
- Behavioral Learning Engine — Starts/stops with server lifecycle during
FULLphase - Utility System Architecture — Complete utility reference: structure, patterns, import guide
- Store System Architecture — Complete store reference: patterns, purpose, page.data vs stores
- Sidebar States & UI Visibility — How panels, sidebars, and layout visibility are managed