Snowflake Time Travel & Fail-safe | What is Continuous data protection | How to | with Examples
Hello Data Pros, and welcome back to another exciting episode of our Snowflake learning series!
In our last video, we explored various types of Snowflake views and their practical applications, demonstrated with easy-to-follow examples!
Today, we're diving deeper into Snowflake's data protection features, specifically Time Travel and Fail-safe!
In this digital world, data is the lifeblood and the most valued asset for any company. However, it is highly vulnerable to accidental loss and incorrect modifications. This reality emphasizes the critical need for robust data protection and recovery mechanisms within a data platform. Hence, Snowflake provides two key data protection features: Time Travel and Fail Safe, addressing this critical need.
To understand how these data protection features work in Snowflake, you must first be familiar with micro-partitions, how they are stored, and how modifications to existing table data are managed within this micro-partitions layer.
Well, we’ve already covered these topics in our initial videos, so I recommend watching our Snowflake playlist linked in the video description below, especially the video on key concepts of the storage layer.
Essentially, Time travel is a feature that allows you to go back in time, and recover table data as of specific dates or times in the past. With Time Travel enabled, you can revert your accidental or incorrect DML operations, or you can even recover an entirely dropped table, schema, or database.
If your data changes fall within the time travel duration, you can access and restore it yourself without involving Snowflake support.
In addition to this time travel feature, Snowflake also supports a failsafe period of 7 days, providing an additional layer of data protection. During this period, you cannot recover the data by yourself, but you can contact Snowflake support to retrieve data as needed.
Let's take a closer look at micro-partitioning and columnar storage using this example!
This diagram is intended only as a small-scale conceptual representation! in real-world data projects, tables often consist of numerous micro-partitions, with each one containing a substantial number of records.
As evident in this diagram, each micro-partition belongs to a single table, and each row belongs to a single micro-partition!
Considering the disk-read-order is left-to-right, you can see that column values are stored contiguously close to each other. This type of data storage is called columnar storage, which is quite different from the row-oriented storage represented by the leftmost image in this diagram.
Snowflake's micro-partitions are immutable, meaning once written, they are never modified. You might be curious about what happens when a record is updated! Well, Snowflake typically maintains a start date and end date for each micro-partition as part of its metadata. When a record is updated, Snowflake sets an end date for the entire micro-partition containing the updated record, and then creates a new micro-partition with the updated values. This mechanism enables Snowflake to support Time-Travel and Fail-Safe features.
It's worth noting that micro-partitions without an end-date are retained for-ever. However, those with end-date are kept until the end-date plus the Time-Travel and Fail-Safe periods elapse, post which they are permanently deleted.
Please note that retaining micro-partitions beyond their end date will result in additional storage costs!
So as a data expert, understanding how to select the right table type, and how to decide optimum time travel retention days for your tables is crucial. The following flowchart offers a high-level overview to help you identify the optimal table type and potential cost implications.
At a high level, as you move from top to bottom, the retention period of end-dated micro-partitions increases, along with the associated costs!
Let's explore different ways to utilize the time-travel feature in Snowflake!
You can query a table as it existed at a specific time in the past by specifying the exact timestamp.
With an offset, you can query a table as it existed 5 minutes earlier.
You can get the query ID of any DML statement, and query the table exactly as it existed before that DML operation was performed.
We’ll talk about cloning in detail in another video, but for now, please be aware that we can indeed clone tables, schemas, and databases as they existed at some point in the past.
Another important Time Travel statement is "un-drop." Yes, if you accidentally dropped your table, schema, or database, you can simply restore it using these "un-drop" statements.
It's time for a deep dive into Snowflake's UI, and witness all these time travel aspects in action!
To start with we’re creating a database, schema, table, and loading some data in the table.
If I query the table, I get all 5 rows that I inserted.
I'm recording the current timestamp so that I can demonstrate the time travel to this point later.
After noting down the timestamp, I'm updating the last name of Alice from Smith to Johnson, and I'm also deleting the row with employee ID 2.
If I query the table, I only get 4 rows, and the last name of Alice has been changed because of the DML statements above.
But what if you now realize that the DML operations you performed were incorrect? This is where Snowflake's robust time travel features come into play.
With time travel, you can easily query a historical snapshot of your table based on a timestamp.
This allows you to see all the records that were originally inserted, without the effects of the incorrect updates and deletes that were performed.
But time travel isn't limited to just direct timestamps. You can also use the OFFSET clause to travel back in time by seconds, minutes, hours, or even days.
Additionally, you can get the query ID for the DML operation from your query history.
And precisely time travel to the state before that DML operation was executed.
In addition to querying, you can also create or clone new tables using these time travel features.
While I've demonstrated this with the STATEMENT clause, TIMESTAMP and OFFSET-based time travel can also be used for cloning.
Furthermore, you may find yourself in a situation where you have accidentally dropped an entire table, schema, or even a database. That’s exactly why Snowflake supports Un-drop statements. With Un-drop, you can restore your objects to their state as they existed before they were dropped!
Please take a look at these SQL statements, which allow you to check the time travel retention days for your specific table. You can also change the default time travel duration to up to 90 days based on your requirements.
Now that we have a solid understanding of time travel, let's quickly understand how fail-safe works!
The main difference between time travel and fail-safe is that customers can perform time travel themselves to recover data as needed. However, to recover data using fail-safe, you must engage Snowflake support.
If the data you need to recover falls outside of your time travel window, but it’s within the 7-day fail-safe period, then you can reach out to Snowflake support for data recovery.
Please take a look at the high-level steps involved:
Firstly, the customer submits a support case, detailing the objects and the desired recovery time.
Next, the Snowflake support team assesses the feasibility of recovery. They carefully analyze the request to determine if recovery is possible.
Once feasibility is confirmed, the support team provides a script. This script typically contains create table statements with snowflake internal object IDs.
Finally, the customer executes the provided script with the appropriate role and virtual warehouse. This completes the fail-safe process, offering an extra layer of protection beyond the time travel duration.
That's all for today! Please stay tuned for our next video where we’ll explore more advanced Snowflake features!
If you found this video helpful, please give it a thumbs up, and hit that subscribe button to stay up-to-date with the latest technologies!
We also welcome your questions or thoughts in the discussion section below!
Thanks for watching!
Comments
Post a Comment