Broadcast variable
| Also known as | SparkContext.broadcast, shared read-only variable
|
|---|---|
| Field | Distributed data processing |
| Key parts | Driver-side value, chunked BitTorrent-like distribution, per-executor cached copy, unpersist() and destroy()
|
| Open source implementations | Apache Spark |
| Related | Accumulator, Broadcast join, Join strategy hints, Spark Connect, RDD |
In Apache Spark, a broadcast variable is a read-only value that the driver sends once to every executor, where it is cached and shared by all tasks, instead of being shipped again with every task. It is one of Spark's two kinds of shared variable; the other is the accumulator.[1] Broadcast variables are part of the SparkContext API and are not available to Spark Connect clients; DataFrame code uses a broadcast join instead.[2]
What is a broadcast variable used for?
A function passed to a Spark operation such as map normally gets its own copy of every variable it uses, shipped with each task. When tasks use a large object from the driver, such as a lookup table, a broadcast variable sends it to each executor once.[1] The Spark tuning guide says that tasks larger than about 20 KiB are probably worth optimizing, and that turning a large driver-side object into a broadcast variable reduces the size of each serialized task and the cost of launching a job.[3]
Spark already broadcasts the common data that tasks within one stage need. An explicit broadcast variable is therefore useful mainly when tasks in several stages need the same data, or when keeping the data in deserialized form matters.[1]
Example
rates = {"EUR": 1.08, "GBP": 1.27, "JPY": 0.0067}
b_rates = spark.sparkContext.broadcast(rates)
usd = orders_rdd.map(lambda o: (o.id, o.amount * b_rates.value[o.currency]))
total = usd.map(lambda kv: kv[1]).sum() # action: the map runs now and reads b_rates
b_rates.unpersist() # free executor copies; b_rates is re-sent if used again
refunds_usd = refunds_rdd.map(lambda r: r.amount * b_rates.value[r.currency]).collect()
b_rates.destroy() # last step: free everything; b_rates can no longer be used
The value is read with .value. It should not be changed after it is broadcast, so that every node sees the same value.[1] Because transformations such as map are lazy and run only when an action needs a result, the broadcast value is read when the action runs, not when map is called.[4] unpersist() releases the copies on executors, and a later use broadcasts the value again. destroy() releases all resources, after which the variable cannot be used. Neither blocks by default.[1] The PySpark API reference says to use destroy() with caution, because a destroyed broadcast variable cannot be used again; it therefore belongs after the last action that reads the variable, including actions on RDDs built earlier.[5]
How is a broadcast variable distributed?
Spark's default implementation, TorrentBroadcast, is BitTorrent-like. The driver splits the serialized value into chunks and stores them in its block manager. Each executor fetches the chunks it lacks from the driver or from other executors that already have them, then stores them so that more executors can fetch from it. This stops the driver from being the bottleneck when every executor needs a copy.[6]
Two settings control the transfer: spark.broadcast.blockSize (default 4m) is the chunk size, and spark.broadcast.compress (default true) compresses broadcast data with spark.io.compression.codec.[7]
How large can a broadcast be?
The Spark docs set no specific maximum for SparkContext.broadcast. The practical limit is memory: the value is created on the driver, and each machine keeps a cached copy.[1] SizeEstimator.estimate can show how much heap a broadcast value will take on each executor.[3]
Broadcast joins in Spark SQL have explicit limits. Spark plans one automatically when a table is under spark.sql.autoBroadcastJoinThreshold (default 10 MB), waits up to spark.sql.broadcastTimeout (default 300 seconds) for the broadcast, and fails a broadcast table of 8 GiB or more (spark.sql.maxBroadcastTableSize, added in 4.1.0) or with too many rows: 229 / 1.5 (357,913,941) rows for a hash join whose key is not a single LongType column, and 512,000,000 rows otherwise.[8][9][10][11]
Are broadcast variables supported in Spark Connect?
No. A Spark Connect client talks to the server through logical plans and has no access to the SparkContext; the Spark docs list SparkContext and RDDs among the APIs that Spark Connect does not support.[2] In the PySpark Connect client, reading spark.sparkContext raises JVM_ATTRIBUTE_NOT_SUPPORTED.[12]
The DataFrame equivalent works in both classic Spark and Spark Connect: put the lookup data in a small DataFrame and join with a BROADCAST hint or pyspark.sql.functions.broadcast().[9]
from pyspark.sql.functions import broadcast
rates_df = spark.createDataFrame(list(rates.items()), ["currency", "rate"])
usd = orders.join(broadcast(rates_df), "currency")
Broadcast variable compared with broadcast join
| Broadcast variable | Broadcast join | |
|---|---|---|
| API | SparkContext.broadcast (RDD and UDF code)[1]
|
DataFrame / SQL join with a broadcast hint or automatic planning[9] |
| Spark Connect | Not available[2] | Available |
| Size limits | None documented; bounded by driver and executor memory | 10 MB automatic threshold; hard limits of 8 GiB and a row limit of 357,913,941 or 512,000,000 rows, depending on the join key[9][11] |
| Typical use | Lookup tables or models inside RDD functions or UDFs | Joining a large table with a small one without shuffling the large one |
See also
References
- ↑ 1.0 1.1 1.2 1.3 1.4 1.5 1.6 RDD Programming Guide: Broadcast Variables (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 2.0 2.1 2.2 Spark Connect Overview (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 3.0 3.1 Tuning Spark (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ RDD Programming Guide: RDD Operations (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ pyspark.Broadcast.destroy (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ TorrentBroadcast.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Configuration (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ BytesToBytesMap.java (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 9.0 9.1 9.2 9.3 Performance Tuning (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ SQLConf.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 11.0 11.1 BroadcastExchangeExec.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ connect/session.py (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.