{"record":{"id":"fe89e9d8851c00fa","repo":"apache/cassandra","slug":"key-length-of-d-is-longer-than-maximum-of-d-fe89e9","errorCode":null,"errorMessage":"Key length of %d is longer than maximum of %d","messagePattern":"Key length of (.+?) is longer than maximum of (.+?)","errorType":"validation","errorClass":"org.apache.cassandra.exceptions.InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/db/ClusteringPrefix.java","lineNumber":340,"sourceCode":"\n    default byte[] arrayAt(int i)\n    {\n        return accessor().toArray(get(i));\n    }\n\n    default String stringAt(int i, ClusteringComparator comparator)\n    {\n        return comparator.subtype(i).getString(get(i), accessor());\n    }\n\n    default void validate()\n    {\n        ValueAccessor<V> accessor = accessor();\n        int sum = 0;\n        for (V v : getRawValues())\n        {\n            if (v != null && accessor.size(v) > FBUtilities.MAX_UNSIGNED_SHORT)\n                throw new InvalidRequestException(String.format(\"Key length of %d is longer than maximum of %d\",\n                                                                dataSize(),\n                                                                FBUtilities.MAX_UNSIGNED_SHORT));\n            sum += v == null ? 0 : accessor.size(v);\n        }\n        if (sum > FBUtilities.MAX_UNSIGNED_SHORT)\n            throw new InvalidRequestException(String.format(\"Key length of %d is longer than maximum of %d\",\n                                                            sum,\n                                                            FBUtilities.MAX_UNSIGNED_SHORT));\n    }\n\n    default void validate(int i, ClusteringComparator comparator)\n    {\n        comparator.subtype(i).validate(get(i), accessor());\n    }\n\n    /**\n     * Adds the data of this clustering prefix to the provided Digest instance.\n     *","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/db/ClusteringPrefix.java#L322-L358","documentation":"ClusteringPrefix.validate() enforces that each clustering component, and the total serialized clustering size, fit within FBUtilities.MAX_UNSIGNED_SHORT (65535 bytes), because component lengths are encoded as unsigned shorts. Exceeding this raises InvalidRequestException.","triggerScenarios":"Writing a row whose clustering key component (e.g. a text partition of a time-series key) exceeds 65535 bytes, or whose combined clustering components exceed the limit.","commonSituations":"Using very long strings/blobs as clustering keys; application-generated composite keys that concatenate large values; importing data from systems without key-length limits.","solutions":["Reduce the size of clustering key values (hash, truncate, or move large payloads to a regular column).","Validate key lengths in the application before writing.","Redesign the schema so large values are stored in value columns, not clustering columns."],"exampleFix":"// before\nINSERT INTO t (pk, very_long_clustering_key, v) VALUES (1, <200000-byte string>, 'x');\n// after\nINSERT INTO t (pk, ck_hash, payload) VALUES (1, md5(payload), <large string stored as value>);","handlingStrategy":"validation","validationCode":"int total = 0;\nfor (byte[] v : clusteringValues) {\n    if (v.length > 65535) throw new IllegalArgumentException(\"Clustering component too long\");\n    total += v.length;\n}\nif (total > 65535) throw new IllegalArgumentException(\"Clustering key too long\");","typeGuard":null,"tryCatchPattern":"try { session.execute(insert); } catch (InvalidRequestException e) { if (e.getMessage().contains(\"longer than maximum\")) shrinkKey(); else throw e; }","preventionTips":["Keep clustering key components well under 64KB","Hash or truncate large string keys","Store large payloads in value columns, not keys"],"tags":["clustering","key-size","validation"],"backgroundTag":"value-out-of-range","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}