Overview
Persistence in buntime.sh means files, data, and state survive across multiple code executions within a session. Understanding how persistence works is crucial for building reliable applications and multi-step workflows.What Persists
✅ Files in /workspace
All files created in the/workspace directory persist:
✅ Installed Packages
Packages installed viabun add or auto-installed on import persist:
✅ Database Files
SQLite and other file-based databases persist:✅ Background Processes (While Active)
Processes started in previous executions keep running:What Doesn’t Persist
❌ In-Memory Variables
Variables only exist during a single execution:❌ Files Outside /workspace
Files created outside/workspace may not persist:
❌ Environment Variables
Environment variables don’t persist between executions:❌ Processes After Deep Sleep
Processes are terminated after 30 minutes of inactivity:Persistence Across Session States
Active State
Everything persists in memory:- Files immediately accessible
- Processes running
- No I/O delays
- Full speed
Idle State (5 min)
Container sleeps but everything remains:- Files still in memory
- Processes suspended
- Wake time: instant
- State preserved
Deep Sleep (30 min)
Filesystem saved to storage:- Files saved to R2
- Processes terminated
- Wake time: ~3 seconds
- File state preserved
After Wake
Files restored but processes restarted:- All files restored from R2
- Need to restart servers
- Database files intact
- Package installations preserved
Storage Limits
Each session has disk space limits:| Plan | Disk Space |
|---|---|
| Free | 1 GB |
| Paid | 5 GB |
| Enterprise | Custom |
Database Persistence Patterns
SQLite
Best for structured data:JSON Files
Best for simple state:Redis (In-Memory)
Best for caching and temporary data:File Organization Best Practices
Organize by Purpose
Use Subdirectories
State Management Patterns
Counter Example
Session Store Example
Backup and Recovery
Manual Backup
Download files before session expires:Automatic Snapshots
buntime.sh automatically snapshots your workspace:- On idle (5 min): In-memory snapshot
- On deep sleep (30 min): R2 snapshot
- On wake: Restore from snapshot
Common Patterns
Persistent Database
Persistent Database
Always use file-based databases in
/workspace:File Uploads
File Uploads
Save uploaded files to disk:
Caching
Caching
Use JSON files for simple caches:
Timestamps
Timestamps
Track when data was last modified:
Best Practices
Use /workspace
Always write to /workspace to ensure persistence
Close connections
Close database connections when done to free resources
Handle missing files
Use try-catch when reading files that might not exist
Clean up
Delete temporary files to stay within disk limits