Kryo vs Java Serialization
Serialization also happens with no Python anywhere in sight. Spark turns JVM objects into bytes to shuffle an RDD, to keep a cached block in a serialized storage level, to ship a broadcast variable, and to send every task from the driver to an executor. spark.serializer picks the library for the first 3, and its default is org.apache.spark.serializer.JavaSerializer. Task closures always use Java serialization, whatever the setting says.
What each serializer writes
Java serialization writes a class descriptor before the data. The descriptor holds the full class name and a version id, followed by every field's name and type, and then the serializer walks the object graph by reflection. Within one stream a repeated descriptor becomes a back-reference, but Spark resets the stream every spark.serializer.objectStreamReset objects, 100 by default, so descriptors are written again and again. Kryo writes a small integer for each registered class and uses per-class serializers that write field values in order, with no field names. A class you never registered still works, but Kryo then stores its full class name with each object, as the Spark tuning guide warns.
- 10x
- 64m
Turning it on
from pyspark.sql import SparkSession
spark = (
SparkSession.builder
.config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
.config("spark.kryo.registrationRequired", "true")
.config("spark.kryo.classesToRegister", "com.acme.routing.Edge,com.acme.routing.Tile")
.config("spark.kryoserializer.buffer.max", "256m")
.getOrCreate()
)The serializer is fixed when the session starts, so it belongs in the builder or in spark-submit, never in a spark.conf.set call mid-job. registrationRequired makes the first unregistered class fail its task with an error naming the class, which turns a silent size penalty into a list of names to add. buffer.max caps a single serialized object, and a Buffer overflow error means one record or broadcast value outgrew it, often a large lookup map built in JVM code.
Where the setting changes nothing
| Data path | Bytes written by | Does spark.serializer matter? |
|---|---|---|
| DataFrame shuffle | `UnsafeRow` binary, copied as is | No |
| DataFrame `cache()` | Compressed columnar batches | No |
| DataFrame broadcast join | The hash relation's own binary format | Barely |
| Python UDF border | Pickle or Arrow | No |
| Python RDD records | Pickled in Python, byte arrays in the JVM | Barely |
| RDD of JVM objects: shuffle, serialized cache | `spark.serializer` | Yes |
| Broadcast of a JVM object | `spark.serializer` | Yes |
| Task closures | Java serialization, always | Not configurable |
For a job built from DataFrames and Python, switching to Kryo moves no number in the Spark UI that you could attribute to it. Even for RDDs, Spark has used Kryo internally since 2.0 when it shuffles primitives and strings, and arrays of primitives get the same treatment. The setting matters in JVM code that keeps RDDs of custom classes. Graph and iterative algorithms do this, and so does the ALS recommender in Spark's ML library, which runs its iterations on RDDs internally. Where no stage handles RDDs of JVM objects, the choice is a no-op, and the tuning time belongs at the Python border.
Python has a serializer choice of its own for RDD records: pickle by default, or a marshal-based serializer passed when the SparkContext is created, faster but limited to Python's built-in types. It changes the byte arrays the JVM receives, which the JVM treats as opaque, so spark.serializer plays no part in it.
A job built only from DataFrame operations writes 400GB of shuffle per run. The team sets spark.serializer to Kryo with every custom class registered. What happens to shuffle write in the Spark UI?
- It drops sharply, because Kryo writes far fewer bytes per record than Java serialization
- It stays about the same, because DataFrame exchanges write UnsafeRow bytes without spark.serializer
- It stays the same until the row classes are registered, then drops by about half
- It grows, because Kryo stores a full class name with each row that it writes
About this lesson
This section is part of the UDFs and Serialization: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.