Skip to main content

External Storage

View Markdown

The Temporal Service enforces a 2 MB per-payload limit by default. This limit is configurable on self-hosted deployments. When your Workflows or Activities handle data larger than the limit, you can offload payloads to external storage, such as Amazon S3, and pass a small reference token through the Event History instead. This page shows you how to set up External Storage with Amazon S3 and how to implement a custom storage driver.

For a conceptual overview of External Storage and its use cases, see External Storage.

Store and retrieve large payloads with Amazon S3​

Use the SDK-provided Temporalio::Contrib::Aws::S3Driver::S3StorageDriver. The AwsSdkS3StorageDriverClient adapter connects it to the AWS SDK for Ruby. The driver ships in temporalio. Add aws-sdk-s3 separately for the AWS client.

Prerequisites​

  • Ruby 3.3 or later and the temporalio gem version 1.12.0 or later.
  • The aws-sdk-s3 gem.
  • An S3 bucket that your Clients and Workers can access. Your Clients and Workers need s3:PutObject and s3:GetObject permissions on the objects, and s3:ListBucket permission on the bucket.
  • AWS credentials and a Region configured for the AWS SDK for Ruby, for example through an IAM role and AWS_REGION.

Add the dependencies to your Gemfile and run bundle install:

gem 'temporalio', '~> 1.12'
gem 'aws-sdk-s3'

Procedure​

Create the storage driver​

Require temporalio/contrib/aws/s3_driver and temporalio/contrib/aws/s3_driver/aws_sdk_s3_storage_driver_client. Create an Aws::S3::Client, wrap it in AwsSdkS3StorageDriverClient, and pass the adapter and your bucket name to S3StorageDriver.new. The AWS client loads credentials and Region settings through its default configuration chain.

ruby-sandbox/best-practices/data-handling/external-storage/configuration.rb

s3_client = Aws::S3::Client.new
driver = Temporalio::Contrib::Aws::S3Driver::S3StorageDriver.new(
client: Temporalio::Contrib::Aws::S3Driver::AwsSdkS3StorageDriverClient.new(s3_client),
bucket: bucket
)

The S3 driver uses content-addressed keys derived from the serialized Payload's SHA-256 hash and scoped to the execution context. It checks for an existing object before uploading and verifies the hash on retrieval. It rejects payloads larger than 50 MiB by default. Set max_payload_size: on S3StorageDriver.new to change that limit.

Payload retention

Keep stored payloads available for the entire Workflow lifetime, including retries, replay, and retention. The driver does not delete objects. See Lifecycle management when planning retention.

Configure the Client and Worker​

Create ExternalStorage with the driver in drivers: and pass it as external_storage: to DataConverter.new.

ruby-sandbox/best-practices/data-handling/external-storage/configuration.rb

data_converter = Temporalio::Converters::DataConverter.new(
external_storage: Temporalio::Converters::ExternalStorage.new(drivers: [driver])
)

Load connection settings with Temporalio::EnvConfig::ClientConfig.load_client_connect_options and pass the Data Converter to Temporalio::Client.connect as data_converter:.

ruby-sandbox/best-practices/data-handling/external-storage/run.rb

args, options = Temporalio::EnvConfig::ClientConfig.load_client_connect_options
args[0] ||= 'localhost:7233'
args[1] ||= 'default'
client = Temporalio::Client.connect(*args, **options, data_converter: data_converter)

Pass the Client to Temporalio::Worker.new. For a Worker that uses threads, set workflow_payload_codec_thread_pool to a Temporalio::Worker::ThreadPool so storage I/O can run outside Workflow Execution. Fiber-based Workers do not need this option.

ruby-sandbox/best-practices/data-handling/external-storage/run.rb

worker = Temporalio::Worker.new(
client: client,
task_queue: task_queue,
workflows: [LargePayloadWorkflow],
activities: [LargePayloadActivity],
workflow_payload_codec_thread_pool: Temporalio::Worker::ThreadPool.new
)

Configure compatible drivers on every Client and Worker process that can send or receive an offloaded payload. The Worker uses its Client's Data Converter. Drivers run outside Workflow Definitions.

By default, serialized Payloads that are 256 KiB or larger are offloaded. For other thresholds, see Configure payload size threshold.

Run the example​

The complete example starts a Worker and passes a 3 MiB string through Workflow input, Activity input and result, and Workflow result. With a local Temporal Service running, use S3:

cd ruby-sandbox/best-practices/data-handling/external-storage
bundle install
S3_BUCKET=my-temporal-payloads AWS_REGION=us-east-1 bundle exec ruby run.rb

The program checks the returned value and verifies that Event History contains four small references. It loads Temporal connection settings through environment configuration. See Environment configuration for connection settings.

Implement a custom storage driver​

