A manifest-driven, multi-threaded Spark Structured Streaming design for transforming files spread across thousands of directories into Iceberg tables, at a steady 2,000+ partitions a minute.
Arrival and processing are decoupled by a manifest. Spark reads the manifest as a stream and fans each micro-batch out to a pool of threads, one partition per thread.
Highly partitioned data is everywhere: telemetry by device and time window, transactions by seller and date, logs by service and region. Here each partition was a set of compressed CSV files, and the files for one partition could sit in several different directory trees. Every partition had to be validated, transformed and converted to Parquet.
At a few thousand partitions, processing each file as it arrives works. At 450,000+ partition combinations a day it breaks down. The system needed to:
A lightweight serverless function captures metadata for every arriving file, including its full path, and writes it to a manifest partitioned by date. The manifest becomes the single source of truth: it maps each partition key to every location holding its files, so the processing layer never has to walk directory trees. It also gives you observability for free, since pending work is just a query.
A Spark Structured Streaming job reads the manifest and hands each micro-batch to foreachBatch. Inside, a ThreadPoolExecutor with 200 workers processes partitions concurrently, one partition per thread.
# Spark Streaming integration
spark.readStream \
.format("parquet") \
.load("manifest_path") \
.writeStream \
.foreachBatch(process_partition_batch) \
.trigger(processingTime="5 minutes") \
.start()
# Conceptual approach
def process_partition_batch(manifest_batch):
with ThreadPoolExecutor(max_workers=200) as executor:
futures = []
for partition in manifest_batch:
future = executor.submit(
process_single_partition,
partition.partition_key_1,
partition.partition_key_2,
partition.file_paths
)
futures.append(future)
# Wait for all partitions in batch to complete
results = [f.result() for f in futures]
return results
Each thread passes all of its partition's file paths to a single read, so Spark loads files from different directories into one DataFrame. No manual consolidation, and no path handling in application code. Dynamic executor allocation keeps the cluster small while threads chew through KB-sized partitions and scales it up when a 100 GB+ partition arrives.
The same job runs in different modes by changing only the trigger: processingTime for steady micro-batches, availableNow to drain a backlog like a batch job and stop, keeping the checkpoint either way.
Deleting a partition and re-inserting it is fast but loses data if the job dies between the two steps. Instead each partition lands as one atomic Iceberg commit on a copy-on-write table, so a failed write leaves the previous snapshot intact. Iceberg's auto-compaction consolidates small files in the background without blocking writers.
def process_with_retry(partition, max_retries=5):
for attempt in range(max_retries):
try:
return process_partition(partition)
except Exception as e:
if attempt == max_retries - 1:
write_to_failed_tracking_table(partition, e)
raise
time.sleep(2 ** attempt) # Exponential backoff
A failure in one thread never affects the others. Spark's checkpoint records which manifest batches are complete, so there is no external state store to run.
200 partitions in flight. Fine enough to keep the cluster busy, coarse enough that each unit is a meaningful Spark job rather than millions of tiny file tasks. At 2,000+ a minute a full day clears in about an hour.
Bucket-level encryption keys cut calls to the encryption service by about 99%, which kept 200 concurrent readers from being throttled.
Process only the newest version. In batch mode the manifest query picks the latest version; in streaming mode a version-tracking table (or DynamoDB) gates each write so an older file never overwrites a newer one.
Partition keys are the identity. Reprocessing a partition replaces it rather than appending, so retries and replays never create duplicates.