dbt-databricks icon indicating copy to clipboard operation
dbt-databricks copied to clipboard

support concurrent_batches for microbatch

Open data-blade opened this issue 1 year ago • 3 comments

Describe the feature

see slack thread

setting concurrent_batches=true for incremental_strategy=microbatch yields

[WARNING]: Found 1 microbatch model with the `concurrent_batches` config set to true, but the databricks adapter does not support running batches concurrently. Batches will be run sequentially.

@benc-db followed the dbt guide to activate concurrency, which yielded

[DELTA_CONCURRENT_APPEND] ConcurrentAppendException: Files were added to the root of the table by a concurrent update. Please try the operation again.

qouting him:

There are ways to allow concurrent writes, but it looks like they may be dependent on the actual schema of the table: link

considering that the whole 'microbatch' feature was developed especially with large datasets in mind, concurrency would be really powerful.

Describe alternatives you've considered

given an incremental batch of 5 days, we tried using jinja to loop through these 5 days and then unioning them (to not overload the memory with too many rows), but that got "optimized away" by the internal query optimizer. but it also just creates annoying complexity on top of your models.

Additional context

none

Who will this benefit?

clients with large datasets/heavy operations, where the 'row count' bottleneck does not scale linearly but explodes after some threshold (depending on memory)

Are you interested in contributing this feature?

yes, but no idea how for this topic

data-blade avatar Jan 27 '25 13:01 data-blade

Hi, I'll describe my experience with enabling full microbatch concurrency support for this adapter. I have a model with microbatch incremental materialization with batch size = day. It contains data for the last 3.5 years. Each day is calculated somewhere between 60 (closer to the beginning) and 700 (close to the end) seconds. I wanted to speed up its full refresh so I read what options for concurrent writes I have with Delta:

For partitioned table Delta claims to not have conflicts when writes happen to different partitions

You can make the two sets of files disjoint by partitioning the table by the same columns as those used in the conditions of the operations... Hence, partitioning a table according to the conditions commonly used on the command can reduce conflicts significantly. https://docs.delta.io/latest/concurrency-control.html#avoid-conflicts-using-partitioning-and-disjoint-command-conditions

Liquid clustering does a similar statement

Row-level concurrency reduces conflicts between concurrent write operations by detecting changes at the row-level and automatically resolving conflicts that occur when concurrent writes update or delete different rows in the same data file. https://docs.databricks.com/aws/en/optimizations/isolation-level#write-conflicts-with-row-level-concurrency

My table was already enabled for auto liquid clustering so I decided to give it a try. I forked the repo dbt-databricks and made a change to enable full microbatch concurrency support: https://github.com/oruen/dbt-databricks/commit/cf861f7222d9a0f9cd64ce4eca79421c710ca688 After that a python wheel was created which replaced pypi dbt-databricks dependency in Databricks job environment. The change resulted in 8 microbatches being calculated in parallel, significantly reducing the full refresh time.

Even though there were no errors related to concurrent updates to the Delta table, I cannot call it a clean test because Delta write time was only ~ 8 seconds and there is a chance no writes happened at the exactly same time. I think this test can be a ground for testing concurrent batches execution more thoroughly. I can think of a test design that would run more than 8 batches concurrently with no transformation of the model data, which should guarantee writes happen at the same time.

What do you think? Does it make sense to try?

oruen avatar Jul 31 '25 17:07 oruen

I would like this feature please! I just implemented incremental microbatch on a ton of tables and while its nice for multiple other reasons, having the concurrency support would be amazing

wmjones avatar Aug 06 '25 12:08 wmjones

One issue that I run into while having concurrent batches enabled is optimize query that is executed after each microbatch. Concurrent optimize can lead to an error: https://kb.databricks.com/delta/error-when-trying-to-perform-maintenance-on-a-delta-table

The workaround is to disable optimize calls via DATABRICKS_SKIP_OPTIMIZE variable and call optimize in a post-run hook

oruen avatar Oct 01 '25 09:10 oruen