Network & Performance Optimization
This document outlines the multi-tier caching architecture, binary serialization protocols, server-side data loaders, and offline resilience mechanisms in Scripture Habit.
1. Multi-Tier Optimization Architecture
To ensure instant application boot times and resilient network performance, optimizations are applied across the client, network, backend, and database layers:
Architecture Breakdown
Multi-Tier Client Caching
Instant UI rendering is powered by TanStack Query persisted tolocalStorage, eliminating loading spinners on return visits. Static assets (JS, CSS, fonts) are cached via Service Worker Cache Storage for offline boot, while study notes and chat histories are persisted in IndexedDB through the Firestore Client SDK.Network Transport Efficiency
API endpoints automatically negotiate binary serialization via HTTP headers (Accept: application/x-msgpack), reducing payload sizes by 30–50% compared to JSON. Pre-compressed Brotli (.br) and Gzip (.gz) static bundles minimize bandwidth consumption.Backend Load Reduction & Batching
External article metadata and high-frequency read models are cached in Redis for single-digit millisecond response times. For database operations, DataLoader aggregates concurrent document lookups into batcheddb.getAllcalls, preventing N+1 query overhead.
2. Multi-Tier Client Caching Strategy
| Layer | Storage | Retention | Purpose |
|---|---|---|---|
| Query State | localStorage | 24 hours | Instantly restores recent UI state on app reload without loading spinners. |
| API Cache | In-Memory (Axios) | 2 minutes | Deduplicates identical concurrent GET requests within short intervals. |
| Static Assets | Cache Storage (SW) | Per version | Pre-caches JS, CSS, and web fonts to enable immediate offline launch. |
| Firestore Data | IndexedDB | Managed | Supports offline note and chat access with multi-tab mutex coordination. |
3. Serialization & Payload Optimizations
Transparent MessagePack (
@msgpack/msgpack)
Reduces payload size by 30–50% relative to JSON. The client and server negotiate headers automatically to transfer binary payloads over HTTP.Build-Time Pre-Compression (Brotli & Gzip)
Pre-generates.brand.gzstatic assets during the build process, serving compressed files directly to reduce bandwidth usage.Self-Hosted Typography (
@fontsource)
Eliminates external Google Fonts CDN dependencies, preventing layout shift (FOUT) and removing additional TLS handshake latency.
4. Backend & Database Optimizations
Redis API Caching
Caches external URL metadata and frequently accessed resources in Redis for low-latency responses.DataLoader Batching
Consolidates concurrent Firestore document lookups within the same request lifecycle into a singledb.getAllcall, eliminating N+1 query overhead.HTTP Keep-Alive Pooling
Maintains persistent socket connections for external service communication, reducing TLS handshake overhead.
5. Offline Resilience & Traffic Control
Application-Level Scoped Offline Queuing
Pending messages and reflections are securely persisted in user- and group-scoped client queues (offline-chat-queue.ts). When network connectivity is restored, the application acquires a fresh, unexpired Firebase ID token before resending requests, preventing401 Unauthorizedtoken expiry failures common with raw Service Worker Background Sync (see ADR-003).Request Cancellation (
AbortController)
Aborts pending GET requests upon route transitions to conserve device resources and client bandwidth.Exponential Backoff Retries
Automatically retries intermittent network failures and 5xx errors up to 3 times with progressive backoff delays.