Skip to main content

What problem does Spark Connect solve?

Summary

  • Spark Connect solves the tight coupling between a Spark application and the Spark driver. In the classic architecture, client code runs on the driver, so one application's memory errors or dependency clashes can affect everyone sharing the cluster.
  • It replaces that with a decoupled client-server architecture: a thin client sends unresolved logical plans, encoded as protocol buffers, to the Spark server over gRPC, and results stream back as Apache Arrow-encoded row batches.
  • The Spark docs name the operational wins: applications run in their own processes, the driver can be upgraded independently of applications, and developers can debug interactively from an IDE.
  • Spark Connect arrived in Apache Spark 3.4 for PySpark and Spark 3.5 for Scala. Spark 4.0 added a lightweight Python client, pyspark-client, and the spark.api.mode configuration.
  • It has limits: SparkContext and RDD APIs aren't supported, and Spark Connect has no built-in authentication, so you secure it with an authenticating proxy.

What problem does Spark Connect solve?

Spark Connect solves the problem of Spark applications being welded to the Spark driver. In classic Spark, the driver is monolithic: your application runs on top of the scheduler, optimizer and analyzer in the same process. That made Spark hard to embed in applications, IDEs and notebooks, tied every client to the cluster's Spark version and dependencies, and let one user's out-of-memory error take down a shared cluster. Spark Connect, introduced in Apache Spark 3.4, splits the client from the server so applications talk to Spark over a network protocol instead of living inside the driver.

The problems in classic Spark, and the fix

The Databricks post that introduced Spark Connect in 2022 laid out the problems, and the Spark docs describe the fixes.

Problem in classic Spark Why it happens What Spark Connect changes
Hard to use Spark from apps, IDEs and notebooks The driver is monolithic and runs client applications on top of the scheduler, optimizer and analyzer, with no built-in remote connection from languages other than SQL A thin client connects to a remote Spark server from anywhere
One app can destabilize a shared cluster A user's exception, such as out of memory, can bring the cluster down for all users Applications run in their own processes, so excess memory use affects only their own environment
Dependency conflicts Platform and client APIs are entangled Users define their own dependencies on the client
Painful upgrades Platform and client APIs are entangled, which blocks smooth upgrades between Spark versions The driver can be upgraded independently of applications, as long as server-side RPC definitions stay backwards compatible
Languages without JVM interoperability are left out Languages without JVM interoperability can't use Spark Unresolved logical plans are a language-agnostic protocol
Weak debugging Debugging the JVM process itself lifts the security boundaries Spark puts in place Interactive debugging from your IDE, plus monitoring through the application's own metrics and logging libraries

How Spark Connect works

The client translates DataFrame operations into unresolved logical query plans, encoded with protocol buffers and sent to the server over gRPC. The Spark Connect endpoint on the server turns those plans into Spark's logical plan operators, much as Spark parses a SQL query, and then the standard Spark execution process runs with all of Spark's optimizations. Results stream back over gRPC as Apache Arrow-encoded row batches.
Getting started takes a server script and one builder call. The server binds to the port in spark.connect.grpc.binding.port, which defaults to 15002, and the Spark Connect REPL looks for a local server on that port by default.

# from a Spark distribution
./sbin/start-connect-server.sh
# on the client machine
pip install pyspark-client==4.2.0
from pyspark.sql import SparkSession

spark = SparkSession.builder.remote("sc://localhost").getOrCreate()

You can also set export SPARK_REMOTE="sc://localhost" on the client, and a new session becomes a Spark Connect session with no code change. Since Spark 4.0, the spark.api.mode configuration (classic or connect) lets a Spark Classic application switch to Spark Connect by running a local Spark Connect server.

What Spark Connect doesn't do

  • No RDDs or SparkContext. PySpark on Spark Connect supports most APIs, including DataFrame, Functions and Column, but not SparkContext and RDD. The PySpark API reference labels supported APIs "Supports Spark Connect".
  • No built-in authentication. Its gRPC HTTP/2 interface works with authenticating proxies, so you secure it with your existing authentication infrastructure.
  • Partial streaming coverage. The majority of the Structured Streaming API is supported, including DataStreamReader, DataStreamWriter, StreamingQuery and StreamingQueryListener.
  • UDF setup. UDFs work by default in the shell, and standalone applications need additional setup.

How Databricks approaches Spark Connect

Databricks Connect is built on open-source Spark Connect. It's a client library for Databricks Runtime that lets you run Spark code from IDEs such as Visual Studio Code, PyCharm and IntelliJ IDEA, from notebooks and from custom applications, in Python, R or Scala. It can run all data transformations on Databricks serverless compute.
Spark Connect is also the architecture behind serverless compute, Python notebooks on Databricks Runtime 14.3 and above and Scala notebooks on 13.3 and above on standard compute, and Spark Declarative Pipelines on Databricks with environment versions configured. The Databricks docs flag the behavior changes that matter when you move code over: Spark Connect defers analysis and name resolution to execution time, DataFrames reference temporary views by name, and Python UDFs are serialized lazily. To surface analysis errors at a known point, trigger eager analysis with df.columns, df.schema or df.collect().

FAQs

Is Spark Connect the same as Databricks Connect?

No. Spark Connect is the open-source client-server protocol in Apache Spark. Databricks Connect is a Databricks client library built on it that connects your IDE or application to Databricks compute.

Which languages can use Spark Connect?

The Spark docs cover PySpark, supported since Spark 3.4, and Scala, supported since Spark 3.5. Apache also hosts separate Spark Connect client projects for Go, Swift and Rust.

Is Spark Connect a performance feature?

That isn't its purpose. Queries still run through the standard Spark execution process with all of its optimizations; Spark Connect changes where the client runs and how it talks to the server.

The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.