Data Lakehouse Architecture: Delta Lake, Iceberg, Hudi
Difficulty: Senior Level | Companies: Databricks, Netflix, Uber, Airbnb, Apple
1. What is the Data Lakehouse?
The lakehouse combines data lake flexibility with data warehouse reliability:
Traditional Stack:
Data Lake (S3/GCS) ā ETL ā Data Warehouse ā Analytics
(Cheap, flexible) (Slow) (Expensive, fast)
Lakehouse:
Data Lake + ACID + Schema Enforcement + Time Travel
(All in one layer)
Key Lakehouse Properties
| Feature | Data Lake | Data Warehouse | Lakehouse |
|---|---|---|---|
| Storage Cost | Low | High | Low |
| Schema Enforcement | None | Strict | Flexible |
| ACID Transactions | No | Yes | Yes |
| Time Travel | No | Limited | Yes |
| Format | Parquet/ORC | Proprietary | Open (Parquet) |
| Streaming Support | Good | Poor | Good |
ā¹ļø
Key Insight: The lakehouse eliminates the need for separate lake and warehouse by adding warehouse-like features directly on top of open file formats.
2. Delta Lake
ACID Transactions on Data Lakes
from delta.tables import DeltaTable
from pyspark.sql import SparkSession
spark = SparkSession.builder \
.appName("DeltaLakehouse") \
.config("spark.sql.extensions", "io.delta.sql.DeltaSparkSessionExtension") \
.config("spark.sql.catalog.spark_catalog", "org.apache.spark.sql.delta.catalog.DeltaCatalog") \
.getOrCreate()
# Create Delta table with ACID
df = spark.read.parquet("s3://raw/events/")
df.write.format("delta") \
.mode("overwrite") \
.save("s3://lakehouse/events")
# MERGE (upsert) ā impossible with plain Parquet
delta_table = DeltaTable.forPath(spark, "s3://lakehouse/events")
new_data = spark.read.parquet("s3://raw/events_new/")
delta_table.alias("target").merge(
new_data.alias("source"),
"target.event_id = source.event_id"
).whenMatchedUpdateAll() \
.whenNotMatchedInsertAll() \
.execute()
# Time Travel ā query historical data
df_yesterday = spark.read.format("delta") \
.option("versionAsOf", 123) \
.load("s3://lakehouse/events")
# Or by timestamp
df_jan1 = spark.read.format("delta") \
.option("timestampAsOf", "2024-01-01") \
.load("s3://lakehouse/events")
# VACUUM ā clean up old files
delta_table.vacuum(retentionHours=168) # 7 days
Delta Lake Internals
3. Apache Iceberg
Table Format
from pyspark.sql import SparkSession
spark = SparkSession.builder \
.appName("IcebergLakehouse") \
.config("spark.sql.catalog.iceberg", "org.apache.iceberg.spark.SparkCatalog") \
.config("spark.sql.catalog.iceberg.type", "hadoop") \
.config("spark.sql.catalog.iceberg.warehouse", "s3://lakehouse/iceberg") \
.getOrCreate()
# Create Iceberg table
spark.sql("""
CREATE TABLE iceberg.db.events (
event_id BIGINT,
user_id BIGINT,
event_type STRING,
event_timestamp TIMESTAMP,
properties MAP<STRING, STRING>
) USING iceberg
PARTITIONED BY (days(event_timestamp))
TBLPROPERTIES (
'format-version' = '2',
'write.parquet.compression-codec' = 'zstd'
)
""")
# Schema evolution (safe, column-level)
spark.sql("""
ALTER TABLE iceberg.db.events ADD COLUMN new_field STRING AFTER user_id
""")
# Hidden partitioning (auto-pruning)
spark.sql("""
SELECT * FROM iceberg.db.events
WHERE event_timestamp >= '2024-01-01'
LIMIT 100
""")
# Time travel with snapshots
spark.sql("""
SELECT * FROM iceberg.db.events
TIMESTAMP AS OF '2024-01-15 10:00:00'
""")
# Snapshot inspection
spark.sql("SELECT * FROM iceberg.db.events.snapshots")
python
from pyspark.sql import SparkSession
spark = SparkSession.builder \
.appName("HudiLakehouse") \
.config("spark.serializer", "org.apache.spark.serializer.KryoSerializer") \
.config("spark.sql.hive.convertMetastoreParquet", "false") \
.getOrCreate()
hudi_options = {
'hoodie.table.name': 'events',
'hoodie.datasource.write.recordkey.field': 'event_id',
'hoodie.datasource.write.precombine.field': 'event_timestamp',
'hoodie.datasource.write.operation': 'upsert',
'hoodie.datasource.write.table.type': 'COPY_ON_WRITE',
'hoodie.compact.inline.max.delta.commits': '5',
}
# Upsert data
df.write.format("hudi") \
.options(**hudi_options) \
.mode("append") \
.save("s3://lakehouse/hudi/events")
# Read with time travel
spark.read.format("hudi") \
.option("as.of.instant", "20240115000000") \
.load("s3://lakehouse/hudi/events")
# Incremental read
spark.read.format("hudi") \
.option("hoodie.datasource.query.type", "incremental") \
.option("hoodie.datasource.read.begin.instanttime", "20240101000000") \
.option("hoodie.datasource.read.end.instanttime", "20240131000000") \
.load("s3://lakehouse/hudi/events")
5. Lakehouse Comparison
| Feature | Delta Lake | Iceberg | Hudi |
|---|---|---|---|
| ACID Transactions | Yes | Yes | Yes |
| Schema Evolution | Yes | Yes (better) | Yes |
| Time Travel | Yes | Yes | Yes |
| Hidden Partitioning | No | Yes | Yes |
| Upsert Performance | Good | Good | Excellent |
| Streaming Reads | Yes | Yes | Yes |
| Open Source | Yes (OSS) | Yes (ASF) | Yes (ASF) |
| Primary Backer | Databricks | Netflix | Uber |
ā ļø
Common Interview Trap: Don't say "Iceberg is better than Delta" or vice versa. Each has tradeoffs ā Iceberg has better hidden partitioning, Delta has better streaming, Hudi has better upserts.
6. Lakehouse Best Practices
File Optimization
# Optimize file sizes (aim for 128MB-1GB per file)
spark.sql("""
OPTIMIZE delta.`s3://lakehouse/events`
ZORDER BY (user_id, event_timestamp)
""")
# Iceberg compaction
spark.sql("""
CALL iceberg.system.rewrite_data_files(
table => 'db.events',
options => map('target-file-size-bytes', '134217728')
)
""")
Partition Strategy
Partition by date (not user_id):
ā
Date pruning for time-range queries
ā
Even file sizes (events distributed by time)
ā User-specific queries scan all partitions
Partition by user_id:
ā
User lookups are fast
ā Hot partitions (power users)
ā Time-range queries scan all partitions
Best: Partition by date, Z-order by user_id
ā¹ļø
Best Practice: Use Z-ORDER or data clustering for multi-dimensional queries. This creates zone maps that enable skipping irrelevant data blocks.
Follow-Up Questions
- How would you migrate from Parquet to Delta Lake without downtime?
- Compare write amplification between COW and MOR table types.
- Design a lakehouse architecture for 10PB of streaming + batch data.
- How do you handle GDPR deletions in a lakehouse?
- Design a cost optimization strategy for a lakehouse on S3.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type ā Always start by categorizing the problem
- Incorrect formula application ā Verify the formula applies to your specific case
- Unit errors ā Always check units are consistent throughout
- Sign errors ā Pay careful attention to positive and negative values
- Rounding too early ā Keep full precision until the final answer
- Forgetting constraints ā Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic ā Build on the foundation laid here
- Next Topic ā See how this concept extends to more advanced material
- Practice Problems ā Additional exercises with solutions
- Video Explanation ā Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions