Modal logo

Persistence

Sandboxes are ephemeral by default, but can be made persistent or composed with Modal’s storage and persistence products.

Ephemeral sandboxes do not persist memory or filesystem state when they terminate. This ephemerality is acceptable for some sandbox applications, and allows for higher concurrency and lower cost. But many reinforcement learning rollout and agentic applications need persistence, and can adopt a number of solutions in the Modal platform.

ProductState persistedKey use cases
Persistent Sandboxesidentity, filesystem, configurationBackground agents
Filesystem snapshots (incl. exit and directory kinds)filesystemBackground agents, CI/CD
Memory snapshotsmemory, filesystem, configurationComputer use, cold start optimization, RL rollout preemption.
VolumesfilesystemCentralized agent data management
CloudBucketMountfilesystemData residency; Backup outside Modal.
Filesystem APIfilesystemLightweight file extraction at runtime

When to use Persistent Sandboxes 

Persistent Sandboxes provide an abstraction allowing users to interact with a Sandbox over long periods (≥1 day) and across many pause/resume boundaries. Persistent sandboxes are a natural fit for cloud agents, background agents, preview servers, and devboxes.

Ephemeral sandboxes work well for CI/CD, evals, RL rollouts, and Computer Use. For these applications consistent Sandbox identity and pause/resume is unnecessary, and the use cases benefit from the faster scheduling and higher concurrency of ephemeral sandboxes.

See Persistent Sandboxes for guidance on adopting and operating persistent sandboxes.

Sandboxes and filesystem snapshot persistence 

Filesystem snapshotting is the workhorse of Sandbox persistence. Modal allows users to snapshot a rootfs, a rootfs subdirectory, and sidecars.

Snapshotting can create save points within a Sandbox’s lifetime, and maintain a consistent, long-running agent session across multiple ephemeral sandboxes by snapshotting on sandbox exit.

Filesystem snapshots are incremental and support high concurrency on both creation and restore (see our Sandbox scale guide for numbers). A small diff, say from a source code edit, can snapshot in milliseconds and be forked across hundreds of new Sandboxes per second.

Filesystem snapshotting produces standard Images, so they are interoperable with Modal’s image building APIs.

For convenient access to the persistence of filesystem snapshotting, look at using Persistent Sandboxes. Background agents which pause and resume in milliseconds via the Persistent Sandbox API are using filesystem snapshots under the hood.

Sandboxes and memory snapshot persistence 

Sandbox memory snapshots save the full execution state of a Sandbox: running processes, memory contents, and generally everything loaded in kernel and userspace memory (note that open TCP connections are closed when a snapshot is taken).

A key use case of memory snapshot persistence is Sandbox time-to-ready restore time. When tackling computer use problems, Windows desktop application state is often expensive (>1 minute latency) to restore after boot.

Any Sandbox application which is difficult to resume quickly from disk state alone can benefit from memory snapshots.

Memory snapshotting also aids in RL rollout management. By snapshotting all Sandbox program state, rollout sessions can be resumed without command-log replay, and trajectories avoid the bias of program state loss.

See the Sandbox memory snapshots guide for details on usage.

Sandboxes and Volumes 

Volumes are a high-performance distributed file system for Modal applications. Sandboxes can have one or more Volumes attached, and writes to mounted Volumes persist beyond the Sandbox lifecycle.

A common pattern is to have a single Volume mounted into many Sandboxes, each writing into a unique subdirectory. Using subdirectory mounts, Sandbox reads and writes are isolated from each other, but the Volume remains a central store of data for analysis and observability. For example, each Sandbox could write logs and working documents into their subdirectory for later offline aggregation and processing.

Volumes can also be used as a shared, mutable store of common data which is attached read-only to all Sandboxes in an App. Directory snapshot mounts are another method of sharing common data, but as immutable snapshots they are less ergonomic to update.

Finally, Volumes are the suitable method of reading and mutating very large datasets (≥ 1 TiB).

Avoid application architectures which require one Volume per Sandbox or per agent session. Volumes are not designed to be created and deleted as frequently as Sandboxes.

Sandboxes and CloudBucketMount 

The modal.CloudBucketMount is a mutable volume that allows for both reading and writing files from a cloud bucket such as AWS S3.

CloudBucketMount allows customers to fulfill data residency requirements on Modal, but otherwise we recommend using Volumes with Sandboxes due to CloudBucketMount limitations and their lower performance compared with Volumes.

Persistent Sandboxes 

A Persistent Sandbox is a Sandbox that you can pause and resume. Pausing stops the Sandbox, saving its filesystem and maintaining the Sandbox’s identity and configuration.

A Persistent Sandbox is only deleted when it is terminated.

By default, Modal Sandboxes are ephemeral. To create a Persistent Sandbox, pass _experimental_persistent=True at creation as shown in the examples below.

Usage 

Create a Persistent Sandbox with the bool option enabled, then pause and resume it:

import modal

app = modal.App.lookup("persistent-sandbox-example", create_if_missing=True)

sb = modal.Sandbox.create(app=app, timeout=None, _experimental_persistent=True)
sb.exec("bash", "-c", "echo hello > /root/state.txt").wait()

sb._experimental_pause()
sb._experimental_resume()

p = sb.exec("cat", "/root/state.txt")
print(p.stdout.read())  # "hello\n"

sb.terminate()

Pausing ends the Sandbox’s current session, not the Sandbox itself, and terminate() stops a Persistent Sandbox for good. Once terminated, it can no longer be resumed.

Performance 

A paused persistent sandbox can be resumed in 100ms.

Once running, a persistent sandbox’s runtime performance is identical with that of an ephemeral sandbox.

Limitations 

Persistent Sandboxes don’t yet support these Sandbox features:

  • Custom timeouts. Setting a timeout or idle_timeout is rejected (in Python, pass timeout=None). By default, each session runs for at most 1 hour, or 5 minutes while idle, before the Sandbox is paused automatically; you can configure these up to 24 hours via experimental_options (persistent_sandbox_session_timeout and persistent_sandbox_session_idle_timeout). Operations that require a running session (such as exec()) raise SandboxPaused while paused and require an explicit resume (_experimental_resume() in Python, experimentalResume() in JavaScript).
  • Names and listing. Persistent Sandboxes can’t be named, and can’t be looked up with from_name or list (use from_id to reconnect by Sandbox ID).
  • GPUs. During alpha and beta, using GPUs with Persistent Sandboxes is not supported.
  • Memory snapshots. Only the filesystem is kept between pause and resume. Memory snapshot support is planned for Q1 2027.
  • Environment variables and tags. Use a Secret to pass environment variables instead.
  • Ephemeral credentials. Secrets must be stored server-side, so locally defined Secrets such as Secret.from_dict aren’t supported.
  • Updating a running Sandbox. Changing the network policy or outbound policy, renaming the Sandbox, and mounting or unmounting Images are not supported.
  • Sidecar containers don’t yet carry over to the session started on resume.

If any of these limitations is a blocker, please contact us on Slack or at support@modal.com.