Notes · Data engineering
Consolidating 185 Airflow DAG files into 97
Our lakehouse refresh used to take two to three hours. Most of that time went into starting Spark applications, and there were a lot of them. Over about five weeks this fall I folded the per-table jobs into a handful of config-driven ones, and the same refresh now finishes in minutes.
How it got that way
The lakehouse loads infrastructure inventory into Apache Iceberg: EC2, VPC and IAM data from our cloud's admin APIs, plus some monitoring and data-center sources. It grew one table at a time. Whenever someone needed a new table, the quickest path was to copy an existing Spark job and its DAG and change the parts that differed, so that's what we did. By September the VPC data alone was spread over 25 separate Spark applications, and EC2 over 14.
Each application started its own driver and executors, listed the same S3 prefixes and read the same Parquet before writing its one table. Multiply that by a few hundred runs and you have most of the refresh time. The schedules didn't help. Jobs ran hourly or daily because the DAG they were copied from did, whether or not new data had arrived.
Nobody had filed a ticket about any of this. It was just how the pipelines were, and I was tired of making the same timestamp fix in 25 files.
One application per source
The replacement is one Spark application per source, with a registry of datasets inside it. A dataset is little more than a source folder, a SQL transform onto the output schema, and the merge keys. Roughly:
DATASETS = {
"subnets": Dataset(source="DescribeSubnets", schema=SUBNETS,
merge_keys=["subnet_id", "region"], sql=SUBNETS_SQL),
"route_tables": Dataset(...),
# 25 in all for VPC
}
def run(spark, datasets, max_parallel=4):
def one(ds):
s = spark.newSession() # separate temp views and conf
dates = dates_behind(s, ds) # a table that is current gets skipped
if not dates:
return
df = transform(s, ds, read_source(s, ds.source, dates))
assert_schema(df, ds.schema) # checked before anything is written
merge(s, df, ds.merge_keys, dates)
with ThreadPoolExecutor(max_parallel) as pool:
results = {name: pool.submit(one, ds) for name, ds in datasets.items()}
failed = [n for n, f in results.items() if f.exception()]
if failed:
raise RuntimeError(f"datasets failed: {failed}")
Most of the speedup comes from doing the start-up and the reads once. A few other choices mattered too.
Each table works out its own dates. If a table is already current the job skips it, and if it's a few days behind it catches up in the same run. The job then lists and reads only the source folders that at least one table needs.
Datasets run in parallel from a thread pool, and each one gets its own spark.newSession(). That keeps temp views and per-dataset settings apart, which is what lets several Iceberg MERGEs run from one application without stepping on each other. With spark.scheduler.mode=FAIR they share executors instead of queuing behind the biggest dataset.
The output schemas are imported unchanged from the old jobs and asserted during query analysis, before anything is written. Table names, partitioning and merge keys stayed the same, so nothing downstream had to know the jobs had changed. There are also no count(), collect() or isEmpty() calls before the write; the only work before it is listing S3 prefixes and analysing the queries.
If one dataset fails, the others still finish, and the job fails at the end with a list of what broke. With 25 tables in one job, I didn't want one bad transform to take the other 24 down with it.
Running when the data lands
The other half of the change was timing. Ingestion writes new files on a known schedule, so each DAG now runs a few minutes after its source lands. A run also covers whatever range of dates is outstanding. Before, if Airflow had been paused, it queued one run per missed interval; now a single run catches up.
Once the jobs were cheap, hourly runs became realistic. I added hour partitions to the EC2, VPC and IAM tables, moved them to hourly runs, and pulled Iceberg maintenance (snapshot expiry and compaction) into its own daily job.
Rollout
I did it one source at a time, in four pull requests between September 10 and October 16. EC2 and VPC went first, then hourly partitions and IAM, then the monitoring sources together with the schedule change, and last the configuration-management inventory, which also let me delete a set of aggregation DAGs. Going source by source kept each cut-over small enough to check table by table.
| Before | After | |
|---|---|---|
| Airflow DAG files | 185 | 97 |
| Spark job modules | 364 | 266 |
| Lines of code | about 41K fewer (68K deleted, 27K added) | |
| Full refresh | 2-3 hours | minutes |
| Schedule | a few runs a day | hourly |
If I did it again, I'd put data-quality checks into the shared job earlier. With 25 tables behind one runner, a single bug reaches all of them, and a schema assertion only catches part of that. I'd also write down missing source fields from the start. A few fields the old jobs filled in aren't in the flattened source at all. They're listed in the code now and logged on every run, so a null in those columns is expected rather than a mystery.
More notes at ankitkhare.dev. Questions or corrections: ankit@ankitkhare.dev.