Apache Spark Big Data (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
What are Broadcast Variables and Accumulators?
senior LevelCode ExampleSystem Design
In Simple Words:
- Broadcast Variable: Sends a read-only lookup table or dictionary to every Executor node once, instead of sending a copy with every single task. Drastically reduces network serialization traffic.
- Accumulator: Write-only shared variable updated across tasks via associative addition (e.g. counting corrupted rows or tracking metrics across a distributed pipeline).
python
# Broadcast a small lookup table:
lookup_dict = {"US": "United States", "DE": "Germany", "JP": "Japan"}
broadcast_countries = spark.sparkContext.broadcast(lookup_dict)
def map_country(code):
return broadcast_countries.value.get(code, "Unknown")