Skip to main content

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 via bun 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:
Workaround: Write to files:

❌ Files Outside /workspace

Files created outside /workspace may not persist:
Always use /workspace:

❌ Environment Variables

Environment variables don’t persist between executions:
Workaround: Pass env vars in each request or write to a file.

❌ 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: Monitor disk usage:

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

Always use file-based databases in /workspace:
Save uploaded files to disk:
Use JSON files for simple caches:
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

Sessions

Understanding session lifecycle

Resource Limits

Disk space and memory limits

Files API

Managing files via API

Databases Guide

Working with databases