Batch, streaming, and real-time inference
Databricks Machine Learning Associate · Lesson 17 of 19 · 2:16:46
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 Associate exam. In this lesson: batch, streaming, or real-time inference, inference with pandas, batch inference with a Spark UDF from MLflow, and streaming inference in a Lakeflow pipeline. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Three kinds of predictions
Juniper Rides needs three kinds of predictions: the demand of every station for the next day, a damage score for each trip as it ends, from a stream of trip events, and a price estimate, shown in the app before a ride starts. Each one has a different deadline, and a different cost. This lesson covers how to choose between batch, streaming, and real time inference, how to score pandas data, how to score Spark data in batch, and how to score a stream in a Lakeflow pipeline.
Batch, streaming, or real time
Batch inference scores large volumes of data at set intervals, like every night or every hour, and it accepts a high latency, since nobody waits for one prediction. It is the simplest and cheapest mode, and the predictions land in a table, ready for reports and other jobs. Streaming inference scores records continuously, as they arrive in a stream, with a latency of seconds to minutes, like a damage score for each trip as it ends. Real time inference answers each request within a fraction of a second, through a Model Serving endpoint, like a price shown in the app, and it usually costs the most. So pick the slowest mode that the business accepts: the less urgent the prediction, the cheaper it is to produce.
Inference with pandas
For data that fits in memory, mlflow dot pyfunc dot load_model loads a logged or registered model from its URI, which can name a model in Unity Catalog and an alias, like champion. The predict method of the loaded model takes a pandas DataFrame with the input columns of the signature, and returns the predictions. The pyfunc flavor gives every model the same predict interface, whatever library trained it. This runs on a single machine, the driver of a notebook or a job, so it suits small batches, tests, and checks before deployment. For large tables, the same model must run across the cluster, which is the job of spark_udf.
Batch inference with Spark
mlflow dot pyfunc dot spark_udf wraps the model in a Spark user defined function, which runs it on the executors, in parallel, over a Spark DataFrame. You create it once, from the model URI, then apply it with withColumn, passing the feature columns, often grouped with struct. A Lakeflow job then runs the scoring on a schedule, and writes the predictions to a Delta table in Unity Catalog. When the model was logged with feature tables, score_batch of the Feature Engineering client looks up the features itself, as lesson three showed. And in SQL, the ai_query function can also score a table, with a model served on an endpoint.
Streaming in a Lakeflow pipeline
To score a stream, like trip events arriving from the app, a Lakeflow pipeline can apply the model in a streaming table. At the top of the source file, spark_udf loads the model from its URI, once. A function decorated with dp dot table reads the source with spark dot readStream, and adds a prediction column with the user defined function, so each new trip is scored as it arrives. The same pattern with a materialized view scores a batch table instead. Pipelines do not install MLflow by default, so add MLflow to the environment dependencies of the pipeline.
Recap
Let's recap. Batch inference scores large volumes at intervals, with a high latency, at the lowest cost. Streaming scores records as they arrive, within seconds or minutes, and real time inference answers each request within a fraction of a second, through Model Serving. For pandas data, load_model returns a model whose predict method scores a DataFrame on one machine. For Spark data, spark_udf runs the model across the cluster, and score_batch adds features from feature tables. And a Lakeflow pipeline scores a stream with spark_udf in a dp dot table function, with MLflow in its dependencies.
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.
The app shows riders an estimated price for a trip before they unlock a scooter, and it waits for that estimate. Which mode fits?
- Batch scoring every hour into a table
- Real time, through a Model Serving endpoint
- Streaming inference on the trip events
- A daily job that writes prices to a table
Show the answer
Real time, through a Model Serving endpoint
The app waits for each estimate, so the prediction must come back within a fraction of a second, from a Model Serving endpoint. “Batch scoring every hour into a table” would serve estimates up to an hour old, and cannot price a request that it has never seen. “Streaming inference on the trip events” processes events after they happen, with seconds or minutes of delay. And “A daily job that writes prices to a table” is batch too, with even older estimates.
A data scientist must score 500 million trips with the champion model, every hour. The notebook calls load_model and predict on a pandas DataFrame, and runs out of memory. What fits better?
- A larger driver, with the same pandas code
- A real-time endpoint, called for each trip
- Converting trips to pandas in smaller chunks
- spark_udf on a Spark DataFrame, in a job
Show the answer
spark_udf on a Spark DataFrame, in a job
Spark_udf runs the model on the executors, in parallel, so the whole cluster scores the trips, and a scheduled job runs it every hour. “A larger driver, with the same pandas code” still scores everything on one machine, and only delays the problem. “A real-time endpoint, called for each trip” adds an endpoint and a request per trip, for a task that needs no low latency. And “Converting trips to pandas in smaller chunks” still runs on the driver, one chunk at a time.
A new Lakeflow pipeline that scores trip events fails at the line import mlflow. What is the likely fix?
- Add MLflow to the pipeline’s environment dependencies
- Replace dp.table with dp.materialized_view
- Register the model in the workspace registry
- Call load_model instead of spark_udf
Show the answer
Add MLflow to the pipeline’s environment dependencies
Pipelines do not install MLflow by default, so it must be added to the environment dependencies of the pipeline. “Replace dp.table with dp.materialized_view” changes the kind of dataset, not the missing library. “Register the model in the workspace registry” would install nothing, and Unity Catalog is the recommended registry. And “Call load_model instead of spark_udf” would still need MLflow, and would score on the driver only.