⚠ Errata — read Lesson 12 alongside this
This lesson shows how to build a 5-minute pipeline but skips the prior question of whether you should, and its code assumes classic compute. On serverless, trigger(processingTime=…) is not supported and Trigger.Once() is deprecated. See Lesson 12 — Is Databricks Right for 5-Minute SCADA Data? for the corrected tier guidance and the current recommendation to build this as a Lakeflow pipeline.
In the energy market, new telemetry and market signals can arrive every 5 minutes. This lesson shows how Databricks supports a low-latency forecasting pipeline that ingests frequent updates, refreshes features, and writes a new forecast with a short SLA.
For your use case, real-time means:
Use a fast lane for frequent 5-minute updates and a slow lane for daily model training. The fast lane ingests and scores fresh data; the slow lane retrains models on historical data. Both must share the same Delta-based asset model and data governance.
This is the first important step. A micro-batch pipeline captures every 5-minute payload and writes it to a Delta Bronze table with checkpointing. That keeps the data durable, queryable, and replayable.
from pyspark.sql.functions import col
streaming_df = (
spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/tmp/checkpoints/telemetry_schema")
.option("cloudFiles.inferColumnTypes", "true")
.load("/mnt/data/realtime/telemetry/")
.select(
col("asset_id"),
col("timestamp"),
col("wind_speed"),
col("power_output"),
col("market_price")
)
)
query = (
streaming_df.writeStream
.format("delta")
.option("checkpointLocation", "/tmp/checkpoints/bronze_telemetry")
.outputMode("append")
.trigger(processingTime="5 minutes")
.table("prod.bronze.telemetry")
)
Use a second micro-batch stage that reads the Bronze stream and updates Silver features and forecasts every 5 minutes. A practical pattern is foreachBatch(), which gives you deterministic batch processing plus Delta table writes.
def process_batch(batch_df, batch_id):
# 1) Enrich telemetry with asset metadata
enriched = (
batch_df
.join(spark.table("prod.silver.dim_assets"), "asset_id", "left")
.withColumn("event_time", col("timestamp"))
)
# 2) Create incremental feature table rows
features = compute_features(enriched)
# 3) Score with MLflow model
model = mlflow.pyfunc.load_model("models:/wind-forecast/latest")
scored = model.predict(features.toPandas())
# 4) Write Gold forecast output
to_write = (
features
.withColumn("forecast", lit(scored))
.withColumn("produced_at", current_timestamp())
)
(to_write.write
.format("delta")
.mode("append")
.saveAsTable("prod.gold.forecast_ts"))
query = (
spark.readStream
.table("prod.bronze.telemetry")
.writeStream
.foreachBatch(process_batch)
.option("checkpointLocation", "/tmp/checkpoints/silver_scoring")
.trigger(processingTime="5 minutes")
.start()
)
Note: In production, avoid converting the entire micro-batch to pandas unless the batch size is small. A distributed Spark scoring solution or MLflow model serving endpoint is safer for larger fleets.
model_version and produced_at for traceability.Operational rule
For a 5-minute update cycle, design the pipeline so each run completes well under 5 minutes. That gives you headroom for retries and ensures the next cycle is not delayed.
For your forecast use case, a 5-minute micro-batch pipeline is usually the best fit:
Continuous streaming is useful when latency must be seconds, but it adds complexity in state management, exactly-once joins, and operational overhead. For 5-minute forecast refreshes, micro-batch is the right tradeoff.
| Pattern | Why it matters | Where to use it |
|---|---|---|
| Auto Loader / cloudFiles | Efficient, schema-aware incremental file ingestion | Telemetry files landing in cloud storage |
| Delta Streaming + foreachBatch | Deterministic, repeatable batch processing for scoring | Fast lane Silver + Gold updates |
| MLflow model registry | Versioned models + safe stage promotion | Predict with the same model used in production |
| Checkpointing | Exactly-once delivery and restart safety | Every streaming write to Delta or model scoring job |
You receive telemetry every 5 minutes and update the forecast after each arrival. Which pattern preserves both analytics and production needs?
Why is micro-batch processing usually better than continuous streaming for 5-minute forecast updates?
Your 5-minute forecast pipeline must not miss a cycle. What should you monitor?
📖 Recommended Reading
Databricks Structured Streaming documentation — covers micro-batch triggers, checkpointing, Delta writes, and production streaming patterns for low-latency pipelines.
Now you know how to design the fast lane for 5-minute updates. Ask your teacher to build the next lesson around realtime model serving or streaming feature state in the same energy-market pipeline.
Need an example notebook for 5-minute streaming ingestion and scoring? Ask me to generate one.
Rechtliches: Impressum