Sub topic 15
Best practices and common pitfalls
This chapter assumes you have worked through the earlier chapters. You should know how streaming tables, materialized views, expectations, update modes, AUTO CDC and the event log work. It collects the habits that keep a pipeline cheap, readable and safe, and the mistakes that most often cause trouble.
Design the pipeline
Keep the graph simple and layered. Use bronze for raw ingestion, silver for validated and typed rows, and gold for aggregates. Each layer should have one job.
- Use streaming tables for append-style sources and materialized views for transformations. Files, message buses and CDC feeds fit streaming tables. Joins and aggregations that must reflect current data fit materialized views.
- Cast types in silver, not bronze. Bronze should keep the data as it arrived so you can reprocess it later.
- Set a target catalog and schema on the pipeline. Then dataset names stay short and each environment can point to its own schema.
- Prefer serverless compute when it is available. It removes cluster sizing from the work and supports incremental refresh of materialized views.
Protect data quality
Choose the expectation action by the cost of a bad row.
| Action | Use it when |
|---|---|
| Warn (default `EXPECT`) | You want to measure a problem but keep the rows. |
| Drop row | Bad rows would corrupt downstream results and can be discarded. |
| Fail update | A violation means something upstream is badly broken. |
Do not drop rows silently. A dropped row still shows up in the event log metrics, so check them. The query below lists recent progress events for the pipeline that owns orders_silver.
SELECT timestamp, event_type, message
FROM event_log(TABLE(main.demo.orders_silver))
WHERE event_type = 'flow_progress'
ORDER BY timestamp DESC
LIMIT 20;Common pitfalls
- Treating the pipeline as a script. Dataset functions and queries should only define a result. Do not write to external systems, call APIs or rely on execution order inside them. The pipeline may run them more than once or in a different order than you expect.
- Running a full refresh casually. A full refresh clears the target and reprocesses all source data. On a streaming table this discards its checkpoint, and if the source files have been deleted, the data cannot be rebuilt. Protect important tables from full refresh with the
pipelines.reset.allowed = falsetable property. - Reading a materialized view as a stream. Streaming reads need an append-only source. Read streaming tables with
STREAM(...)and use materialized views only in batch queries. - Ignoring out-of-order CDC events. AUTO CDC needs a reliable
sequence_bycolumn. If it is missing or not monotonic, older changes can overwrite newer ones. - Staying in development mode. Development mode keeps the cluster running and disables retries so you can iterate. Production runs should use production mode, usually through a scheduled job.
- Changing code by hand in the workspace. Manual edits drift between environments. Keep source in version control and deploy with Databricks Asset Bundles.
Operate it
- Start with triggered updates. Move to continuous only when the latency requirement justifies keeping compute running all the time.
- Alert on failed updates and on expectation violation counts, not only on job failure.
- Test transformation logic in a small development schema before promoting it.
- Keep a deployment definition for every pipeline so it can be recreated from source.
Exercise
A silver streaming table drops rows with a null order_id. Last week an upstream bug sent many null IDs, but no alert fired and the update succeeded. Which practice would have caught this, and where would you look?
Show answer
Monitor expectation metrics. Dropped rows do not fail the update, so you need to read the data quality information in the event log, for example through flow_progress events from event_log(...), and alert when the violation count rises. If the missing IDs should stop the pipeline, use the fail-update action instead of drop.
Exercise
Why is a casual full refresh risky on a bronze streaming table, and how can you guard against it?
Show answer
A full refresh clears the table and its checkpoint and re-reads the source. If the original files have been removed, the history is lost. Set pipelines.reset.allowed = false on the table to block full refresh.