Skip to main content

Remote state on GCS or S3

drt normally keeps run state, execution history, and the dead letter queue (DLQ) under the project's local .drt/ directory. That default is convenient on a laptop, but the deployment model drt promotes—ephemeral CI runners and Cloud Run Jobs among them—destroys that disk after every run. The production field report recorded in ADR 0005 describes the consequences: drt status is blank in a fresh checkout, drt retry cannot see yesterday's failures, and a laptop and a runner have separate histories.

Set the project-level state.backend to gcs or s3 to put run state and the DLQ in object storage that survives the runner and can be shared by every operator using the project — for drt CLI invocations (run, build, test, validate, serve), the dagster-drt resource, and the Airflow/Prefect run_drt_sync() helper alike. All four now route state and watermark persistence through the same SyncObserver-based path this backend depends on.

That wasn't always true, and the two gaps broke differently — worth knowing if you're on a version before the fix landed. dagster-drt's resource constructed a local-only state manager directly, so a project configured with state.backend: gcs/s3 still wrote to local disk when run through Dagster (fixed in dagster-drt's Unreleased entry, see its CHANGELOG). The Airflow/Prefect runner's bug was the opposite and easier to miss: it did resolve the correct backend via build_state_bundle(), but never wired that store into an observer — run_sync() defaults to a silent no-op observer whenever one isn't passed — so nothing was ever persisted through it, local backend included, not just remote. If your Airflow or Prefect state history looks thinner than expected from before this fix, that's why; nothing needs migrating, saves just resume from the next run.

Execution history is a separate mechanism — run_sync()'s history_manager/history_retention_days parameters, appended directly inside the engine rather than through an observer. All three call paths (drt CLI invocations, the Airflow/Prefect runner, and dagster-drt's resource, fixed in #980) now wire it correctly; drt status/drt retry (state and DLQ) are unaffected, only the runs history a sync accumulates over time.

Quick start​

GCS:

# drt_project.yml
state:
backend: gcs
bucket: my-drt-state
prefix: production/customer-activation

S3:

# drt_project.yml
state:
backend: s3
bucket: my-drt-state
prefix: production/customer-activation
region: ap-northeast-1

Install the matching extra before running drt:

pip install 'drt-core[gcs]' # GCS
pip install 'drt-core[s3]' # S3

Configuration reference​

The state: block belongs in drt_project.yml, alongside history:. Remote fields default to null; fields that do not apply to the selected backend are rejected rather than silently ignored.

FieldBackendDefaultRequiredMeaning
backendalllocalnolocal, gcs, s3, or warehouse. Omitting the whole state: block preserves local behavior. This page covers gcs/s3; see Warehouse-backed state, history, and DLQ for warehouse and its connection_profile field.
bucketGCS, S3nullyes for GCS/S3Bucket containing the state objects. Rejected with backend: local.
prefixGCS, S3nullnoObject-key prefix. Leading and trailing / characters are normalized away; an empty value writes at the bucket root.
regionS3nullnoAWS region passed to the boto3 session; otherwise boto3 resolves its default region.
aws_profileS3nullnoNamed boto3 profile, normally from ~/.aws/credentials.
aws_access_key_id_envS3nullnoEnvironment-variable name or secret-provider URI resolving to the AWS access key ID.
aws_secret_access_key_envS3nullnoEnvironment-variable name or secret-provider URI resolving to the AWS secret access key.
aws_session_token_envS3nullnoEnvironment-variable name or secret-provider URI resolving to an optional AWS session token.
endpoint_urlS3nullnoAlternate S3-compatible endpoint, such as MinIO, LocalStack, R2, or Spaces. The endpoint must support conditional writes.

bucket is required for both remote backends. The six S3-only fields (region, aws_profile, the three *_env fields, and endpoint_url) are rejected under gcs; every remote field is rejected under local.

Google Cloud Storage​

GCS state uses Application Default Credentials (ADC). The client is created without an explicit credential argument, so use gcloud auth application-default login for local development and an attached service account in CI or production. No credential field belongs in the state: block.

The backend reads object metadata and content, conditionally uploads objects, and lists the history/DLQ prefixes. Install drt-core[gcs] and grant the runner's principal the bucket-scoped Storage Object User role (roles/storage.objectUser):

gcloud storage buckets add-iam-policy-binding gs://my-drt-state \
--member="serviceAccount:drt-runner@my-project.iam.gserviceaccount.com" \
--role="roles/storage.objectUser"

That built-in role includes the object create, read, update/delete, and list permissions needed to replace existing objects safely. A custom role can be narrower in unrelated permissions, but must still cover the equivalent object operations used by the backend.

Amazon S3​

S3 state deliberately uses the same credential vocabulary and precedence as the S3 destination. The three *_env fields are resolved through drt's resolve_env() chain; resolved values, aws_profile, and region are passed to boto3.session.Session. If none is set, boto3's standard credential chain applies (environment variables, shared credentials/config files, and attached workload roles). endpoint_url is passed when the S3 client is created.

