Skip to the page
Tecyfy

PySpark interview question · easy

What are accumulators and broadcast variables in Spark?

The short answerAccumulators and broadcast variables are Spark's two kinds of shared variables.

The full answer

Accumulators and broadcast variables are Spark's two kinds of shared variables. An accumulator lets tasks add to a value that only the driver can read, such as a count of bad records. A broadcast variable sends a read-only value, such as a lookup table, to each executor once instead of with every task.

Accumulators Tasks can only add to them; the driver reads the result after an action.

bad_rows = spark.sparkContext.accumulator(0)

def check(row):
    if row["amount"] is None:
        bad_rows.add(1)

orders.foreach(check)          # an action
print(bad_rows.value)          # read on the driver
  • Built-in numeric accumulators exist; Scala and Java also have collection accumulators, and you can define custom ones (AccumulatorParam in Python, AccumulatorV2 in Scala).
  • Reliability: updates made inside actions are applied exactly once per task. Updates made inside transformations (map, filter, a UDF) can be applied more than once if a task or stage is re-executed, or not at all if the transformation is never computed, so use them there only for rough debugging counts.
  • They are lazy: nothing is counted until an action runs.
  • For data quality counts in DataFrame code, a separate aggregation (df.filter(...).count()) or metrics written to a table are usually clearer.

Broadcast variables A read-only value cached on each executor.

rates = {"EUR": 1.0, "USD": 0.92, "GBP": 1.17}
b_rates = spark.sparkContext.broadcast(rates)

from pyspark.sql import functions as F, types as T
@F.udf(T.DoubleType())
def to_eur(amount, ccy):
    return amount * b_rates.value.get(ccy, 0.0) if amount is not None else None

orders.withColumn("amount_eur", to_eur("amount", "currency"))
b_rates.unpersist()            # when no longer needed
  • Spark sends the value efficiently (in pieces, peer to peer), once per executor, instead of serialising it into every task's closure.
  • Values must fit in executor and driver memory and must not be modified.
  • unpersist() removes copies from executors; destroy() releases them everywhere.

The DataFrame equivalent: broadcast joins For lookup tables, the idiomatic DataFrame approach is a broadcast join rather than a broadcast variable in a UDF:

rates_df = spark.createDataFrame(list(rates.items()), ["currency", "rate"])
orders.join(F.broadcast(rates_df), "currency", "left")

This keeps the work inside Spark's optimised engine. Spark broadcasts small tables automatically (below 10 MB by default).

Environment note Both are part of the classic SparkContext API. Spark Connect and serverless environments that use it (such as Databricks serverless compute) don't support sparkContext accumulators and broadcast variables; use aggregations and broadcast joins there.

Last updated 16 October 2026

Sources

Where each fact comes from

  1. 1RDD programming guide