Snapshot Storage Backends
Relevant source files
The following files were used as context for generating this wiki page:
Snapshot Storage Backends persist shard snapshots to ensure durability and state recovery. The system utilizes a common Storage interface to support multiple storage providers, including NATS JetStream, AWS S3, and a No-Op implementation for testing. Snapshots represent a point-in-time capture of the ECS world state, including tick height, timestamps, and archetype data.
Sources: pkg/cardinal/snapshot/snapshot.go:37-55, proto/gen/go/worldengine/cardinal/v1/snapshot.pb.go:31-40
Architecture and Data Flow
The snapshot system follows a strict ownership model for memory management. When storing, the backend marshals the snapshot into an independent byte slice before network transmission to prevent mutating the caller's state. When loading, the backend returns a newly decoded message to the caller.
The diagram shows the flow of snapshot data from the ECS world through the serialization layer to specific storage providers.
Sources: pkg/cardinal/snapshot/snapshot.go:42-55, pkg/cardinal/snapshot/storage_jetstream.go:73-82, pkg/cardinal/snapshot/storage_s3.go:103-116
Storage Interface
All backends must implement the Storage interface, which defines two primary operations:
- Store: Saves the snapshot and atomically replaces any existing snapshot. It treats the input as read-only and performs a deep copy or marshals to bytes before returning.
- Load: Retrieves the current snapshot and returns a newly decoded message.
Sources: pkg/cardinal/snapshot/snapshot.go:37-55
Snapshot Versions
The system enforces versioning to ensure compatibility. The CurrentVersion is set to 1. The unmarshalSnapshot function validates the version at the decode boundary and rejects any unsupported versions.
Sources: pkg/cardinal/snapshot/snapshot.go:14-30
JetStream Storage Backend
JetStreamStorage utilizes the NATS JetStream ObjectStore to persist snapshots. It organizes data into buckets named using the service address components.
Configuration
| Option | Type | Description |
|---|---|---|
Address | *micro.ServiceAddress | The service identity used to generate the bucket name. |
SnapshotStorageMaxBytes | uint64 | Maximum size for the ObjectStore bucket. |
Sources: pkg/cardinal/snapshot/storage_jetstream.go:121-127
Implementation Details
- Bucket Naming: Generated as
{org}_{project}_{serviceId}_snapshot. - Storage Logic: Uses
PutBytesto overwrite the object namedsnapshot. - Retrieval Logic: Uses
os.Getandio.ReadAllto fetch bytes for unmarshaling.
Sources: pkg/cardinal/snapshot/storage_jetstream.go:47-60, pkg/cardinal/snapshot/storage_jetstream.go:73-82
S3 Storage Backend
S3Storage supports AWS S3 and S3-compatible services like MinIO or Cloudflare R2. It uses the AWS SDK for Go v2 and supports custom endpoints and path-style addressing.
Configuration
| Environment Variable | Description |
|---|---|
CARDINAL_S3_BUCKET | The name of the pre-provisioned S3 bucket. |
CARDINAL_S3_ENDPOINT | Custom URL for S3-compatible services (optional). |
AWS_REGION | The AWS region for the bucket. |
Sources: pkg/cardinal/snapshot/storage_s3.go:21-39, pkg/cardinal/snapshot/storage_s3.go:132-143
Data Organization
Snapshots are stored using a specific key prefix to prevent collisions in shared buckets:
{organization}/{project}/{serviceId}/snapshot
Sources: pkg/cardinal/snapshot/storage_s3.go:72-77
Data Structures
The snapshot data is defined using Protobuf messages to ensure cross-language compatibility.
The class diagram represents the hierarchical structure of the Cardinal snapshot Protobuf schema.
Sources: proto/gen/go/worldengine/cardinal/v1/snapshot.pb.go:31-155, proto/worldengine/cardinal/v1/snapshot.proto:13-63
Summary
Snapshot Storage Backends provide the persistence layer for the World Engine's shard state. By abstracting storage through the Storage interface, the system supports high-performance NATS JetStream for internal gaming rollup architecture and AWS S3 for cloud-native deployments. The system ensures technical accuracy through deterministic Protobuf marshaling and strict version validation.
Sources: pkg/cardinal/snapshot/snapshot.go:72-87, pkg/cardinal/snapshot/storage_s3.go:50-55