mark_ott
Databricks Employee
Databricks Employee

For scenarios in Databricks where lower latency is needed for Silver tables but continuous streaming pipelines are not feasible, using jobs or notebooks with foreachBatch running in Structured Streaming mode is a common and recommended approach. This lets micro-batches process and write data incrementally, giving more frequent updates compared to traditional batch jobs while not requiring a fully continuous pipeline.​

Why foreachBatch Is Used

  • Enables micro-batch processing for data sinks (or targets) that may only support batch writes, not full streaming writes.​

  • Provides flexibility for upserting, deduplication, complex transformations, and custom logic that can't be expressed in straightforward streaming queries.​

  • Can be triggered by jobs or notebooks and run until manually stopped, making them suitable for near-real-time updates where a persistent pipeline isn't needed or isn't possible.​

Tradeoffs and Considerations

  • Latency depends on the trigger interval of the job/notebook (how often micro-batches are processed)—setting a lower interval (like a few seconds rather than minutes) can reduce Silver table latency.​

  • This approach usually provides “at least once” guarantees unless deduplication is explicitly handled (using batchId or primary key logic).​

  • If frequent updates and low-latency are a top priority, but a continuously running pipeline is too costly/complex, this method balances flexibility and performance for many Silver table scenarios.​

Additional Optimization Tips

  • For even lower latency, tune the micro-batch trigger interval and manage Spark cluster resources to avoid queuing delays.​

  • Consider file optimization options (such as compaction and optimized writes for Delta tables) to reduce read and write latency on Silver tables.​

  • If latency is still insufficient, continuous pipelines (while more expensive) may ultimately be required for real-time use cases, but for most “near-real-time” cases, the foreachBatch jobs/notebooks solution is effective and widely adopted.​

In summary, running jobs or notebooks leveraging foreachBatch until stopped is generally the best solution for low-latency Silver tables when continuous pipelines are not viable or required.​