Skip to content

Scalable caching pattern using Cloudflare Durable Objects for high-traffic applications

Notifications You must be signed in to change notification settings

acoyfellow/cache-sync

Repository files navigation

Durable Cache Sync Pattern

A production-ready pattern for high-traffic applications needing low-latency reads with eventual consistency. Perfect for:

  • Session management
  • User profile caching
  • Real-time collaboration features
  • High-velocity write buffers

Why This Pattern?

Traditional Database Challenges With Cache Sync Pattern
📉 90%+ of queries hit primary DB 📈 90%+ cache hit rate
💸 Expensive direct DB writes 🐢 Batched async writes save costs
🌍 Global latency inconsistencies 🚀 Edge caching <5ms responses
🔄 Complex cache invalidation 🔄 Auto-sync with writeback queue

Architecture

Sync/Caching Pattern

Key Components:

  • Durable Object Cache: Per-key cache with in-memory access
  • Write Buffer: Batched updates every 60s
  • Auto Refresh: Stale-while-revalidate pattern
  • Pass-through Worker: Simple edge routing

Napkin Math Cost Comparison (1M Requests/Month)

Resource Traditional DB Cache Sync Pattern
Database Reads $500-$2000 $15-$50
Database Writes $300-$1500 $5-$20
Cache Layer $0 $10-$30
Total $800-$3500 $30-$100

Getting Started

  1. Clone and install:
git clone https://github.com/acoyfellow/cache-sync.git
cd cache-sync
bun install
  1. Configure environment (wrangler.json):
{
  "name": "cache-sync-pattern",
  "main": "src/worker.ts",
  "compatibility_date": "2024-03-01",
  "durable_objects": {
    "bindings": [
      {
        "name": "userprofile",
        "class_name": "UserProfileCache"
      }
    ]
  },
  "vars": {
    "MAIN_DB_API": "https://your-db-api.com"
  }
}
  1. Local development:
bun run dev
# Interact with App.svelte at http://localhost:5173
  1. Deploy to Cloudflare:
bunx wrangler deploy

Deploy to Cloudflare Workers

How It Works

  1. Read Path:
Client → Worker → Dedicated User DO Instance (Isolated per session)
                ↳ (Cache Miss) → Create New DO Instance → Return Stub
  1. Write Path:
Client → Dedicated User DO Instance → In-Memory State + Storage
                               ↳ Alarm → Batch Sync to Main DB
  1. Client Sync:
Offline → Local Storage → Auto Retry

Features:

  • Network detection
  • Local persistence
  • Auto-retry on reconnect
  • Accessible status

This implements a robust offline-first pattern where:

  • All user interactions remain available offline
  • Changes queue locally until connectivity resumes
  • UI clearly indicates sync status
  • Data automatically reconciles when back online

Implementation Phases:

  1. Shadow Mode: Run cache layer parallel to main API, compare results

  2. Read-Only Canary: Route 5% of read traffic through cache

  3. Write Queueing: Enable write buffering for canary users

  4. Full Cutover: Gradually increase traffic over 1-2 weeks

This maintains security while allowing gradual rollout. The key is keeping the cache layer as a transparent proxy that enforces existing auth rules.

Future Concerns

  • Data Compression - Store more data in Durable Object state
  • TTL Expiration - Automatically expire stale data
  • Rate Limiting - Prevent API abuse
  • Auth Integration - Add request validation
  • Metrics Dashboard - Track cache performance

License

MIT License - Free for commercial and personal use.

About

Scalable caching pattern using Cloudflare Durable Objects for high-traffic applications

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published