๐Ÿ“Š Data Engineering

Delta Lake

Delta Lake

Provides ACID transactions on data lakes. Developed by Databricks.

๐Ÿ“– Detailed Explanation

Delta Lake is an open-source storage layer that provides ACID transactions, schema evolution, and Time Travel capabilities for data lakes. It solves the "Data Swamp" problem of traditional data lakes and provides data warehouse-level reliability.

The core mechanism is the transaction log (_delta_log). Every data change is recorded as a commit log in JSON format, which guarantees ACID transactions. Even when multiple operations modify the same table at the same time, data consistency is maintained. This log also makes Time Travel possible, allowing you to query past versions of the data.

In ACID transactions, Atomicity guarantees that an operation either succeeds completely or fails completely. Consistency maintains data consistency through schema validation. Isolation provides isolation between concurrent operations, and Durability guarantees the persistence of committed data.

Time Travel lets you query past data by version number or timestamp. This allows you to roll back incorrect data modifications or perform an audit on the data as of a specific point in time. By default, history is retained for 30 days, and the VACUUM command cleans up old data.

๐Ÿ’ป Code Example

# Delta Lake ํ•ต์‹ฌ ๊ธฐ๋Šฅ ์˜ˆ์ œ

from delta.tables import DeltaTable
from pyspark.sql.functions import col, current_timestamp

# 1. Delta ํ…Œ์ด๋ธ” ์ƒ์„ฑ
df = spark.createDataFrame([
    ("user1", "Alice", 100),
    ("user2", "Bob", 200),
    ("user3", "Charlie", 150)
], ["user_id", "name", "balance"])

df.write.format("delta").mode("overwrite").save("/delta/accounts")

# 2. ACID ํŠธ๋žœ์žญ์…˜ - MERGE (Upsert)
updates = spark.createDataFrame([
    ("user1", "Alice", 120),   # ๊ธฐ์กด ์‚ฌ์šฉ์ž ์—…๋ฐ์ดํŠธ
    ("user4", "David", 300)    # ์ƒˆ ์‚ฌ์šฉ์ž ์‚ฝ์ž…
], ["user_id", "name", "balance"])

delta_table = DeltaTable.forPath(spark, "/delta/accounts")

delta_table.alias("target").merge(
    updates.alias("source"),
    "target.user_id = source.user_id"
).whenMatchedUpdate(set={
    "name": col("source.name"),
    "balance": col("source.balance")
}).whenNotMatchedInsert(values={
    "user_id": col("source.user_id"),
    "name": col("source.name"),
    "balance": col("source.balance")
}).execute()

# 3. Time Travel - ๋ฒ„์ „์œผ๋กœ ์กฐํšŒ
df_v0 = spark.read.format("delta").option("versionAsOf", 0).load("/delta/accounts")
print("๋ฒ„์ „ 0 (์ดˆ๊ธฐ ์ƒํƒœ):")
df_v0.show()

# ํƒ€์ž„์Šคํƒฌํ”„๋กœ ์กฐํšŒ
df_past = spark.read.format("delta").option("timestampAsOf", "2024-01-15 10:00:00").load("/delta/accounts")

# 4. ํžˆ์Šคํ† ๋ฆฌ ์กฐํšŒ
delta_table.history().show(truncate=False)

# 5. ๋กค๋ฐฑ - ์ด์ „ ๋ฒ„์ „์œผ๋กœ ๋ณต์›
# ๋ฐฉ๋ฒ• 1: RESTORE ๋ช…๋ น
spark.sql("RESTORE TABLE delta.`/delta/accounts` TO VERSION AS OF 0")

# ๋ฐฉ๋ฒ• 2: ์ด์ „ ๋ฒ„์ „ ๋ฎ์–ด์“ฐ๊ธฐ
df_v0 = spark.read.format("delta").option("versionAsOf", 0).load("/delta/accounts")
df_v0.write.format("delta").mode("overwrite").save("/delta/accounts")

# 6. ์Šคํ‚ค๋งˆ ์ง„ํ™” - ์ƒˆ ์ปฌ๋Ÿผ ์ž๋™ ์ถ”๊ฐ€
new_data = spark.createDataFrame([
    ("user5", "Eve", 250, "VIP")
], ["user_id", "name", "balance", "tier"])

new_data.write.format("delta").mode("append").option("mergeSchema", "true").save("/delta/accounts")

# 7. ์ตœ์ ํ™” - ์†Œ๊ทœ๋ชจ ํŒŒ์ผ ๋ณ‘ํ•ฉ
delta_table.optimize().executeCompaction()

# Z-ORDER๋กœ ์ฟผ๋ฆฌ ์„ฑ๋Šฅ ์ตœ์ ํ™”
delta_table.optimize().executeZOrderBy("user_id")

# 8. VACUUM - ์˜ค๋ž˜๋œ ํŒŒ์ผ ์ •๋ฆฌ (๊ธฐ๋ณธ 7์ผ ์ด์ƒ ์ง€๋‚œ ํŒŒ์ผ)
delta_table.vacuum(168)  # 168์‹œ๊ฐ„ = 7์ผ

print("์ตœ์ข… ํ…Œ์ด๋ธ” ์ƒํƒœ:")
spark.read.format("delta").load("/delta/accounts").show()

๐Ÿ—ฃ๏ธ How It's Said in Practice

Data Engineer: "The batch job failed halfway through yesterday, but thanks to Delta Lake there was no partial write problem. The transaction was rolled back, so data consistency was maintained."

Junior: "What would have happened with a legacy Parquet table?"

Senior: "It would have been left half-written, and we'd have had to clean it up manually. Without ACID, data easily gets corrupted in situations like this."

Interviewer: "Among Delta Lake, Apache Iceberg, and Apache Hudi, which would you choose?"

Candidate: "In a Databricks environment, I would choose Delta Lake because it is optimized for it. If compatibility with various query engines such as Presto, Trino, and Flink in a multi-engine environment is important, I think I would consider Iceberg. All three support ACID and Time Travel, but their ecosystems are different."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Reviewer: "You set the VACUUM retention to 0 hours, so Time Travel may not work."

Author: "I was trying to save storage space, but I'll change it to keep at least 7 days. We should prepare for a possible rollback situation."

โš ๏ธ Cautions

๐Ÿ“š Learn More