Initializing Data Safely

Last updated 27 days ago

Provision the instance and deploy its spec before creating application data. Run data initialization as an explicit action against a selected environment, using that environment's write key. Repeating setup should not implicitly repeat application seed events.

Choose stable identities

The application owns fixture IDs, relationships and uniqueness rules. Give each seeded aggregate a stable ID that the script reuses when it resumes or runs again. Generating a new random ID on every run creates a new record on every run, even when fields such as email match an existing record.

Create-only concurrency control protects one aggregate ID. It does not enforce email uniqueness across different IDs. Existing duplicates require application-owned reconciliation and a choice of which identities to retain.

Create only if the stream is absent

Use the normal event-write API with metadata.previous_length: 0:

{
  "data": { "name": "Example" },
  "metadata": {
    "actor": {
      "type": "user",
      "id": "11111111-1111-4111-8111-111111111111"
    },
    "previous_length": 0
  }
}

The event and actor must match your deployed spec. Reuse the stable aggregate ID in the request URL. If that stream already exists, the create-only precondition returns 409. Read its state and decide whether it matches the intended fixture; ignoring every conflict can hide incompatible existing data. See Writing events for the request format.

Retry the same operation

Also send an operation-specific header, for example:

Idempotency-Key: seed-v1:customer:11111111-1111-4111-8111-111111111111:create

Use the same key and body when retrying that operation. Include enough identity to distinguish the seed version, aggregate and operation; keys are scoped to the entire instance/environment, rather than to one request URL.

A retry with a recognized key and the same body returns the original successful response, even when that create has already succeeded. Keys support retries for 24 hours. After expiry or when a replay response is unavailable, the stable ID and create-only precondition still prevent a second creation event. See Idempotency for the complete failure contract.

Resume partial initialization

Ordinary writes can trigger the business behavior defined by your spec. Several HTTP requests are not one transaction: a failed script can leave earlier records committed. The application must order dependencies, inspect existing records and resume incomplete work. Re-running the script does not make the entire script execute exactly once.

For a small initialization that must commit together, the existing atomic-write API can create records in one request, each with create-only preconditions. It is limited to ten targets and 100 total events and refuses event types with implications.

Bulk cold start is export replay

The instance-data POST /_cold_start endpoint loads an export without generating implied events or triggering live business actions. It is not an atomic create-if-empty operation or an exactly-once guarantee. Write failures can leave committed events, and HTTP 207 with ok: false means the load partially failed.

Use it only after establishing that the target is empty and other writers and loaders are stopped. For disposable non-production data, an explicit reset can prepare a fresh load. Ordinary create-only writes provide the per-record preconditions needed by resumable application seed scripts.

Can't find what you need? support@j17.app