| 1 | ## Native protocol |
| 2 | |
| 3 | The native protocol defines the format of the binary messages exchanged |
| 4 | between the driver and Cassandra over TCP. As a driver user, you don't |
| 5 | need to know the fine details (although the protocol spec is [in the |
| 6 | Cassandra codebase][native_spec] if you're curious); the most visible |
| 7 | aspect is that some features are only available with specific protocol |
| 8 | versions. |
| 9 | |
| 10 | [native_spec]: https://github.com/apache/cassandra/tree/trunk/doc |
| 11 | |
| 12 | ### Compatibility matrix |
| 13 | |
| 14 | By default, the protocol version is negotiated between the driver and |
| 15 | Cassandra when the first connection is established. Both sides are |
| 16 | backward-compatible with older versions: |
| 17 | |
| 18 | <table border="1" style="text-align:center; width:100%;margin-bottom:1em;"> |
| 19 | <tr><td> </td><td>Cassandra: 1.2.x<br/>(DSE 3.2)</td><td>2.0.x<br/>(DSE 4.0 to 4.6)</td><td>2.1.x<br/>(DSE 4.7)</td><td>2.2.x</td><td>3.0.x</td></tr> |
| 20 | <tr><td>Driver: 1.0.x</td> <td>v1</td> <td>v1</td> <td>v1</td> <td>v1</td> <td>Unsupported <i>(1)</i></td> </tr> |
| 21 | <tr><td>2.0.x to 2.1.1</td> <td>v1</td> <td>v2</td> <td>v2</td> <td>v2</td> <td>Unsupported <i>(1)</i></td> </tr> |
| 22 | <tr><td>2.1.2 to 2.1.x</td> <td>v1</td> <td>v2</td> <td>v3</td> <td>v3</td> <td>Unsupported <i>(2)</i></td> </tr> |
| 23 | <tr><td>3.x</td> <td>v1</td> <td>v2</td> <td>v3</td> <td>v4</td> <td>v4</td> </tr> |
| 24 | </table> |
| 25 | |
| 26 | *(1) Cassandra 3.0 does not support protocol versions v1 and v2* |
| 27 | |
| 28 | *(2) There is a matching protocol version (v3), but the driver 2.1.x can't read the new system table format of Cassandra 3.0* |
| 29 | |
| 30 | For example, if you use version 2.1.5 of the driver to connect to |
| 31 | Cassandra 2.0.9, the maximum version you can use (and the one you'll get |
| 32 | by default) is protocol v2 (third row, second column). If you use the |
| 33 | same version to connect to Cassandra 2.1.4, you can use protocol v3. |
| 34 | |
| 35 | ### Controlling the protocol version |
| 36 | |
| 37 | To find out which version you're currently using, use |
| 38 | [ProtocolOptions#getProtocolVersion()][gpv]: |
| 39 | |
| 40 | ```java |
| 41 | ProtocolVersion myCurrentVersion = cluster.getConfiguration() |
| 42 | .getProtocolOptions() |
| 43 | .getProtocolVersion(); |
| 44 | ``` |
| 45 | |
| 46 | The protocol version can not be changed at runtime. However, you can |
| 47 | force a given version at initialization: |
| 48 | |
| 49 | ```java |
| 50 | Cluster cluster = Cluster.builder() |
| 51 | .addContactPoint("127.0.0.1") |
| 52 | .withProtocolVersion(ProtocolVersion.V2) |
| 53 | .build(); |
| 54 | ``` |
| 55 | |
| 56 | If you specify a version that is not compatible with your current |
| 57 | driver/Cassandra combination, you'll get an error: |
| 58 | |
| 59 | ``` |
| 60 | Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException: |
| 61 | All host(s) tried for query failed |
| 62 | (tried: /127.0.0.1:9042 (com.datastax.driver.core.UnsupportedProtocolVersionException: |
| 63 | [/127.0.0.1:9042] Host /127.0.0.1:9042 does not support protocol version V3 but V2)) |
| 64 | ``` |
| 65 | |
| 66 | [gpv]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/ProtocolOptions.html#getProtocolVersion-- |
| 67 | |
| 68 | #### Protocol version with mixed clusters |
| 69 | |
| 70 | If you have a cluster with mixed versions (for example, while doing a |
| 71 | rolling upgrade of Cassandra), note that **the protocol version will be |
| 72 | negotiated with the first host the driver connects to**. |
| 73 | |
| 74 | This could lead to the following situation (assuming you use driver |
| 75 | 2.1.2+): |
| 76 | |
| 77 | * the first contact point is a 2.1 host, so the driver negotiates |
| 78 | protocol v3; |
| 79 | * while connecting to the rest of the cluster, the driver contacts a 2.0 |
| 80 | host using protocol v3, which fails; an error is logged and this host |
| 81 | will be permanently ignored. |
| 82 | |
| 83 | To avoid this issue, you can use one the following workarounds: |
| 84 | |
| 85 | * always force a protocol version at startup. You keep it at v2 while |
| 86 | the rolling upgrade is happening, and only switch to v3 when the whole |
| 87 | cluster has switched to Cassandra 2.1; |
| 88 | * ensure that the list of initial contact points only contains hosts |
| 89 | with the oldest version (2.0 in this example). |
| 90 | |
| 91 | |
| 92 | ### New features by protocol version |
| 93 | |
| 94 | #### v1 to v2 |
| 95 | |
| 96 | * bound variables in simple statements |
| 97 | ([Session#execute(String, Object...)](http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Session.html#execute-java.lang.String-java.lang.Object...-)) |
| 98 | * [batch statements](http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/BatchStatement.html) |
| 99 | * [query paging](../paging/) |
| 100 | |
| 101 | #### v2 to v3 |
| 102 | |
| 103 | * the number of stream ids per connection goes from 128 to 32768 (see |
| 104 | [Connection pooling](../pooling/)) |
| 105 | * [serial consistency on batch statements](http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/BatchStatement.html#setSerialConsistencyLevel-com.datastax.driver.core.ConsistencyLevel-) |
| 106 | * [client-side timestamps](../query_timestamps/) |
| 107 | |
| 108 | #### v3 to v4 |
| 109 | |
| 110 | * [query warnings](http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/ExecutionInfo.html#getWarnings--) |
| 111 | * allowed unset values in bound statements |
| 112 | * [Custom payloads](../custom_payloads/) |