Recovery and final lab: preserve the boundary and prove the state
A recovery plan must explain what data was lost, what source can reconstruct it and where normal incremental processing resumes.
Understand the idea
Never wait deliberately for the learning stream to become stale or shorten retention on shared data. Read SHOW STREAMS and rehearse a controlled rebuild on the durable ORDER_EVENTS log instead. A genuinely stale stream needs the same evidence-based reasoning.
| Concept | What it means |
|---|---|
| Quiesce writers | Pause source changes and consumers for this simple recovery procedure. |
| Rebuild | Reconstruct state from an authoritative complete source. |
| New boundary | Establish tracking only after reconciliation. |
| Independent audit | Consume its own stream without affecting the processing stream. |
A concrete example
After all prior lessons run once: active count 2, active total 130, audit count 9, processing pending 0. Additional manual inserts or repeated task experiments change these assumptions.
Remember: Recovery is a data-reconciliation procedure plus a new offset—not simply CREATE OR REPLACE STREAM.
Explain it step by step · full theory
Understand the concept
Never wait deliberately for the learning stream to become stale or shorten retention on shared data. Read SHOW STREAMS and rehearse a controlled rebuild on the durable ORDER_EVENTS log instead. A genuinely stale stream needs the same evidence-based reasoning.
This lab has the complete immutable event history, so current state can be rebuilt by choosing the highest version for each order. If the real source no longer contains a complete history, this method is not sufficient: retrieve backups or source replays before claiming recovery.
Walk through the behavior
For the simple procedure here, stop producers and all consuming tasks, confirm no runs are active, build a separate candidate state and compare it. Keep producers paused while committing target replacement and resetting the tracking boundary. DDL and DML are separate boundaries; this is a maintenance-window approach, not a zero-downtime guarantee.
Resetting a stream deliberately discards its old unconsumed interval. Only do that after proving the rebuild includes it. The recreated source stream captures future writes; it does not recover history by itself.
Finally consume AUDIT_CHANGES into EVENT_AUDIT. After the full ordered lab there are nine immutable events, including the replay and deletion. The processing target has three state rows, one tombstone, two active orders and active total 130.
Words used in this lesson
- Quiesce writers
- Pause source changes and consumers for this simple recovery procedure.
- Rebuild
- Reconstruct state from an authoritative complete source.
- New boundary
- Establish tracking only after reconciliation.
- Independent audit
- Consume its own stream without affecting the processing stream.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Recovery and final lab: preserve the boundary and prove the state
Start with the source
A recovery plan must explain what data was lost, what source can reconstruct it and where normal incremental processing resumes.
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
- Start with the source
A recovery plan must explain what data was lost, what source can reconstruct it and where normal incremental processing resumes.
- Follow the work
Never wait deliberately for the learning stream to become stale or shorten retention on shared data. Read SHOW STREAMS and rehearse a controlled rebuild on the durable ORDER_EVENTS log instead. A genuinely stale stream needs the same evidence-based reasoning.
- Explain the outcome
Recovery is a data-reconciliation procedure plus a new offset—not simply CREATE OR REPLACE STREAM.
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.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- 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.
- 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. - 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.
Reconcile business state and independent audit
SELECT ORDER_ID,EVENT_VERSION,AMOUNT,IS_DELETED FROM ORDER_STATE ORDER BY ORDER_ID;
SELECT COUNT(*) AS ACTIVE_ORDERS,SUM(AMOUNT) AS ACTIVE_TOTAL FROM ORDER_STATE WHERE NOT IS_DELETED;
INSERT INTO EVENT_AUDIT SELECT EVENT_ID,ORDER_ID,EVENT_VERSION,OP,AMOUNT FROM AUDIT_CHANGES;
SELECT COUNT(*) AS AUDIT_EVENTS FROM EVENT_AUDIT;
SELECT COUNT(*) AS PROCESSING_PENDING FROM EVENT_CHANGES;What to expect and why
After all prior lessons run once: active count 2, active total 130, audit count 9, processing pending 0. Additional manual inserts or repeated task experiments change these assumptions.
Controlled rebuild candidate — producers and tasks paused
CREATE TABLE RECOVERY_STATE AS
SELECT ORDER_ID,EVENT_VERSION,EVENT_ID,IFF(OP='DELETE',NULL,AMOUNT) AS AMOUNT,
(OP='DELETE') AS IS_DELETED FROM ORDER_EVENTS
QUALIFY ROW_NUMBER() OVER(PARTITION BY ORDER_ID ORDER BY EVENT_VERSION DESC,EVENT_ID DESC)=1;
SELECT * FROM RECOVERY_STATE ORDER BY ORDER_ID;What to expect and why
Expect the same latest state as ORDER_STATE. First validate event contract and absence of conflicting versions; do not choose arbitrary winners for malformed history.
Apply only after candidate reconciliation, with producers still paused
BEGIN TRANSACTION;
DELETE FROM ORDER_STATE;
INSERT INTO ORDER_STATE SELECT ORDER_ID,EVENT_VERSION,EVENT_ID,AMOUNT,IS_DELETED FROM RECOVERY_STATE;
COMMIT;
-- Deliberately reset tracking only because the complete source was rebuilt:
CREATE OR REPLACE STREAM EVENT_CHANGES ON TABLE ORDER_EVENTS APPEND_ONLY=TRUE;
-- Inspect ownership/grants and task dependencies again before restarting.
SHOW STREAMS IN SCHEMA L2_OBJECTS_LAB.M11_LAB;What to expect and why
This is a dedicated lab maintenance operation, not a blanket production repair. It discards the old stream boundary after rebuilding the represented state; producers must remain paused through the reset. Review grants because replacement creates a new stream object.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
State the final expected active records after the full ordered lab.
Compare your reasoning
Order 102 has 55 and order 103 has 75. Order 101 remains deleted at version 3. Active total is 130; nine source events remain available for audit.