Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Databricks · Databricks Machine Learning Professional

Ray Tune, and choosing Ray or Spark

Databricks Machine Learning Professional · Lesson 7 of 22 · 47:03

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.

In this lesson

Prepare for the Databricks Certified Machine Learning Professional exam. In this lesson: Ray on Databricks clusters, Ray Tune search spaces and trials, schedulers such as ASHA that stop weak trials early, logging trials to MLflow, and when to choose Ray or Spark. Includes three original practice questions.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Heavy trials

Pebblecove's fraud team trains a deep learning model on card payments, and wants to tune its learning rate and its number of layers. Each trial is a long training run that keeps the processors busy, and the weak ones should stop early. This lesson covers Ray on Databricks, Ray Tune, and how to choose between Ray and Spark.

Ray on Databricks

Ray is an open source framework for scaling Python applications, with libraries like Ray Data, Ray Train, and Ray Tune. On Databricks, it comes preinstalled in the machine learning runtime, and it runs on top of a Spark cluster. You start it from a notebook with setup_ray_cluster, giving the minimum and maximum number of worker nodes, and the CPUs or GPUs per node, then you call ray dot init to connect. The Ray cluster shuts down when the notebook detaches, or after thirty minutes without a task.

Ray Tune

Ray Tune is the library of Ray for hyperparameter tuning at any scale. You define a trainable, a function that receives a config with the values of one trial, trains the model, and reports its metrics with tune dot report. The param_space describes the search space, with functions like tune dot loguniform, tune dot choice, or tune dot grid_search. Then a Tuner takes the trainable, the param_space, and a TuneConfig with the metric, the mode, min or max, and num_samples, the number of trials, and fit runs them, while get_best_result returns the best one.

Schedulers and MLflow

Two settings of TuneConfig decide how smart the search is. The search_alg chooses the next values, and Ray Tune can use Optuna, Hyperopt, or Bayesian optimization through it, instead of the default random and grid search. The scheduler decides which trials keep running: the default runs every trial to the end, while ASHA, HyperBand, or population based training stop weak trials early, or adapt them. For MLflow, start a parent run on the driver, let each trial log a child run, or use the MLflow logger callback, and write big artifacts as files in a volume.

Ray or Spark

So when should you choose Ray, and when Spark? Spark excels at data parallelism: the same operation applied to each part of a large dataset, as in joins, aggregations, feature engineering, and Spark ML training. Ray excels at task parallelism: many independent tasks that run at the same time, each focused on computation, like hyperparameter search, deep learning training, simulation, or reinforcement learning. The two work together: Spark reads and prepares the data, ray dot data dot from_spark hands it to Ray, and Ray runs the heavy computation, while models go to MLflow and tables to Unity Catalog. And a large extract, transform, and load stage can run as a separate task of the job, on a cluster sized for it, so the two engines do not compete for resources.

Recap

Let's recap. On Databricks, setup_ray_cluster starts Ray on the workers of a cluster, and ray dot init connects to it. Ray Tune runs a trainable for each trial, with a Tuner, a param_space, and a TuneConfig. A search algorithm chooses the values, and a scheduler like ASHA stops weak trials early. MLflow gets a parent run on the driver, and a child run for each trial. And Spark excels at data parallelism, Ray at task parallelism, and the two combine well.

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.

  1. A notebook needs a Ray cluster on four worker nodes of its Databricks cluster. Which steps start it?

    • ray.init() alone, on the driver
    • spark.conf.set("ray.workers", 4)
    • setup_ray_cluster(...), then ray.init()
    • %pip install ray, then restart Python
    Show the answer

    setup_ray_cluster(...), then ray.init()

    Setup_ray_cluster starts Ray on the workers, here with a minimum and a maximum of four nodes, and ray dot init connects the notebook to it. “ray.init() alone, on the driver” would only start Ray locally on the driver, without the workers. “spark.conf.set("ray.workers", 4)” is not a Ray setting at all. And “%pip install ray, then restart Python” is not needed, since the machine learning runtime already includes Ray.

  2. Each trial trains for fifty epochs, and most trials are clearly weak after five. Which TuneConfig setting saves the most compute?

    • search_alg=OptunaSearch()
    • num_samples=500
    • mode="min"
    • scheduler=ASHAScheduler()
    Show the answer

    scheduler=ASHAScheduler()

    ASHA is a scheduler that stops the weak trials early, so their remaining epochs are never paid for. “search_alg=OptunaSearch()” only chooses better values for the next trials, and lets each one run to the end. “num_samples=500” would add more trials, which raises the cost. And “mode="min"” only says whether the metric is minimized or maximized.

  3. A job joins and aggregates two billion rows into features, then runs 300 independent simulations of pricing strategies. Which split fits?

    • Ray for both stages
    • Spark for the features, Ray for the simulations
    • Spark for both stages
    • Ray for the features, Spark for the simulations
    Show the answer

    Spark for the features, Ray for the simulations

    The joins and aggregations are data parallel work, where Spark excels, and the independent simulations are task parallel work, where Ray excels. “Ray for both stages” would give the large joins to Ray, which Spark handles better. “Spark for both stages” is a weaker fit for many independent tasks that are heavy on computation. And “Ray for the features, Spark for the simulations” would swap the two strengths.