Jump to content

Spark configuration precedence

From Open Lakehouse Wiki
Revision as of 00:52, 25 November 2026 by Open Lakehouse Wiki sync (talk | contribs) (Sync from open-lakehouse/wiki@7c9abaa)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Spark configuration precedence
Also known as Spark property precedence
Field Distributed data processing
Key parts spark-defaults.conf, --properties-file, --conf, SparkConf, session configuration, SQL SET
Open source implementations Apache Spark
Related spark-submit, Spark Connect, Executor
Last verified 23 November 2026 against the v4.3.0 tag of apache/spark (docs/configuration.md, docs/submitting-applications.md, docs/sql-migration-guide.md, docs/spark-connect-overview.md, SQLConf.scala, StaticSQLConf.scala, classic/RuntimeConfig.scala, python/pyspark/sql/session.py and conf.py) and SPARK-48392.

In Apache Spark, configuration precedence is the order in which Spark merges property values from the defaults file, the spark-submit command line and application code, and the rule that decides which of those values can still change after the application has started.[1] Values set on a SparkConf in code win over --conf flags, which win over spark-defaults.conf.[1] Once the application runs, only runtime SQL configurations can be changed per session, with spark.conf.set or the SQL SET command.[1]

What is the order of precedence for Spark properties?

Spark reads properties from several places when an application starts and merges them. The Spark 4.3.0 configuration guide gives this order, highest first:[1]

Rank Source Example
1 (highest) Properties set directly on a SparkConf or the SparkSession builder in application code SparkSession.builder.config("spark.sql.shuffle.partitions", "64")
2 Flags passed to spark-submit, spark-shell or pyspark: --conf/-c, dedicated flags such as --master or --executor-memory, and a file given with --properties-file --conf spark.eventLog.enabled=true
3 (lowest) conf/spark-defaults.conf under SPARK_HOME or SPARK_CONF_DIR spark.executor.memory 4g

Any property that none of these sources sets keeps its built-in default. The Environment tab of the Spark UI lists only values that were set explicitly through spark-defaults.conf, SparkConf or the command line.[1]

When a configuration key has been renamed in a newer Spark version, Spark still accepts the old name, but any value under the new name takes precedence over the old one.[1]

How do spark-defaults.conf and --properties-file interact?

By default spark-submit reads conf/spark-defaults.conf, a file with one whitespace-separated key and value per line.[1] Setting SPARK_CONF_DIR makes Spark read spark-defaults.conf, spark-env.sh and log4j2.properties from another directory instead of SPARK_HOME/conf.[1]

When a file is passed with --properties-file, Spark no longer loads spark-defaults.conf unless the --load-spark-defaults flag is also given. With both, values in the --properties-file file take precedence over those in spark-defaults.conf.[2][3] The --load-spark-defaults flag was added in Spark 4.0.0 by SPARK-48392.[4] Within rank 2, SparkSubmitArguments at the v4.3.0 tag gives --conf values priority over the --properties-file file, and that file priority over spark-defaults.conf; a dedicated flag such as --master wins over the same property passed as --conf spark.master.[3]

To see which source supplied a value, the Spark documentation recommends running spark-submit with --verbose.[2]

Which properties must be set before the application starts?

Spark's configuration guide divides properties into two kinds. Deploy-related properties, such as spark.driver.memory and spark.executor.instances, may have no effect when set in code at runtime, so the guide recommends setting them in a configuration file or on the spark-submit command line. Runtime-control properties, such as spark.task.maxFailures, can be set either way.[1]

The driver's JVM settings are the most common case. In client deploy mode, spark.driver.memory (default 1g) and spark.driver.extraJavaOptions must not be set through SparkConf in the application, because the driver JVM has already started when that code runs. They must be given with --driver-memory, --driver-java-options or the defaults file instead.[1]

What can change at runtime?

