Architecture
Zero Cache follows a sync-first architecture:Components
- Replication Manager: Streams changes from PostgreSQL using logical replication
- View Syncers: Process client queries and maintain incremental views
- Change Streamer: Distributes database changes to view syncers
- SQLite Replica: Local copy of upstream data for fast query execution
Installation
Zero Cache is included in the@rocicorp/zero package:
Running Zero Cache
Development Mode
Usezero-cache-dev for local development with schema hot-reloading:
- Starts Zero Cache on port 4848 (default)
- Watches for schema changes and auto-restarts
- Uses lower resource limits suitable for development
- Defaults to
NODE_ENV=development
Production Mode
Run the cache server directly:Configuration
Configure Zero Cache using environment variables with theZERO_ prefix or CLI flags.
Required Configuration
Common Options
| Environment Variable | CLI Flag | Default | Description |
|---|---|---|---|
ZERO_PORT | --port | 4848 | Port for sync connections |
ZERO_APP_ID | --app-id | zero | Unique app identifier |
ZERO_REPLICA_FILE | --replica-file | zero.db | Path to SQLite replica |
ZERO_UPSTREAM_MAX_CONNS | --upstream-max-conns | 20 | Max connections to PostgreSQL |
ZERO_CVR_MAX_CONNS | --cvr-max-conns | 30 | Max CVR database connections |
ZERO_NUM_SYNC_WORKERS | --num-sync-workers | auto | Number of sync worker processes |
ZERO_ADMIN_PASSWORD | --admin-password | - | Password for admin endpoints |
Database Configuration
Performance Tuning
WebSocket Configuration
Multi-Node Deployment
For production, deploy with a replication manager and multiple view syncers:Replication Manager
Runs the PostgreSQL replication stream and change streamer:View Syncers
Connect to the replication manager:Litestream Integration
Zero Cache supports Litestream for SQLite replica backup and restore:App Isolation
Multiple Zero apps can share a PostgreSQL instance using theZERO_APP_ID:
{app-id}- App metadata{app-id}_0- Shard 0 metadata (client/mutation IDs){app-id}_0/cvr- Client view records{app-id}_0/cdc- Change data capture
Initial Sync
On first startup, Zero Cache performs an initial sync from PostgreSQL:Monitoring
Health Check Endpoint
Stats Endpoint
Requires admin password:OpenTelemetry
Zero Cache exports traces and metrics:Rate Limiting
Limit mutations per user:Development Tools
Schema Hot Reload
zero-cache-dev watches your schema file and auto-restarts:
Debug Mode
Run with Node debugger:Query Debugging
Error Handling
Auto Reset
Automatically reset replica on schema changes:Lazy Startup
Delay connection until first client request:Security
Admin Password
Required in production:NODE_ENV=development), admin endpoints work without a password.
Cookie Forwarding
Forward authentication cookies to mutation/query handlers:Custom Headers
Allow specific client headers:Telemetry
Anonymous usage telemetry is enabled by default:Troubleshooting
Port Already in Use
Connection Pool Exhaustion
Increase connection limits:Out of Memory
Reduce sync workers or back pressure limit:Slow Initial Sync
Increase table copy workers:Next Steps
- Setup Guide - Configure your server environment
- Mutations - Handle data writes
- Custom Queries - Transform client queries
- Deployment - Production deployment patterns