Sub topic 14
Photon and the runtime engine setting
This chapter assumes you know how a cluster is built (driver, workers, executors) and how Databricks bills a cluster as DBUs plus cloud infrastructure. It also assumes you can read a Spark query plan at a basic level. Here you will learn what Photon changes, what it does not change, and how to decide whether to turn it on.
What Photon is
Photon is a query engine written in C++ that runs inside the Databricks Runtime. It replaces the JVM-based execution of many Spark SQL and DataFrame operators with native, vectorized code. Vectorized means the engine processes batches of column values at once instead of one row at a time.
Photon does not change your code, and it does not change the cluster layout. The driver, workers, executors, and task slots work as before. The same Spark plan is produced, and Photon takes over the operators it supports.
The setting that controls Photon is the runtime engine. It has two values:
STANDARD: the regular Databricks Runtime engine.PHOTON: Photon is on for operators that support it.
In the Clusters API this is the runtime_engine field. In the cluster UI it appears as a Photon acceleration checkbox.
{
"cluster_name": "etl-photon",
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"runtime_engine": "PHOTON",
"autoscale": {
"min_workers": 2,
"max_workers": 8
},
"autotermination_minutes": 30
}This is the autoscaling specification from earlier in the tutorial with one added line. Sizing, autoscaling bounds, and auto-termination all work the same way.
What Photon speeds up
Photon helps most with work that scans, filters, joins, and aggregates large tables. Typical wins are:
- Reading and writing Delta and Parquet data.
- Aggregations over many rows, such as
GROUP BYon large fact tables. - Joins and sorts on large inputs.
- Wide tables with many columns.
- SQL-heavy ETL and BI queries.
Photon helps little or not at all with:
- Python or Scala user-defined functions (UDFs). They run outside the native engine.
- RDD code.
- Very small queries, where startup and scheduling time dominate.
- Workloads limited by something else, such as slow external APIs, skewed partitions, or a single overloaded driver.
Photon speeds up execution, not waiting. If a job spends most of its time on one straggler task caused by data skew, a faster engine shortens only the part that was already fast.
What Photon costs
Photon clusters bill at a higher DBU rate than the same cluster on the standard engine. The cloud infrastructure cost for the same instances is unchanged. So the question is whether the job finishes enough faster to offset the higher rate.
A simple way to compare is total cost per run:
cost per run = (DBU rate x DBUs/hour x hours) + (instance price x nodes x hours)If Photon cuts runtime sharply, both terms shrink because hours falls. The infrastructure term always benefits. The DBU term benefits only if the speedup is larger than the rate premium. Check your own workspace's current DBU rates and run the job both ways, instead of relying on a general speedup number.
Photon and cluster sizing
Photon changes how many cores you need for a given job. Because each core does more work, a Photon cluster often finishes the same job with fewer workers, or finishes sooner with the same workers. Treat your starting size from the earlier chapter as a first guess and re-measure.
Some points to keep in mind:
- Memory use differs. Photon uses off-heap memory for its data structures. A node type that was comfortable on the standard engine can still be right, but watch for spill (data written to disk because memory ran out) in the Spark UI after switching.
- Autoscaling bounds may shift. If jobs finish sooner, the cluster may spend less time at its maximum. Revisit
max_workersafter a few runs. - Node types matter. Photon runs only on supported instance types. GPU instances, for example, do not use Photon. If your cluster is an ML cluster built on GPU nodes, the setting does not apply to it.
- Parallelism is unchanged. Task slots, shuffle partitions, and adaptive query execution behave as before. The partition guidance from the earlier chapter still holds.
Checking that Photon is actually running
Enabling Photon does not guarantee that every operator runs in it. Photon takes over operators it supports and leaves the rest to the standard engine. A plan can mix both.
You can see this in the query plan. This notebook cell builds a small aggregation and prints its plan:
from pyspark.sql import functions as F
df = (
spark.range(0, 10_000_000)
.withColumn("bucket", F.col("id") % 100)
.groupBy("bucket")
.agg(F.count("*").alias("rows"), F.sum("id").alias("total"))
)
df.explain()On a Photon cluster, the plan contains operator names that start with Photon, such as PhotonGroupingAgg. If you see only the usual operator names, that part of the query did not run in Photon. In the Spark UI, the SQL/DataFrame tab shows the same information in the query graph, and it lets you spot which stages fell back to the standard engine.
Deciding whether to enable it
In practice, the decision often reduces to these cases:
| Situation | Recommendation |
|---|---|
| Large SQL or Delta ETL | Test Photon first. It is the most likely to pay off. |
| BI and ad hoc SQL on big tables | Photon usually helps latency and concurrency. |
| Python UDF-heavy transformations | Stay on standard, or rewrite UDFs as built-in functions first. |
| GPU machine learning training | Standard. Photon does not apply. |
| Small, short jobs | Often no gain. Compare cost per run. |
Rewriting a UDF as built-in Spark functions is often the best change of all. It speeds up the job on either engine and lets Photon cover more of the plan.
Enforcing the engine choice with a policy
Teams that have measured a benefit often want every job cluster to use Photon. A cluster policy can fix the value so users cannot change it:
{
"runtime_engine": {
"type": "fixed",
"value": "PHOTON"
},
"autoscale.max_workers": {
"type": "range",
"maxValue": 8,
"defaultValue": 4
}
}This pairs the engine choice with the worker cap from the policies chapter. Use a fixed rule only after measuring. If one team runs GPU or UDF-heavy work under the same policy, a fixed Photon rule will cost them more without helping.
Common mistakes
- Turning Photon on everywhere. The higher DBU rate wastes money on UDF-heavy or tiny jobs.
- Never testing it. SQL-heavy pipelines can leave a large speedup unused.
- Keeping the old size. After enabling Photon, workers that were needed before may now sit idle. Re-measure utilization.
- Ignoring fallback. A plan with few
Photonoperators gets little benefit. Look at the plan before judging the result.
Exercise
A nightly job runs on a standard-engine cluster and takes 60 minutes. You switch to Photon and it takes 40 minutes, but the DBU rate for the cluster is 1.5 times higher. Ignoring infrastructure cost, did the DBU cost go up or down? What else should you check before keeping Photon?
Show answer
The DBU cost is proportional to rate times hours. Before: 1.0 x 60 = 60 units. After: 1.5 x 40 = 60 units. The DBU cost is unchanged. The infrastructure cost falls because the instances run 20 minutes less, so the total cost per run is lower. Before keeping Photon, check the query plan for Photon operators (to confirm the speedup comes from Photon and not from a warm cache), run each variant more than once, and look in the Spark UI for spill or idle workers so you can lower the cluster size.