{"record":{"id":"66d82f76d949c22b","repo":"apache/cassandra","slug":"any-consistencylevel-is-only-supported-for-writes","errorCode":null,"errorMessage":"ANY ConsistencyLevel is only supported for writes","messagePattern":"ANY ConsistencyLevel is only supported for writes","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/db/ConsistencyLevel.java","lineNumber":225,"sourceCode":"     * Determine if this consistency level meets or exceeds the consistency requirements of the given cl for the given keyspace\n     * WARNING: this is not locality aware; you cannot safely use this with mixed locality consistency levels (e.g. LOCAL_QUORUM and QUORUM)\n     */\n    public boolean satisfies(ConsistencyLevel other, AbstractReplicationStrategy replicationStrategy)\n    {\n        return blockFor(replicationStrategy) >= other.blockFor(replicationStrategy);\n    }\n\n    public boolean isDatacenterLocal()\n    {\n        return isDCLocal;\n    }\n\n    public void validateForRead() throws InvalidRequestException\n    {\n        switch (this)\n        {\n            case ANY:\n                throw new InvalidRequestException(\"ANY ConsistencyLevel is only supported for writes\");\n        }\n    }\n\n    public void validateForWrite() throws InvalidRequestException\n    {\n        switch (this)\n        {\n            case SERIAL:\n            case UNSAFE_DELAY_SERIAL:\n            case LOCAL_SERIAL:\n            case UNSAFE_DELAY_LOCAL_SERIAL:\n                throw new InvalidRequestException(\"You must use conditional updates for serializable writes\");\n        }\n    }\n\n    // This is the same than validateForWrite really, but we include a slightly different error message for SERIAL/LOCAL_SERIAL\n    public void validateForCasCommit(AbstractReplicationStrategy replicationStrategy) throws InvalidRequestException\n    {","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/db/ConsistencyLevel.java#L207-L243","documentation":"ConsistencyLevel.ANY means a write may succeed with zero live replicas (only a hint is recorded). That semantics is meaningless for reads, so validateForRead rejects ANY when a read request specifies it. This is a client request validation error, raised before any data is touched.","triggerScenarios":"Executing a SELECT (or read-path statement) with consistency level ANY, e.g. `CONSISTENCY ANY;` in cqlsh followed by a SELECT, or setting cl=ANY on a read in a driver.","commonSituations":"cqlsh session left at ANY after testing writes; scripts that reuse one consistency constant for all operations; misunderstanding of ANY's write-only semantics.","solutions":["Use ONE for reads where latency matters and some staleness is acceptable.","Use QUORUM or LOCAL_QUORUM when read consistency must match write quorums.","In cqlsh, run `CONSISTENCY ONE;` (or similar) before issuing reads.","Split read and write consistency constants in application code instead of sharing one value."],"exampleFix":"// before\nsession.execute(SimpleStatement.builder(\"SELECT * FROM t\").setConsistencyLevel(ConsistencyLevel.ANY).build());\n// after\nsession.execute(SimpleStatement.builder(\"SELECT * FROM t\").setConsistencyLevel(ConsistencyLevel.ONE).build());","handlingStrategy":"validation","validationCode":"// Guard before executing a read\nprivate static final Set<ConsistencyLevel> READ_OK =\n    Set.of(ConsistencyLevel.ONE, ConsistencyLevel.TWO, ConsistencyLevel.THREE,\n           ConsistencyLevel.QUORUM, ConsistencyLevel.ALL,\n           ConsistencyLevel.LOCAL_ONE, ConsistencyLevel.LOCAL_QUORUM);\nif (!READ_OK.contains(cl)) throw new IllegalArgumentException(\"ANY not valid for reads\");","typeGuard":null,"tryCatchPattern":"catch (InvalidRequestException e) {\n    if (e.getMessage().contains(\"ANY ConsistencyLevel\")) {\n        statement.setConsistencyLevel(ConsistencyLevel.ONE); // retry at ONE\n    } else throw e;\n}","preventionTips":["Keep separate read/write consistency constants in application config.","Never set session-global ANY in cqlsh; scope it to specific write statements.","Code-review consistency-level literals; prefer named enums over magic values.","Add unit tests asserting read paths use read-safe consistency levels."],"tags":["cassandra","consistency-level","read","validation"],"backgroundTag":"invalid-argument-value","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"}