Choosing a Data Persistence Strategy for Google Colab Notebooks
A decision guide for picking a data persistence strategy in Google Colab: compare ephemeral local disk, Google Drive mount, and Cloud Storage buckets with concrete implementation patterns and validation steps.
12 Sept 2026, 01:54 UTC

The Problem: Ephemeral Runtimes Break Reproducibility
Google Colab provisions a fresh VM for each session. The local filesystem at /content disappears when the runtime disconnects, idles out after roughly 90 minutes, or is preempted. Any dataset, model checkpoint, or processed artifact left there is lost unless copied elsewhere. This forces a decision every time you start a notebook: where do you read and write data so that work survives restarts and can be reused by collaborators?
Takeaway: Match the storage layer to your latency, durability, and sharing requirements. Use the local disk for scratch, Google Drive for personal persistence with zero extra cost, and a Cloud Storage bucket for team workflows and high-throughput pipelines.
Constraints That Shape the Choice
- Ephemeral VM storage cleared on disconnect or idle timeout (~90 min).
- Free-tier quotas limit GPU/TPU hours; long-running jobs may be cut short.
- Drive API rate limits throttle large files and frequent small I/O.
- Reproducible paths needed across runs and by different users.
- Credential exposure risk when secrets are pasted into notebook cells.
Option Comparison
| Strategy | Persistence | Latency | Setup Effort | Sharing Model | Typical Use Case |
|---|---|---|---|---|---|
Ephemeral /content | Lost on restart | Lowest (local NVMe/SSD) | None | Notebook owner only | Scratch space, temporary preprocessing, small experiments |
| Google Drive mount | Survives restarts | Higher (network + FUSE overhead) | OAuth consent each session | Drive sharing permissions | Personal datasets, model checkpoints, ad-hoc collaboration |
| Cloud Storage bucket | Survives restarts | High sustained throughput | Service account + Colab Secrets | IAM roles, signed URLs | Team pipelines, large datasets, CI/CD artifacts |
Trade-offs in Detail
Ephemeral /content — Speed Without Durability
The local disk is the fastest option because it avoids network hops and FUSE translation. It’s ideal for intermediate files generated during a single run — extracted archives, tokenized shards, augmentation caches. The catch: everything vanishes when the runtime ends. You must explicitly copy final artifacts to a durable location before the session closes.
Risk: An idle timeout or preemption can kill the runtime without warning, leaving no chance to run cleanup code.
Google Drive Mount — Familiar Persistence With Throttling
Mounting Drive via google.colab.drive.mount exposes your My Drive at /content/drive/MyDrive. No extra storage cost if you have Drive quota. However, the FUSE layer adds latency (often 10–50× slower for small random reads) and the Drive API enforces per-user rate limits. Large sequential writes can hit throttling, and the OAuth token expires, prompting re-authentication that breaks unattended runs.
Risk: Shared notebooks expose your Drive to anyone with edit access unless you restrict the mount point.
Cloud Storage Bucket — Scalable Throughput for Teams
A bucket accessed via a service account (stored in Colab Secrets) or signed URL gives consistent high throughput, fine-grained IAM, and no per-session auth prompts. It’s the only option that cleanly supports multi-user pipelines and CI/CD. The downside is credential management: you must create a service account, grant Storage Object Admin on the bucket, and store the JSON key securely. Egress charges apply if data leaves Google’s network.
Risk: A leaked service account key grants bucket access until rotated.
Concrete Implementation: Drive Mount With Fallback Copy
This pattern uses /content for active work and copies results to Drive at checkpoints. Run in a Colab notebook cell with internet access and a Google account that has Drive space.
import os, shutil, time
from google.colab import drive
# 1. Mount Drive (requires OAuth consent in browser popup)
drive.mount('/content/drive', force_remount=True)
project_root = '/content/drive/MyDrive/my-project'
os.makedirs(project_root, exist_ok=True)
# 2. Verify mount succeeded
print('Drive contents:', os.listdir('/content/drive')[:5])
# 3. Work in fast local scratch
scratch = '/content/scratch'
os.makedirs(scratch, exist_ok=True)
# Example: write a large temporary file
with open(os.path.join(scratch, 'intermediate.npy'), 'wb') as f:
f.write(os.urandom(100_000_000)) # 100 MB dummy data
# 4. Periodic checkpoint copy to Drive
def checkpoint(src_dir, dest_dir):
for name in os.listdir(src_dir):
src = os.path.join(src_dir, name)
dst = os.path.join(dest_dir, name)
if os.path.isfile(src):
shutil.copy2(src, dst)
print(f'Copied {name} -> {dest_dir}')
checkpoint(scratch, project_root)
# 5. Latency spot-check (run once to compare)
import timeit
local_time = timeit.timeit(lambda: open('/content/scratch/test.bin','wb').write(b'x'*1024*1024), number=5)
drive_time = timeit.timeit(lambda: open('/content/drive/MyDrive/my-project/test.bin','wb').write(b'x'*1024*1024), number=5)
print(f'Local 5x1MB writes: {local_time:.2f}s')
print(f'Drive 5x1MB writes: {drive_time:.2f}s')
Where to run: Any Colab notebook cell. Requires internet for Drive auth.
Permissions: Your Google account must have Drive storage quota. The notebook runs with your user credentials.
Expected checks: After drive.mount, os.listdir('/content/drive') should show MyDrive. The latency printout typically shows Drive writes 10–50× slower than local.
Risks: OAuth token expires after ~1 hour; long runs will pause for re-auth. Large copies may hit Drive API quotas (error 403 userRateLimitExceeded).
Concrete Implementation: Bucket Access via Service Account
Use this for team-shared datasets or model artifacts. Assumes you have a Google Cloud project, a bucket (e.g., gs://my-team-bucket), and a service account with Storage Object Admin on that bucket.
# 1. Store service account JSON in Colab Secrets (left sidebar 🔑)
# Name: GCS_SA_KEY Value: paste entire JSON
# 2. In notebook, write key to a temp file (only in memory)
import json, os, tempfile
from google.colab import userdata
sa_info = json.loads(userdata.get('GCS_SA_KEY'))
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json') as f:
json.dump(sa_info, f)
sa_path = f.name
# 3. Authenticate gsutil / gcloud
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = sa_path
!gcloud auth activate-service-account --key-file=$GOOGLE_APPLICATION_CREDENTIALS
# 4. Verify access
!gsutil ls gs://my-team-bucket/
# 5. Use in code (example with tensorflow/keras)
# model.save('gs://my-team-bucket/models/my-model')
# Or mount via gcsfuse for POSIX-like access (requires fuse install)
# !gcsfuse --implicit-dirs my-team-bucket /content/gcs
Where to run: Colab notebook with internet. Requires gcloud CLI (preinstalled).
Permissions: Service account needs roles/storage.objectAdmin on the bucket. The notebook runtime uses that identity.
Expected checks: gsutil ls returns object list without permission errors. gcsfuse mount shows bucket contents at /content/gcs.
Risks: Service account key resides in notebook memory; anyone with notebook view access can extract it. Rotate keys regularly. Egress charges apply for reads outside Google Cloud region.
Validation Checklist
- Runtime identity: Open Runtime → Change runtime type → note runtime ID and accelerator. Confirms you’re on the expected hardware.
- Mount verification: After
drive.mount, runos.listdir('/content/drive'). Should listMyDriveandSharedDrives. - Latency baseline: Time a 10 MB sequential write to
/content/test.binvs/content/drive/MyDrive/test.bin. Expect 0.01–0.05 s locally vs 0.5–2 s on Drive. - Persistence test: Write a file to each location, restart runtime (Runtime → Restart runtime), then check which files remain. Only Drive and bucket files survive.
- Bucket auth: Run
gsutil ls gs://your-bucketafter activating the service account. No 403 errors means IAM is correct.
Limitations and Gotchas
- Drive mount tokens expire; unattended training runs longer than ~1 hour will stall at the next re-auth prompt.
- Free-tier GPU/TPU availability is not guaranteed; design checkpoints assuming preemption.
- Colab Secrets are per-user per-notebook; they don’t sync across collaborators. Each user must add their own service account key.
gcsfusemount adds FUSE overhead similar to Drive; for maximum throughput use the client library (google-cloud-storage) directly.- No rollback needed for these read/write patterns — they don’t mutate shared infrastructure state beyond creating objects.
Decision Flow Summary
- Single user, small-to-medium data, interactive: Drive mount. Zero cost, familiar paths.
- Team, large datasets, automated pipelines: Cloud Storage bucket with service account in Colab Secrets.
- Throwaway preprocessing, one-off notebooks: Local
/contentonly; download results manually before close.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.