Scoring Spark ML models: batch, streaming, or real time
Databricks Machine Learning Professional · Lesson 3 of 22 · 16:13
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Databricks Certified Machine Learning Professional exam. In this lesson: the three ways to score, batch scoring with transform and with a Spark UDF from MLflow, streaming scoring with a pipeline model, real-time inference with Model Serving, and choosing a Spark ML or a single-node model for each case. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Three ways to score
Every night, Pebblecove scores the day's new bookings for cancellation risk, but the product team now wants each score within a minute of the booking, and the fraud team wants a score inside the payment request itself. That is one idea, prediction, served in three ways. This lesson covers batch and streaming scoring with Spark ML, and how to choose a Spark ML model or a single node model for each kind of inference.
Batch scoring
In batch, a job loads the fitted PipelineModel, reads the new rows, and calls transform, which adds a prediction column, and a probability column for a classifier. With MLflow, mlflow dot spark dot load_model loads the logged PipelineModel, and its URI can name a version or an alias of the registered model, like champion. The scored DataFrame is then written to a Delta table, which reports and other jobs can read. A scheduled job runs it, every night or every hour, and batch is the cheapest mode for large volumes. And a single node model can score in batch too, because mlflow dot pyfunc dot spark_udf wraps it as a Spark UDF, which the cluster applies in parallel.
Streaming scoring
In streaming, the same PipelineModel transforms a streaming DataFrame, read with spark dot readStream from a Delta table or from Kafka. Most stages work the same way on a stream, so transform scores each micro batch as it arrives. writeStream then writes the scores to a Delta table, with a checkpoint location, so that the query resumes where it stopped after a failure. Streaming gives fresh scores within seconds or minutes, for a continuous cost, and the availableNow trigger processes what has arrived and then stops, which turns the stream into an incremental batch.
Real time inference
Real time means one request at a time, like a payment that waits for its fraud score, with a latency target of a few tens of milliseconds. Model Serving answers such requests from an HTTP endpoint, which scales with the traffic. Each request carries only a handful of rows, and a Spark job has a fixed cost of planning and scheduling that brings nothing to a single row. So for real time, the usual choice is a light single node model, like scikit-learn or XGBoost, logged with MLflow and served on an endpoint.
Choosing the model
Here is how the choice works out. For batch, a Spark ML model with transform fits large volumes, and so does a single node model wrapped by spark_udf. For streaming, the same two options apply, on a streaming DataFrame. For real time, choose a single node model on Model Serving. And when the training data allows a single node library, one model can serve all three modes: spark_udf for batch and streaming, and an endpoint for real time.
Recap
Let's recap. In batch, load the PipelineModel with MLflow, by version or alias, call transform, and write the scores to a Delta table. In streaming, transform a DataFrame read with readStream, and write it with writeStream and a checkpoint location. Real time means one request at a time with a tight latency target, served by Model Serving. Choose a Spark ML model or spark_udf for batch and streaming, and a light single node model for real time. And spark_udf lets one single node model score at cluster scale.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
A streaming query scores new bookings into a Delta table. After a cluster restart, it must continue from the last processed bookings. What makes this possible?
- A longer trigger interval
- A checkpoint location on writeStream
- Calling transform on the full table
- An alias on the registered model
Show the answer
A checkpoint location on writeStream
The checkpoint location stores the progress of the query, so a restarted query resumes where it stopped. “A longer trigger interval” only changes how often micro batches run. “Calling transform on the full table” would score the whole table again, which is what the team wants to avoid. And “An alias on the registered model” would change which model version is loaded, not the progress of the stream.
The fraud team needs a score inside each card payment, within 50 milliseconds. Which option fits?
- A PipelineModel in a streaming query
- A batch job each night
- A single node model on Model Serving
- spark_udf in a job every minute
Show the answer
A single node model on Model Serving
A light single node model on a Model Serving endpoint answers each payment request within milliseconds. “A PipelineModel in a streaming query” would score micro batches, which adds delay, and the payment cannot wait for a stream. “A batch job each night” would score the payment hours too late. And “spark_udf in a job every minute” would run every minute, which is far slower than fifty milliseconds.
A single node XGBoost model, logged with MLflow, must score 200 million bookings every night. How can the job use the whole cluster?
- load_model on the driver, in a loop
- Rebuild it as a Spark ML model first
- Call its serving endpoint for each row
- Wrap it with mlflow.pyfunc.spark_udf
Show the answer
Wrap it with mlflow.pyfunc.spark_udf
Spark_udf wraps the logged model as a Spark UDF, so the executors score the rows in parallel. “load_model on the driver, in a loop” would loop on the driver alone, and two hundred million rows would not even fit there. “Rebuild it as a Spark ML model first” would mean building a new model, when the current one only needs to run in parallel. And “Call its serving endpoint for each row” would send two hundred million requests to an endpoint, which costs far more than a batch job.