<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Broadcast_variable</id>
	<title>Broadcast variable - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Broadcast_variable"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Broadcast_variable&amp;action=history"/>
	<updated>2026-12-06T00:07:43Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Broadcast_variable&amp;diff=127&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Broadcast_variable&amp;diff=127&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:35Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Broadcast variable&lt;br /&gt;
| also_known_as   = &amp;lt;code&amp;gt;SparkContext.broadcast&amp;lt;/code&amp;gt;, shared read-only variable&lt;br /&gt;
| field           = Distributed data processing&lt;br /&gt;
| key_parts       = Driver-side value, chunked BitTorrent-like distribution, per-executor cached copy, &amp;lt;code&amp;gt;unpersist()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;destroy()&amp;lt;/code&amp;gt;&lt;br /&gt;
| implementations = [[Apache Spark]]&lt;br /&gt;
| related         = [[Accumulator]], [[Broadcast join]], [[Join strategy hints]], [[Spark Connect]], [[RDD]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (docs/rdd-programming-guide.md, docs/tuning.md, docs/configuration.md, docs/spark-connect-overview.md, docs/sql-performance-tuning.md, core broadcast/TorrentBroadcast.scala, core unsafe/map/BytesToBytesMap.java, sql BroadcastExchangeExec.scala, SQLConf.scala, python/pyspark/sql/connect/session.py) and the PySpark 4.3.0 Broadcast.destroy API reference}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]], a &amp;#039;&amp;#039;&amp;#039;broadcast variable&amp;#039;&amp;#039;&amp;#039; 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&amp;#039;s two kinds of shared variable; the other is the [[accumulator]].&amp;lt;ref name=&amp;quot;rddguide&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/rdd-programming-guide.html#broadcast-variables |title=RDD Programming Guide: Broadcast Variables (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Broadcast variables are part of the &amp;lt;code&amp;gt;SparkContext&amp;lt;/code&amp;gt; API and are not available to [[Spark Connect]] clients; DataFrame code uses a [[broadcast join]] instead.&amp;lt;ref name=&amp;quot;connect&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/spark-connect-overview.html |title=Spark Connect Overview (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What is a broadcast variable used for? ==&lt;br /&gt;
A function passed to a Spark operation such as &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; 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.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt; 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.&amp;lt;ref name=&amp;quot;tuning&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/tuning.html |title=Tuning Spark (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
rates = {&amp;quot;EUR&amp;quot;: 1.08, &amp;quot;GBP&amp;quot;: 1.27, &amp;quot;JPY&amp;quot;: 0.0067}&lt;br /&gt;
b_rates = spark.sparkContext.broadcast(rates)&lt;br /&gt;
&lt;br /&gt;
usd = orders_rdd.map(lambda o: (o.id, o.amount * b_rates.value[o.currency]))&lt;br /&gt;
total = usd.map(lambda kv: kv[1]).sum()   # action: the map runs now and reads b_rates&lt;br /&gt;
&lt;br /&gt;
b_rates.unpersist()   # free executor copies; b_rates is re-sent if used again&lt;br /&gt;
refunds_usd = refunds_rdd.map(lambda r: r.amount * b_rates.value[r.currency]).collect()&lt;br /&gt;
&lt;br /&gt;
b_rates.destroy()     # last step: free everything; b_rates can no longer be used&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The value is read with &amp;lt;code&amp;gt;.value&amp;lt;/code&amp;gt;. It should not be changed after it is broadcast, so that every node sees the same value.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt; Because transformations such as &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; are lazy and run only when an action needs a result, the broadcast value is read when the action runs, not when &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; is called.&amp;lt;ref name=&amp;quot;rddops&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/rdd-programming-guide.html#rdd-operations |title=RDD Programming Guide: RDD Operations (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; &amp;lt;code&amp;gt;unpersist()&amp;lt;/code&amp;gt; releases the copies on executors, and a later use broadcasts the value again. &amp;lt;code&amp;gt;destroy()&amp;lt;/code&amp;gt; releases all resources, after which the variable cannot be used. Neither blocks by default.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt; The PySpark API reference says to use &amp;lt;code&amp;gt;destroy()&amp;lt;/code&amp;gt; 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.&amp;lt;ref name=&amp;quot;destroy&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/api/python/reference/api/pyspark.Broadcast.destroy.html |title=pyspark.Broadcast.destroy (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How is a broadcast variable distributed? ==&lt;br /&gt;
Spark&amp;#039;s default implementation, &amp;lt;code&amp;gt;TorrentBroadcast&amp;lt;/code&amp;gt;, 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.&amp;lt;ref name=&amp;quot;torrent&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/core/src/main/scala/org/apache/spark/broadcast/TorrentBroadcast.scala |title=TorrentBroadcast.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Two settings control the transfer: &amp;lt;code&amp;gt;spark.broadcast.blockSize&amp;lt;/code&amp;gt; (default &amp;lt;code&amp;gt;4m&amp;lt;/code&amp;gt;) is the chunk size, and &amp;lt;code&amp;gt;spark.broadcast.compress&amp;lt;/code&amp;gt; (default true) compresses broadcast data with &amp;lt;code&amp;gt;spark.io.compression.codec&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;config&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/configuration.html |title=Spark Configuration (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How large can a broadcast be? ==&lt;br /&gt;
The Spark docs set no specific maximum for &amp;lt;code&amp;gt;SparkContext.broadcast&amp;lt;/code&amp;gt;. The practical limit is memory: the value is created on the driver, and each machine keeps a cached copy.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt; &amp;lt;code&amp;gt;SizeEstimator.estimate&amp;lt;/code&amp;gt; can show how much heap a broadcast value will take on each executor.&amp;lt;ref name=&amp;quot;tuning&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Broadcast joins in Spark SQL have explicit limits. Spark plans one automatically when a table is under &amp;lt;code&amp;gt;spark.sql.autoBroadcastJoinThreshold&amp;lt;/code&amp;gt; (default 10 MB), waits up to &amp;lt;code&amp;gt;spark.sql.broadcastTimeout&amp;lt;/code&amp;gt; (default 300 seconds) for the broadcast, and fails a broadcast table of 8 GiB or more (&amp;lt;code&amp;gt;spark.sql.maxBroadcastTableSize&amp;lt;/code&amp;gt;, added in 4.1.0) or with too many rows: 2&amp;lt;sup&amp;gt;29&amp;lt;/sup&amp;gt; / 1.5 (357,913,941) rows for a hash join whose key is not a single &amp;lt;code&amp;gt;LongType&amp;lt;/code&amp;gt; column, and 512,000,000 rows otherwise.&amp;lt;ref name=&amp;quot;btbm&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/core/src/main/java/org/apache/spark/unsafe/map/BytesToBytesMap.java |title=BytesToBytesMap.java (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;perf&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-performance-tuning.html |title=Performance Tuning (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;sqlconf&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala |title=SQLConf.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;bee&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/execution/exchange/BroadcastExchangeExec.scala |title=BroadcastExchangeExec.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Are broadcast variables supported in Spark Connect? ==&lt;br /&gt;
No. A Spark Connect client talks to the server through logical plans and has no access to the &amp;lt;code&amp;gt;SparkContext&amp;lt;/code&amp;gt;; the Spark docs list &amp;lt;code&amp;gt;SparkContext&amp;lt;/code&amp;gt; and RDDs among the APIs that Spark Connect does not support.&amp;lt;ref name=&amp;quot;connect&amp;quot; /&amp;gt; In the PySpark Connect client, reading &amp;lt;code&amp;gt;spark.sparkContext&amp;lt;/code&amp;gt; raises &amp;lt;code&amp;gt;JVM_ATTRIBUTE_NOT_SUPPORTED&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;csession&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/python/pyspark/sql/connect/session.py |title=connect/session.py (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The DataFrame equivalent works in both classic Spark and Spark Connect: put the lookup data in a small DataFrame and join with a &amp;lt;code&amp;gt;BROADCAST&amp;lt;/code&amp;gt; [[join strategy hints|hint]] or &amp;lt;code&amp;gt;pyspark.sql.functions.broadcast()&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from pyspark.sql.functions import broadcast&lt;br /&gt;
&lt;br /&gt;
rates_df = spark.createDataFrame(list(rates.items()), [&amp;quot;currency&amp;quot;, &amp;quot;rate&amp;quot;])&lt;br /&gt;
usd = orders.join(broadcast(rates_df), &amp;quot;currency&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Broadcast variable compared with broadcast join ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! !! Broadcast variable !! Broadcast join&lt;br /&gt;
|-&lt;br /&gt;
! API&lt;br /&gt;
| &amp;lt;code&amp;gt;SparkContext.broadcast&amp;lt;/code&amp;gt; (RDD and UDF code)&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
| DataFrame / SQL join with a broadcast hint or automatic planning&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Spark Connect&lt;br /&gt;
| Not available&amp;lt;ref name=&amp;quot;connect&amp;quot; /&amp;gt;&lt;br /&gt;
| Available&lt;br /&gt;
|-&lt;br /&gt;
! Size limits&lt;br /&gt;
| None documented; bounded by driver and executor memory&lt;br /&gt;
| 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&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;bee&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Typical use&lt;br /&gt;
| Lookup tables or models inside RDD functions or UDFs&lt;br /&gt;
| Joining a large table with a small one without shuffling the large one&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Accumulator]]&lt;br /&gt;
* [[Broadcast join]]&lt;br /&gt;
* [[Join strategy hints]]&lt;br /&gt;
* [[Spark Connect]]&lt;br /&gt;
* [[Spark memory management]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>