Install drt-core[s3]. This minimal IAM policy permits the calls the backend makes under one prefix:

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListDrtStatePrefix",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::my-drt-state",
"Condition": {
"StringLike": {
"s3:prefix": [
"production/customer-activation",
"production/customer-activation/*"
]
}
}
},
{
"Sid": "ReadWriteDrtStateObjects",
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::my-drt-state/production/customer-activation/*"
}
]
}

HeadObject and GetObject require s3:GetObject; prefix enumeration uses s3:ListBucket; conditional object replacement uses s3:PutObject. If the bucket uses a customer-managed KMS key, its key policy may require additional KMS permissions.

Object layout​

The remote layout mirrors the local .drt/ layout exactly. For a normalized prefix production/customer-activation, drt uses:

production/customer-activation/state.json
production/customer-activation/history/<sync_name>.jsonl
production/customer-activation/dlq/<sync_name>.jsonl

In general, the keys are {prefix}/state.json, {prefix}/history/<sync_name>.jsonl, and {prefix}/dlq/<sync_name>.jsonl; without a prefix they are rooted at state.json, history/, and dlq/.

state.json is one project-wide object, containing every sync's latest run state. History and DLQ data use one JSONL object per sync. See #948 for the settled layout's rationale and trade-offs.

Remote history is pruned by both history.retention_days (default 30) and history.max_entries (default 500 per sync). The count cap applies only to remote backends and bounds the object downloaded and rewritten on each update; local history keeps every entry inside the retention window.

Concurrency and failure behavior​

Object stores do not provide POSIX append, so each update is a read-modify-write guarded by an object-version precondition. GCS pins reads to a generation and writes with if_generation_match; S3 pins reads and writes with the current ETag (If-Match), using If-None-Match: * to create an object. A stale precondition triggers a fresh read and retry with jittered backoff, up to eight attempts.

Exhausting those attempts while saving or resetting state.json raises drt.state.errors.StateContentionError from the store itself — this is what stops an exhausted retry loop from falling back to an unconditional, last-write-wins overwrite. History and DLQ appends remain best-effort: they log a warning and continue after contention retries or another write error are exhausted, matching the local stores' philosophy that telemetry persistence must not fail an otherwise-correct sync.

In a normal drt run, that StateContentionError does not reach you as a command failure. State persistence flows through StatePersistingObserver, and every observer callback in this codebase is fire-and-forget by design (AGENTS.md's "logging, state persistence, ... MUST flow through SyncObserver" boundary, and every observer's own docstring): the exception is caught, logged as a WARNING, and the CLI command still exits 0. This means a state.json write that lost every contention retry leaves the sync's watermark silently stale, with the command reporting success. Watch your logs (or a log aggregator) for State persist failure if you're relying on --threads N against a shared remote state.json under real write pressure — a clean exit code is not, by itself, proof the watermark actually advanced.

This whole-object update shape is a deliberate, measured trade-off. The design rationale and reversibility constraint live in ADR 0005 — Where drt's state lives, and what it costs the operator, not in this guide. In particular, its falsification condition calls out write amplification and contention at roughly 50 syncs on a 15-minute cadence as the threshold to measure rather than assume away.

Migrating between local and remote state​

Because remote keys mirror .drt/, migration is a byte-for-byte directory sync with no schema conversion — but only for the three paths the remote backend actually reads and writes: state.json, history/, and dlq/. .drt/ also holds files the remote backend has nothing to do with, most importantly .drt/secrets.toml — a local credential store (see Secret Provider URIs) — plus .drt/schemas/ (--emit-schema) and .drt/test_failures/ (drt test --store-failures). Do not sync the whole .drt/ directory — a recursive .drt/ copy uploads secrets.toml to the state bucket right along with everything else. Sync each of the three paths explicitly instead:

Adopt GCS, or return from GCS to local:

gsutil cp .drt/state.json gs://my-drt-state/production/customer-activation/state.json
gsutil rsync -r .drt/history/ gs://my-drt-state/production/customer-activation/history/
gsutil rsync -r .drt/dlq/ gs://my-drt-state/production/customer-activation/dlq/
# Reverse each command (swap source and destination) to return to local.

Adopt S3, or return from S3 to local:

aws s3 cp .drt/state.json s3://my-drt-state/production/customer-activation/state.json
aws s3 sync .drt/history/ s3://my-drt-state/production/customer-activation/history/
aws s3 sync .drt/dlq/ s3://my-drt-state/production/customer-activation/dlq/
# Reverse each command (swap source and destination) to return to local.

Stop drt writers while copying so a concurrent run cannot change an object mid-migration. After uploading, set the matching remote state.backend. To return, download first if you want the accumulated remote state, history, and DLQ locally, then set backend: local or remove the state: block. Dropping back requires no code change and no data transformation. This is the designed-in reversibility constraint in ADR 0005, Decision 4, not an incidental property of the implementation.

The sync commands do not delete objects that exist only on the destination, so they are safe for normal adoption/return migrations. If you need a strict mirror, review and remove stale objects separately.

Project state and sync watermarks are independent​

state.backend is project-level storage for run state, history, and the DLQ. sync.watermark.storage is a per-sync choice for an incremental cursor. They are different scopes, and setting either one does not relocate the other.

This is valid: shared project state on GCS with one sync's watermark still on the runner's local disk.

# drt_project.yml
state:
backend: gcs
bucket: my-drt-state
prefix: production/customer-activation

# syncs/users.yml
sync:
mode: incremental
cursor_field: updated_at
watermark:
storage: local

The reverse is valid too: a project can keep the default local state backend while an individual sync uses watermark.storage: gcs (or bigquery). Choose and migrate each concern explicitly.

What this isn't​

  • Not a warehouse-queryable observability layer. GCS/S3 make operational files durable and shared; they do not turn history into SQL tables. That separate tier is state.backend: warehouse (#920, split from #756 by ADR 0005) — see Warehouse-backed state, history, and DLQ.
  • Not object encryption managed by drt. Use the bucket provider's server-side encryption, key-management, and IAM controls. drt does not encrypt the payload before upload.
  • Not automatic migration. Changing state.backend does not copy existing .drt/ data. Run the gsutil rsync or aws s3 sync command yourself.

See also​