{"record":{"id":"7241951e1d977a10","repo":"apache/cassandra","slug":"prepared-statements-discarded-in-the-last-minut","errorCode":null,"errorMessage":"{} prepared statements discarded in the last minute because cache limit reached ({} MiB)","messagePattern":"(.+?) prepared statements discarded in the last minute because cache limit reached \\((.+?) MiB\\)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/java/org/apache/cassandra/cql3/QueryProcessor.java","lineNumber":153,"sourceCode":"\n    // Size of the prepared statement cache in bytes.\n    public static long PREPARED_STATEMENT_CACHE_SIZE_BYTES = capacityToBytes(DatabaseDescriptor.getPreparedStatementsCacheSizeMiB());\n\n    private static final AtomicInteger lastMinuteEvictionsCount = new AtomicInteger(0);\n\n    static\n    {\n        preparedStatements = Caffeine.newBuilder()\n                             .executor(ImmediateExecutor.INSTANCE)\n                             .maximumWeight(PREPARED_STATEMENT_CACHE_SIZE_BYTES)\n                             .weigher(QueryProcessor::getSizeOfPreparedStatementForCache)\n                             .removalListener((key, prepared, cause) -> evictPreparedStatement(key, cause))\n                             .build();\n\n        ScheduledExecutors.scheduledTasks.scheduleAtFixedRate(() -> {\n            long count = lastMinuteEvictionsCount.getAndSet(0);\n            if (count > 0)\n                logger.warn(\"{} prepared statements discarded in the last minute because cache limit reached ({} MiB)\",\n                            count,\n                            DatabaseDescriptor.getPreparedStatementsCacheSizeMiB());\n        }, 1, 1, TimeUnit.MINUTES);\n\n        logger.info(\"Initialized prepared statement caches with {} MiB\",\n                    DatabaseDescriptor.getPreparedStatementsCacheSizeMiB());\n    }\n\n    private static void evictPreparedStatement(MD5Digest key, RemovalCause cause)\n    {\n        if (cause.wasEvicted())\n        {\n            metrics.preparedStatementsEvicted.inc();\n            lastMinuteEvictionsCount.incrementAndGet();\n            SystemKeyspace.removePreparedStatement(key);\n        }\n    }\n","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/QueryProcessor.java#L135-L171","documentation":"QueryProcessor's scheduled task warns when prepared statements had to be evicted from the in-memory prepared-statement cache during the last minute because the cache hit its configured size limit (prepared_statements_cache_size_mb). Evicted statements must be re-prepared by clients, adding latency and load.","triggerScenarios":"Client drivers preparing more unique statements than fit in DatabaseDescriptor.getPreparedStatementsCacheSizeMiB(); the per-minute scheduled job in QueryProcessor detects evictions > 0 and logs the warning.","commonSituations":"Applications generating unbounded unique statements (e.g. concatenating IDs/values into query text instead of using bind markers); undersized cache after a client fleet growth; burst of new schema/queries.","solutions":["Increase prepared_statements_cache_size_mb in cassandra.yaml (or set to 0 for auto heuristic) and restart","Fix clients to use bind variables/parameters instead of string-interpolated unique queries","Monitor statement churn and reduce per-partition key IN lists that expand into many unique statements"],"exampleFix":"# cassandra.yaml\n# before\nprepared_statements_cache_size_mb: 16\n# after\nprepared_statements_cache_size_mb: 256","handlingStrategy":"validation","validationCode":"// Client-side: detect query-shape churn\nconst uniqueTemplates = new Set();\nfunction guard(query) {\n  const tpl = query.replace(/\\d+/g, '?').replace(/'[^']*'/g, '?');\n  if (uniqueTemplates.size > 10000) throw new Error('statement churn too high');\n  uniqueTemplates.add(tpl);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always use bind markers instead of string interpolation","Size prepared_statements_cache_size_mb above your actual unique statement count","Alert on this warning in log monitoring; it indicates client-side query design issues"],"tags":["cql","cache","prepared-statements","capacity"],"backgroundTag":"rate-limit-exceeded","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"}