The native protocol defines the format of the binary messages exchanged between the driver and Cassandra over TCP. As a driver user, you don‘t need to know the fine details (although the protocol spec is available if you’re curious); the most visible aspect is that some features are only available with specific protocol versions.
Java Driver 4 supports protocol versions 3 to 5. By default, the version is negotiated with the first node the driver connects to:
Cassandra version | Negotiated protocol version with driver 4 ¹ |
---|---|
2.1.x (DSE 4.7/4.8) | v3 |
2.2.x | v4 |
3.x (DSE 5.0/5.1) | v4 |
4.x ² | v5 |
(1) for previous driver versions, see the 3.x documentation
(2) at the time of writing, Cassandra 4 is not released yet. Protocol v5 support is still in beta, and must be enabled explicitly (negotiation will yield v4).
To find out which version you're currently using, use the following:
ProtocolVersion currentVersion = session.getContext().getProtocolVersion();
The protocol version cannot be changed at runtime. However, you can force a particular version in the configuration:
datastax-java-driver { advanced.protocol { version = v3 } }
If you force a version that is too high for the server, you'll get an error:
Exception in thread "main" com.datastax.oss.driver.api.core.AllNodesFailedException: All 1 node tried for the query failed (showing first 1, use getErrors() for more: /127.0.0.1:9042: com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException: [/127.0.0.1:9042] Host does not support protocol version V5)
It's possible to have heterogeneous Cassandra versions in your cluster, in particular during a rolling upgrade. This used to be a problem with previous driver versions, which would negotiate a version with the first contacted node that might not work with others.
Starting with driver 4, protocol negotiation uses an improved strategy to prevent those issues:
system.peers
table to find out the Cassandra version of the other nodes (for example, node2 → Cassandra 3, node3 → Cassandra 2.1);Thanks to this approach, automatic negotiation works even with mixed clusters, you don't need to force the protocol version manually anymore.
The main steps are logged at level INFO
. If the driver downgrades while negotiating with the first node, you should see logs such as:
INFO ChannelFactory - Failed to connect with protocol v4, retrying with v3
If it then detects a mixed cluster with lower versions, it will log:
INFO DefaultSession - Negotiated protocol version v4 for the initial contact point, but other nodes only support v3, downgrading
If you want to see the details of mixed cluster negotiation, enable DEBUG
level for the category com.datastax.oss.driver.internal.core.CassandraProtocolVersionRegistry
.