Cassandra's binary protocol supports optional compression of requests and responses. This reduces network traffic at the cost of a slight CPU overhead, therefore it will likely be beneficial when you have larger payloads, such as:
To enable compression, set the following option in the configuration:
datastax-java-driver { advanced.protocol.compression = lz4 // or snappy }
Compression must be set before opening a session, it cannot be changed at runtime.
Two algorithms are supported out of the box: LZ4 and Snappy. The LZ4 implementation is a good first choice; it offers fallback implementations in case native libraries fail to load and benchmarks suggest that it offers better performance and compression ratios over Snappy.
Both implementations rely on third-party libraries, declared by the driver as optional dependencies; if you enable compression, you need to explicitly depend on the corresponding library to pull it into your project (see the Integration>Driver dependencies section for more details).
Dependency:
<dependency> <groupId>org.lz4</groupId> <artifactId>lz4-java</artifactId> <version>1.4.1</version> </dependency>
Always double-check the exact LZ4 version needed; you can find it in the driver's parent POM.
LZ4-java has three internal implementations (from fastest to slowest):
sun.misc.Unsafe
;It will pick the best implementation depending on what's possible on your platform. To find out which one was chosen, enable INFO logs on the category com.datastax.oss.driver.internal.core.protocol.Lz4Compressor
and look for the following message:
INFO com.datastax.oss.driver.internal.core.protocol.Lz4Compressor - Using LZ4Factory:JNI
Dependency:
<dependency> <groupId>org.xerial.snappy</groupId> <artifactId>snappy-java</artifactId> <version>1.1.2.6</version> </dependency>
Important: Snappy is not supported when building a GraalVM native image.
Always double-check the exact Snappy version needed; you can find it in the driver's parent POM.