|
| 1 | +## Compression |
| 2 | + |
| 3 | +Cassandra's binary protocol supports optional compression of |
| 4 | +transport-level requests and responses, for example: |
| 5 | + |
| 6 | +* a query with its serialized parameters; |
| 7 | +* a [page](../paging/) from a result set, i.e. a list of serialized |
| 8 | + rows. |
| 9 | + |
| 10 | +It reduces network traffic at the cost of CPU overhead, therefore it |
| 11 | +will likely be beneficial when you have larger payloads. |
| 12 | + |
| 13 | +Two algorithms are available: |
| 14 | +[LZ4](https://github.com/jpountz/lz4-java) and |
| 15 | +[Snappy](https://code.google.com/p/snappy/). |
| 16 | +Both rely on third-party libraries, declared by the driver as *optional* |
| 17 | +dependencies. So If you use a build tool like Maven, you'll need to |
| 18 | +declare an explicit dependency to pull the appropriate library in your |
| 19 | +application's classpath. Then you configure compression at driver |
| 20 | +startup. |
| 21 | + |
| 22 | +### LZ4 |
| 23 | + |
| 24 | +Maven dependency: |
| 25 | + |
| 26 | +```xml |
| 27 | +<dependency> |
| 28 | + <groupId>net.jpountz.lz4</groupId> |
| 29 | + <artifactId>lz4</artifactId> |
| 30 | + <version>1.2.0</version> |
| 31 | +</dependency> |
| 32 | +``` |
| 33 | + |
| 34 | +Always check the exact version of the library: go to the driver's |
| 35 | +[parent POM][pom] (change the URL to match your driver version) and look |
| 36 | +for the `lz4.version` property. |
| 37 | + |
| 38 | +Driver configuration: |
| 39 | + |
| 40 | +```java |
| 41 | +cluster = Cluster.builder() |
| 42 | + .addContactPoint("127.0.0.1") |
| 43 | + .withCompression(ProtocolOptions.Compression.LZ4) |
| 44 | + .build(); |
| 45 | +``` |
| 46 | + |
| 47 | +LZ4-java has three internal implementations (from fastest to slowest): |
| 48 | + |
| 49 | +* JNI; |
| 50 | +* pure Java using sun.misc.Unsafe; |
| 51 | +* pure Java using only "safe" classes. |
| 52 | + |
| 53 | +It will pick the best implementation depending on what's possible on |
| 54 | +your platform. To find out which one was chosen, [enable INFO |
| 55 | +logs](../logging/) on the category |
| 56 | +`com.datastax.driver.core.FrameCompressor` and look for a log similar to |
| 57 | +this: |
| 58 | + |
| 59 | +``` |
| 60 | +INFO com.datastax.driver.core.FrameCompressor - Using LZ4Factory:JNI |
| 61 | +``` |
| 62 | + |
| 63 | +### Snappy |
| 64 | + |
| 65 | +Maven dependency: |
| 66 | + |
| 67 | +```xml |
| 68 | +<dependency> |
| 69 | + <groupId>org.xerial.snappy</groupId> |
| 70 | + <artifactId>snappy-java</artifactId> |
| 71 | + <version>1.0.5</version> |
| 72 | +</dependency> |
| 73 | +``` |
| 74 | + |
| 75 | +Always check the exact version of the library: go to the driver's |
| 76 | +[parent POM][pom] (change the URL to match your driver version) and look |
| 77 | +for the `snappy.version` property. |
| 78 | + |
| 79 | +Driver configuration: |
| 80 | + |
| 81 | +```java |
| 82 | +cluster = Cluster.builder() |
| 83 | + .addContactPoint("127.0.0.1") |
| 84 | + .withCompression(ProtocolOptions.Compression.SNAPPY) |
| 85 | + .build(); |
| 86 | +``` |
| 87 | + |
| 88 | +[pom]: https://repo1.maven.org/maven2/com/datastax/cassandra/cassandra-driver-parent/2.0.10/cassandra-driver-parent-2.0.10.pom |
0 commit comments