1. Extend the StorageDriver class​

Subclass Temporalio::Converters::StorageDriver and implement:

  • name: a stable, nonempty name unique among registered drivers. The SDK records it in references to route retrieval.
  • type: a stable identifier for the implementation, shared across its configured instances.
  • store(context, payloads): stores each complete protobuf Payload and returns one StorageDriverClaim per payload, in the same order.
  • retrieve(context, claims): retrieves and returns the original Payloads in the same order.

2. Store payloads​

Serialize each Payload, including metadata, with payload.to_proto. The SDK applies any Payload Codec before calling the driver. Return StorageDriverClaim.new(claim_data: {...}) with enough information to retrieve the bytes. Do not put credentials or other secrets in claims: reference tokens are recorded in Event History without being encoded by the Payload Codec.

3. Retrieve payloads​

On retrieval, verify the stored bytes and decode with Temporalio::Api::Common::V1::Payload.decode. Raise an error for missing or corrupt data. Drivers must be thread safe because the SDK calls them concurrently. They can perform blocking I/O directly. Use threads rather than Fiber.schedule to parallelize a batch.

This driver uses SHA-256 keys, skips existing files, and publishes complete files without replacing another writer's file. It validates the hash before decoding retrieved bytes. Use it only for local development: every Client and Worker must share the same directory, and the example does not provide production storage durability.

ruby-sandbox/best-practices/data-handling/external-storage/local_disk_driver.rb

# For development only. All Clients and Workers must share this directory.
class LocalDiskDriver < Temporalio::Converters::StorageDriver
def initialize(directory)
@directory = File.expand_path(directory)
FileUtils.mkdir_p(@directory)
end

def name
'local-disk'
end

def type
'example.local-disk'
end

def store(_context, payloads)
payloads.map do |payload|
bytes = payload.to_proto
key = Digest::SHA256.hexdigest(bytes)
path = File.join(@directory, key)
unless File.exist?(path)
Tempfile.create('.payload-', @directory) do |file|
file.binmode
file.write(bytes)
file.close
begin
# Publish the complete file without replacing another writer's file.
File.link(file.path, path)
rescue Errno::EEXIST
# Another writer stored the same payload first.
end
end
end
Temporalio::Converters::StorageDriverClaim.new(claim_data: { 'key' => key })
end
end

def retrieve(_context, claims)
claims.map do |claim|
key = claim.claim_data.fetch('key')
raise 'Invalid payload key' unless /\A[0-9a-f]{64}\z/.match?(key)

bytes = File.binread(File.join(@directory, key))
raise 'Payload checksum mismatch' unless Digest::SHA256.hexdigest(bytes) == key

Temporalio::Api::Common::V1::Payload.decode(bytes)
end
end
end

4. Configure the Data Converter​

Pass a LocalDiskDriver instance in drivers: when creating ExternalStorage, then set it on the Data Converter and Client as shown in Configure the Client and Worker. Leave S3_BUCKET unset to run the complete example with this driver:

bundle exec ruby run.rb

Configure payload size threshold​

By default, the SDK offloads encoded Payloads that are 256 KiB or larger. The size includes Payload metadata and is measured after any Payload Codec has run. Smaller payloads stay inline in Event History. Set payload_size_threshold: on ExternalStorage.new to change the threshold, or use 0 to offload every payload.

ruby-sandbox/best-practices/data-handling/external-storage/configuration.rb

storage = Temporalio::Converters::ExternalStorage.new(
drivers: [driver],
payload_size_threshold: 256 * 1024
)

Pass this storage configuration to DataConverter.new as external_storage:.

Use multiple storage drivers​

Pass all drivers in drivers: and provide a driver_selector: proc to choose the driver for new payloads. The selector receives a context and Payload and returns a registered driver, or nil to keep the Payload inline. It is called only for Payloads that meet the size threshold.

During migration, select the preferred driver for new writes and keep the legacy driver registered for retrieval. Each driver must have a distinct, stable name. The S3 driver defaults to aws.s3driver. Set driver_name: when registering another S3 driver. Retain legacy drivers and their stores for as long as existing references can be read, including during replay.

ruby-sandbox/best-practices/data-handling/external-storage/configuration.rb

storage = Temporalio::Converters::ExternalStorage.new(
drivers: [preferred, legacy],
driver_selector: ->(_context, _payload) { preferred }
)

Multi-region durability with Amazon S3​

Make stored payloads available in each Region where your Clients and Workers can run. Workflow History replication alone does not replicate externally stored payloads. Plan S3 replication and recovery together with your Temporal Service failover strategy.

See Durable External Storage for the S3 Cross-Region Replication and Multi-Region Access Point pattern. Replication is asynchronous, so account for the delay before a new object is available in another Region.