Module 11 · Lesson 9 of 15Detailed study · about 25 min

Pipeline contract: immutable events, versions and tombstones

Reliable incremental processing needs an ordering contract. Our pipeline stores immutable events and maintains one latest state per order, including deletion markers.

Understand the idea

A stream tells you which event rows arrived; it does not define which version is newest in business time. The producer supplies a monotonically increasing version per order. Event timestamps alone are not enough when clocks tie or corrections arrive late.

Key ideas in simple English
ConceptWhat it means
ORDER_IDBusiness key of the order.
EVENT_VERSIONStrictly increasing version for that order.
EVENT_IDStable unique identity of the event; repeated deliveries keep the same ID.
TombstoneA retained deleted state and version that prevents an older event resurrecting a row.

A concrete example

The source and both streams contain four inserted event rows. Business result should become order 101 version 2 amount 120 and order 102 version 1 amount 50. Duplicate event 1 must not multiply target rows.

Remember: Idempotent state updates require stable keys, versions and a deletion strategy—not only MERGE syntax.

Explain it step by step · full theory

Understand the concept

A stream tells you which event rows arrived; it does not define which version is newest in business time. The producer supplies a monotonically increasing version per order. Event timestamps alone are not enough when clocks tie or corrections arrive late.

UPSERT means set the current amount; DELETE means mark the order deleted. We retain the version even for deleted orders. Physically deleting the target row would discard that version and make an old replay look like a brand-new order.

Walk through the behavior

Repeated identical events are allowed for the lab. Conflicting records with the same order/version are not resolved by guesswork: our validation rejects them. This explicit contract is narrower than claiming universal exactly-once delivery.

The append-only stream fits immutable rows. Do not UPDATE or DELETE the event log as a way to correct business data; append a new version instead.

Words used in this lesson
ORDER_ID
Business key of the order.
EVENT_VERSION
Strictly increasing version for that order.
EVENT_ID
Stable unique identity of the event; repeated deliveries keep the same ID.
Tombstone
A retained deleted state and version that prevents an older event resurrecting a row.

See how it works

Choose a step to explore the idea at your own pace.

Open the interactive explanation
Interactive explainer1 / 3

Pipeline contract: immutable events, versions and tombstones

Source data
Change boundary

Start with the source

Reliable incremental processing needs an ordering contract. Our pipeline stores immutable events and maintains one latest state per order, including deletion markers.

Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.

Read the complete diagram explanation
  1. Start with the source

    Reliable incremental processing needs an ordering contract. Our pipeline stores immutable events and maintains one latest state per order, including deletion markers.

  2. Follow the work

    A stream tells you which event rows arrived; it does not define which version is newest in business time. The producer supplies a monotonically increasing version per order. Event timestamps alone are not enough when clocks tie or corrections arrive late.

  3. Explain the outcome

    Idempotent state updates require stable keys, versions and a deletion strategy—not only MERGE syntax.

Try the SQL or command

Read → predict → run in your learning environment → compare. You can also learn by reading without an account.

New to SQL? Where to run it and how to read it

A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.

  1. No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
  2. For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
  3. Read the entire example, including comments beginning with --. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor.
  4. Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
SELECT / FROM
Choose the values to return / identify their input.
VALUES / COLUMN1
VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
AS / WITH
AS gives a result a readable name. WITH names a temporary query step within one statement.
NULL / ::
NULL means missing or unknown. The :: notation converts a value to a named data type.
Session
One connection and its settings. Keep related transaction steps in the same session.
Database.schema.table
A full address for stored data. It is separate from the warehouse that processes queries.

Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →

Help with common practice errors
  • No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
  • Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
  • Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
  • Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.

Before running this lesson

Manual learning lab; nothing runs on this website. Complete lesson 1 setup in L2_OBJECTS_LAB.M11_LAB and follow the lesson order. Use the same authorized role and warehouse for manual SQL. Task templates use <lab_warehouse>: replace it with an approved warehouse name. Do not activate several consumers against the same stream. Each task starts suspended; running or resuming it can consume compute credits.

Create pipeline objects before inserting events

SQL · example
CREATE TABLE ORDER_EVENTS (EVENT_ID NUMBER,ORDER_ID NUMBER,EVENT_VERSION NUMBER,OP VARCHAR,AMOUNT NUMBER(12,2));
CREATE STREAM EVENT_CHANGES ON TABLE ORDER_EVENTS APPEND_ONLY=TRUE;
CREATE STREAM AUDIT_CHANGES ON TABLE ORDER_EVENTS APPEND_ONLY=TRUE;
CREATE TABLE ORDER_STATE (ORDER_ID NUMBER,EVENT_VERSION NUMBER,EVENT_ID NUMBER,AMOUNT NUMBER(12,2),IS_DELETED BOOLEAN);
CREATE TABLE EVENT_BATCH LIKE ORDER_EVENTS;
CREATE TABLE EVENT_AUDIT LIKE ORDER_EVENTS;
INSERT INTO ORDER_EVENTS VALUES
 (1,101,1,'UPSERT',100),(1,101,1,'UPSERT',100),
 (2,101,2,'UPSERT',120),(3,102,1,'UPSERT',50);

What to expect and why

The source and both streams contain four inserted event rows. Business result should become order 101 version 2 amount 120 and order 102 version 1 amount 50. Duplicate event 1 must not multiply target rows.

Check your understanding

Choose an answer, then check your reasoning. This is practice, not a scored test.

Why retain a tombstone for a deleted order?

Try it yourself

What should the initial active total be?

Compare your reasoning

170: order 101 contributes 120 and order 102 contributes 50.

Official sources and curriculum

Module 11 · See module scope