Skip to the page

Databricks interview question · easy

Ron was performing an UPDATE operation on a large Delta table. He then realized he made a mistake. How can he easily revert the update without manually reconstructing the previous state?

The short answerRon can easily revert the incorrect update on a Delta table using Time Travel.

The full answer

Ron can easily revert the incorrect update on a Delta table using Time Travel. Delta Lake automatically versions every operation, allowing you to query or restore previous states of your data. Here's how Ron can revert the update:

1. Identify the Version Before the Mistake:

First, Ron needs to find the Delta table version before the incorrect update. He can do this using the history command:

from delta.tables import *

deltaTable = DeltaTable.forName(spark, "my_delta_table") # Or DeltaTable.forPath(...)

historyDF = deltaTable.history()
historyDF.show()

This will display a history of operations performed on the table, including timestamps, version numbers, and operation types (e.g., UPDATE, WRITE). Ron should identify the version number immediately preceding the erroneous update.

2. Revert to the Previous Version:

Once Ron has the correct version number (let's say it's version 5), he can use one of the following methods to revert:

a) Using restoreToVersion (Recommended):

This is the most direct way to revert the table to a specific version:

deltaTable.restoreToVersion(5)

This will create a new commit that effectively undoes all changes made after version 5.

b) Using restoreToTimestamp:

If Ron knows the timestamp before the mistake, he can restore to that time:

deltaTable.restoreToTimestamp("2024-10-27T10:00:00.000+0000") # Replace with the correct timestamp

This is useful if you don't have the exact version number but remember roughly when the mistake occurred.

c) Querying a Specific Version (Read Only):

If Ron just wants to query the previous state without actually reverting the table, he can use the versionAsOf or timestampAsOf options when reading:

df = spark.read.table("my_delta_table@v5") # Using version
df = spark.read.table("my_delta_table timestamp as of '2024-10-27T10:00:00.000+0000'") # Using timestamp

This creates a DataFrame representing the table at the specified version or timestamp, without altering the current state of the table.

Important Considerations:

  • Vacuum: Delta Lake's VACUUM command removes old data files that are no longer needed. If Ron has run VACUUM with a short retention period, the older versions might be permanently deleted, and he won't be able to revert to them. The default retention period is 7 days.
  • Time Travel is Efficient: Time travel operations are metadata operations and are very fast. They don't involve physically copying large amounts of data.

By using Time Travel, Ron can easily and efficiently correct mistakes made during updates without resorting to complex manual recovery procedures.

Last updated 17 January 2025

Asked next