Sub topic 17
Sizing compute and weighing Photon's cost
This chapter assumes you can read a query plan, know what Photon is, and have enabled it on a cluster or warehouse. It covers how to size compute and how to decide whether Photon's price is worth the speedup.
Photon changes the price, not just the speed
Photon is not free. On classic compute, a Photon-enabled cluster uses a higher DBU rate than the same cluster without Photon. A DBU (Databricks Unit) is the unit Databricks uses to meter usage. Your bill is roughly:
cost = DBUs per hour × hours running × price per DBU, plus the cloud provider's charge for the virtual machines
The cloud VM charge is the same with or without Photon. Only the DBU part rises. The exact multiplier depends on your cloud, plan and compute type, so read it from the current pricing page or your account's rate card instead of assuming a number.
This gives a simple break-even rule. If Photon raises the DBU rate by a factor m, the job must finish at least m times faster for the DBU part to cost no more. The VM charge works in your favor, because it is billed per hour and a shorter run lowers it too. In practice the break-even speedup is a little below m.
def photon_break_even(dbu_rate_base, dbu_rate_photon, dbu_price, vm_hourly):
"""Return the speedup at which Photon costs the same as the baseline.
Both runs are assumed to use the same cluster shape.
Cost per hour = DBU rate * DBU price + VM price.
"""
base_hourly = dbu_rate_base * dbu_price + vm_hourly
photon_hourly = dbu_rate_photon * dbu_price + vm_hourly
return photon_hourly / base_hourly
# Example numbers only. Replace them with values from your own rate card.
speedup = photon_break_even(
dbu_rate_base=4.0,
dbu_rate_photon=8.0,
dbu_price=0.15,
vm_hourly=1.20,
)
print(f"Photon must be at least {speedup:.2f}x faster to cost the same")With these example numbers the break-even is 1.33x, not 2x, because the VM cost does not double. Your numbers will differ.
What the speedup depends on
Photon speeds up the parts of a query that run in its native, vectorized engine: scans, filters, joins, aggregations and many built-in expressions. The overall gain therefore depends on how much of the runtime is spent in those operators.
| Workload | Typical Photon benefit |
|---|---|
| Large scans, joins and aggregations over Delta or Parquet | Often large |
| Wide tables with many columns and expressions | Often large |
| Queries dominated by Python UDFs | Small, because UDFs run outside Photon |
| Queries dominated by waiting on a small table or an external system | Small |
| Short queries of a few seconds on a small dataset | Small, startup and scheduling dominate |
This is Amdahl's law applied to a query. If only 40% of the time is spent in Photon-supported operators, even an infinitely fast engine cannot save more than 40%.
Sizing compute
Sizing means choosing the node type, the number of workers and whether the cluster scales. Three rules help.
- Match memory to the work. Look at spill in the Query Profile or Spark UI. Spill means data was written to disk because it did not fit in memory. Heavy spill suggests larger nodes or more workers, not a different engine.
- Add workers only when tasks can use them. If a stage has 8 tasks, 64 cores will not make it faster. Check the number of tasks and whether skew leaves a few tasks running long.
- Prefer fewer, larger nodes for shuffle-heavy work. Fewer nodes mean less data moves across the network, though a single node loss then costs more.
Photon tends to be memory-hungry because it keeps data in columnar batches. Memory-optimized node types are often a better fit than general-purpose ones for join-heavy or aggregation-heavy jobs. Test this on your own data.
Autoscaling and idle time
Autoscaling adds and removes workers based on load. It helps with uneven demand but has costs. New workers take minutes to start, and a scale-down can discard cached data. For a batch job with a fixed workload, a fixed-size cluster is simpler and gives repeatable timings, which also helps benchmarking.
Idle time is a hidden cost. A cluster that stays up between jobs bills for every minute. Set auto-termination on interactive clusters, and prefer job compute for scheduled work, since it exists only while the job runs.
SQL warehouses
For SQL warehouses, Photon is part of the product and you do not toggle it. You choose a size (for example, 2X-Small up to larger sizes) and the warehouse type. Each size doubles the compute of the one below it, and the DBU rate roughly doubles as well. A larger size makes sense when a single query is slow because of scan or shuffle volume. Multi-cluster scaling makes sense when many users run queries at the same time and queries wait in a queue.
Cost per job, not cost per hour
A faster, more expensive cluster can still be cheaper per job. Compare total cost for the same work, not hourly rates.
| Run | Hourly cost | Runtime | Cost per run |
|---|---|---|---|
| Baseline, no Photon | $2.40 | 60 min | $2.40 |
| Photon, same shape | $3.60 | 30 min | $1.80 |
| Photon, same shape, small dataset | $3.60 | 50 min | $3.00 |
The numbers are illustrative. The third row shows the failure case: a modest speedup that does not cover the higher rate.
You can also trade the speedup for a smaller cluster. If Photon makes a job twice as fast, the job may run at the original speed on half the workers. This often lowers cost more than finishing early does, but check that spill does not appear after shrinking.
Common errors
- Comparing hourly rates. The decision depends on cost per completed job.
- Judging from one run. The first run includes cold caches and cluster startup. Repeat runs and compare medians.
- Enabling Photon on UDF-heavy jobs. You pay the higher rate and get little in return. Replace the UDFs first, as covered earlier.
- Scaling out to hide skew. Extra workers do not help when one task holds most of the data.
- Leaving clusters running. Idle time can outweigh any engine saving.
A repeatable procedure
- Pick a representative query or job and fix the dataset.
- Run it on the baseline compute, then on the Photon compute with identical shape.
- Record runtime, DBUs consumed and VM cost for each.
- Compute cost per run and compare with the break-even speedup.
- If Photon wins, try a smaller cluster and repeat.
Exercise
A Photon cluster costs 1.5 times as much per hour as the same cluster without Photon. A nightly job takes 40 minutes without Photon and 30 minutes with it. Does Photon lower the cost of the job? Show the calculation.
Show answer
Without Photon the cost is 1.0 × 40 min = 40 units. With Photon it is 1.5 × 30 min = 45 units. Photon costs about 12% more per run, so it does not lower the cost here. The speedup is 1.33x, below the 1.5x needed to break even. It would pay off only if the job finished in 26.7 minutes or less, or if the VM share of the hourly cost, which does not rise with Photon, made the real multiplier lower than 1.5.
Exercise
A Photon job shows heavy spill and one task in a stage runs ten times longer than the others. Which two actions are more likely to help than doubling the number of workers?
Show answer
Address the skew, for example with adaptive query execution skew handling or by changing the join or key, and relieve memory pressure by using larger or memory-optimized nodes. More workers do not help when one task holds most of the data, and they do not fix per-node memory limits.