{"record":{"id":"0b15dccc7e7cd564","repo":"apache/cassandra","slug":"native-protocol-version-d-supports-up-to-65535-el","errorCode":null,"errorMessage":"Native protocol version %d supports up to 65535 elements in any collection - but collection contains %d elements","messagePattern":"Native protocol version (.+?) supports up to 65535 elements in any collection - but collection contains (.+?) elements","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/types/CodecUtils.java","lineNumber":102,"sourceCode":"        }\n    }\n\n    /**\n     * Utility method that writes a size value. Mainly intended for collection codecs when serializing\n     * CQL collections.\n     *\n     * @param output  The ByteBuffer to write to.\n     * @param size    The collection size.\n     * @param version The protocol version to use.\n     */\n    private static void writeSize(ByteBuffer output, int size, ProtocolVersion version)\n    {\n        switch (version)\n        {\n            case V1:\n            case V2:\n                if (size > 65535)\n                    throw new IllegalArgumentException(\n                    String.format(\n                    \"Native protocol version %d supports up to 65535 elements in any collection - but collection contains %d elements\",\n                    version.asInt(), size));\n                output.putShort((short) size);\n                break;\n            case V3:\n            case V4:\n            case V5:\n            case V6:\n                output.putInt(size);\n                break;\n            default:\n                throw new IllegalArgumentException(String.valueOf(version));\n        }\n    }\n\n    /**\n     * Utility method that reads a value. Mainly intended for collection codecs when deserializing CQL","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/types/CodecUtils.java#L84-L120","documentation":"The driver's CodecUtils.writeSize serializes collection cardinality in native protocol V1/V2 with a 16-bit element count. If a collection being encoded (a Set, List, or Map argument bound to a statement) contains more than 65535 elements, the short counter would overflow, so pack throws IllegalArgumentException for protocol versions 1 and 2.","triggerScenarios":"Binding a Java collection with more than 65,535 entries (e.g. a huge List<Integer> or Map) to a statement executed with protocolVersion V1 or V2 explicitly set in the cluster configuration.","commonSituations":"Legacy clusters pinned to protocol V1/V2; batch inserts built as giant collection arguments; migrating code from V3+ protocol assumptions to older clients.","solutions":["Upgrade the native protocol version to V3 or higher (set with-clients/Cluster.builder().withProtocolVersion(ProtocolVersion.V4))","Split the data into multiple statements with collections under 65,535 elements each","Model very large datasets in rows rather than one oversized collection column"],"exampleFix":"// before\ncluster.builder().withProtocolVersion(ProtocolVersion.V2); // big collections rejected\n// after\ncluster.builder().withProtocolVersion(ProtocolVersion.V4); // supports 32-bit collection sizes","handlingStrategy":"validation","validationCode":"if (collection.size() > 65535 && clusterConfiguration.getProtocolVersion().compareTo(ProtocolVersion.V3) < 0) {\n    throw new IllegalArgumentException(\"split collection: V1/V2 supports max 65535 elements\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    session.execute(boundStatement);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"65535 elements\")) {\n        // chunk the collection and retry per chunk\n    }\n}","preventionTips":["Run protocol V3 or higher unless a legacy cluster forces otherwise","Chunk collections before binding when sizes are unbounded","Keep collection cardinality bounded by design (e.g. time-partitioned rows)"],"tags":["java-driver","serialization","protocol-limit"],"backgroundTag":"payload-too-large","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}