Spark SQL configurations come in two kinds:[1]

  • Runtime SQL configurations are per session and mutable. They take initial values from the defaults file, --conf or the SparkConf used to build the session, and can then be read and changed with spark.conf.get/spark.conf.set, the SQL SET command, and reset with RESET. spark.sql.shuffle.partitions (default 200) is an example.[1][5]
  • Static SQL configurations are shared by all sessions and fixed once the first session starts. They can be read with spark.conf.get or SET key, but not set or unset. spark.sql.extensions and spark.sql.warehouse.dir are static.[1][6]

In Spark 4.3.0, spark.conf.set on a static SQL key fails with the error condition CANNOT_MODIFY_STATIC_CONFIG, and on a Spark core key (one registered as a SparkConf entry, such as spark.executor.memory) it fails with CANNOT_MODIFY_CONFIG.[7] The SQL SET command has rejected Spark core keys since Spark 3.0; before 3.0 it accepted them silently with no effect. The internal flag spark.sql.legacy.setCommandRejectsSparkCoreConfs controls this check: it defaults to true, and setting it to false restores the old behavior.[8][5]

PySpark has a method that answers the question directly: spark.conf.isModifiable(key), added in PySpark 2.4.0, returns whether a key can be changed in the current session.[9]

Example

The following PySpark code runs against Spark 4.3.0 in classic mode. It reads one runtime and one static SQL configuration, changes the runtime one in two equivalent ways, and checks which keys can be changed.

from pyspark.sql import SparkSession

spark = (SparkSession.builder
         .appName("conf-demo")
         .config("spark.sql.shuffle.partitions", "64")   # rank 1: code
         .getOrCreate())

print(spark.conf.get("spark.sql.shuffle.partitions"))   # '64'

spark.conf.set("spark.sql.shuffle.partitions", "32")    # runtime SQL conf: allowed
spark.sql("SET spark.sql.shuffle.partitions=16")        # same effect in SQL

print(spark.conf.isModifiable("spark.sql.shuffle.partitions"))  # True
print(spark.conf.isModifiable("spark.sql.extensions"))          # False (static)

The same changes in Spark SQL:

SET spark.sql.shuffle.partitions=16;   -- change for this session
SET spark.sql.shuffle.partitions;      -- show the current value
RESET spark.sql.shuffle.partitions;    -- back to the value the session started with

The SET command sets, shows or lists SQL configuration properties; SET -v also prints each property's meaning.[10]

How do sessions share configuration?

In PySpark, SparkSession.builder.getOrCreate() returns the existing global default session if there is one, and applies the options given to the builder to that existing session.[11] spark.newSession() returns a session with its own SQL configuration, temporary views and UDFs, sharing the SparkContext and the table cache.[11] Runtime SQL configurations are therefore per session, while Spark core properties belong to the whole application.

How does Spark Connect change this?

A Spark Connect client gets a session on a remote Spark server and has no access to the static Spark configuration or the SparkContext; it cannot change settings that affect every client of that server.[12] In practice a Spark Connect client can set runtime SQL configurations for its own session, and cluster-level properties are set where the server is started.

See also

References

  1. ↑ 1.00 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.10 1.11 1.12 Spark Configuration (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  2. ↑ 2.0 2.1 Submitting Applications (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  3. ↑ 3.0 3.1 SparkSubmitArguments.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  4. ↑ SPARK-48392: Load spark-defaults.conf when passing configurations to spark-submit through --properties-file. Apache Software Foundation JIRA.Retrieved 2026-11-23.
  5. ↑ 5.0 5.1 SQLConf.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  6. ↑ StaticSQLConf.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  7. ↑ classic/RuntimeConfig.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  8. ↑ Migration Guide: SQL, Datasets and DataFrame (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  9. ↑ python/pyspark/sql/conf.py (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  10. ↑ SET (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  11. ↑ 11.0 11.1 python/pyspark/sql/session.py (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  12. ↑ Spark Connect Overview (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.