Delta lake with Spark: What and Why? | by Jyoti Dhiman | Towards Data Science

Let me start by introducing two problems that I have dealt time and again with my experience with Apache Spark:
Data “overwrite” on the same path causing data loss in case of Job Failure.Updates in the data.
Data “overwrite” on the same path causing data loss in case of Job Failure.
Updates in the data.
Sometimes I solved above with Design changes, sometimes with the introduction of another layer like Aerospike, or sometimes by maintaining historical incremental data.
Maintaining historical data is mostly an immediate solution but I don’t really like dealing with historical incremental data if it’s not really required as(at least for me) it introduces the pain of backfill in case of failures which may be unlikely but inevitable.
The above two problems are “problems” because Apache Spark does not really support ACID. I know it was never Spark’s use case to work with transactions(hello, you can’t have everything) but sometimes, there might be a scenario(like my two problems above) where ACID compliance would have come in handy.
When I read about Delta Lake and its ACID compliance, I saw it as one of the possible solutions for my two problems. Please read on to find out how the two problems are related to ACID compliance failure and how delta lake can be seen as a savior?
Delta Lake Documentation introduces Delta lake as:
Delta Lake is an open source storage layer that brings reliability to data lakes. Delta Lake provides ACID transactions, scalable metadata handling, and unifies streaming and batch data processing. Delta Lake runs on top of your existing data lake and is fully compatible with Apache Spark APIs.
Delta Lake key points:
Supports ACID
Enables Time travel
Enables UPSERT
Consider the following piece of code to remove duplicates from a dataset:
# Read from HDFSdf = spark.read.parquet("/path/on/hdfs") # Line 1# Remove duplicatesdf = df.distinct() # Line 2# Overwrite the datadf.cache() # Line 3df.write.parquet("/path/on/hdfs", mode="overwrite") # Line 4
For my spark application running above piece of code consider a scenario where it fails on Line 4, that is while writing the data. This may or may not lead to data loss. [Problem #1: As mentioned above].You can replicate the scenario, by creating a test dataset and kill the job when it’s in the Write stage.
Let us try to understand ACID failure in spark with the above scenario.
What is Atomicity: Either all changes take place or none, the system is never in halfway state.
How spark fails: While writing data, (at Line 4 above), if a failure occurs at a stage where old data is removed and new data is not yet written, data loss occurs. We have lost old data and we were not able to write new data due to job failure, atomicity fails. [It can vary according to file output committer used, please do read about File output committer to see how data writing takes place, the scenario I explained is for v2]
What is Consistency: Data must be consistent and valid in the system at all times.
How Spark fails: As seen above, in the case of failure and data loss, we are left with invalid data in the system, consistency fails.
What is Isolation: Multiple transactions occur in isolation
How spark fails: Consider two jobs running in parallel, one as described above and another which is also using the same dataset, if one job overwrites the dataset while other is still using it, failure might happen, isolation fails.
What is Durability: Changes once made are never lost, even in the case of system failure.
How spark might fail: Spark really doesn’t affect the durability, it is mainly governed by the storage layer, but since we are losing data in case of job failures, in my opinion, it is a durability failure.
Delta lake maintains a delta log in the path where data is written. Delta Log maintains details like:
Metadata like - Paths added in the write operation.- Paths removed in the write operation.- Data size- Changes in data
Data Schema
Commit information like- Number of output rows- Output bytes- Timestamp
Sample log file in _delta_log_ directory created after some operations:
After successful execution, a log file is created in the _delta_log_ directory. The important thing to note is when you save your data as delta, no files once written are removed. The concept is similar to versioning.
By keeping track of paths removed, added and other metadata information in the _delta_log_, Delta lake is ACID-compliant.
Versioning enables time travel property of Delta Lake, which is, I can go back to any state of data because all this information is being maintained in _delta_log_.
With the support for ACID, if my job fails during the “overwrite” operation, data is not lost, as changes won’t be committed to the log file of _delta_log_ directory. Also, since Delta Lake, does not remove old files in the “overwrite operation”, old state of my data is maintained and there is no data loss. (Yes, I have tested it)
Delta lake supports Update operation as mentioned above so it makes dealing with updates in data easier.
Until next time,Ciao.