{"record":{"id":"7077bc3a52edd32f","repo":"elastic/elasticsearch","slug":"entity-content-is-too-long-for-the-configured","errorCode":null,"errorMessage":"entity content is too long [{}] for the configured buffer limit [{}]","messagePattern":"entity content is too long \\[(.+?)\\] for the configured buffer limit \\[(.+?)\\]","errorType":"exception","errorClass":"ContentTooLongException","httpStatus":null,"severity":"error","filePath":"client/rest/src/main/java/org/elasticsearch/client/HeapBufferedAsyncResponseConsumer.java","lineNumber":76,"sourceCode":"    }\n\n    /**\n     * Get the limit of the buffer.\n     */\n    public int getBufferLimit() {\n        return bufferLimitBytes;\n    }\n\n    @Override\n    protected void onResponseReceived(HttpResponse httpResponse) throws HttpException, IOException {\n        this.response = httpResponse;\n    }\n\n    @Override\n    protected void onEntityEnclosed(HttpEntity entity, ContentType contentType) throws IOException {\n        long len = entity.getContentLength();\n        if (len > bufferLimitBytes) {\n            throw new ContentTooLongException(\n                \"entity content is too long [\" + len + \"] for the configured buffer limit [\" + bufferLimitBytes + \"]\"\n            );\n        }\n        if (len < 0) {\n            len = 4096;\n        }\n        this.buf = new SimpleInputBuffer((int) len, getByteBufferAllocator());\n        this.response.setEntity(new ContentBufferEntity(entity, this.buf));\n    }\n\n    /**\n     * Returns the instance of {@link ByteBufferAllocator} to use for content buffering.\n     * Allows to plug in any {@link ByteBufferAllocator} implementation.\n     */\n    protected ByteBufferAllocator getByteBufferAllocator() {\n        return HeapByteBufferAllocator.INSTANCE;\n    }\n","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/client/rest/src/main/java/org/elasticsearch/client/HeapBufferedAsyncResponseConsumer.java#L58-L94","documentation":"In onEntityEnclosed, HeapBufferedAsyncResponseConsumer checks the incoming HttpEntity's declared content length against bufferLimitBytes and throws ContentTooLongException when len > bufferLimitBytes. This protects the JVM heap by refusing to buffer an oversized response body before allocation; both the actual length and the configured limit are reported.","triggerScenarios":"An Elasticsearch node returns a response whose Content-Length exceeds the configured heap buffer limit — e.g. a large scroll/search response, a bulk index of oversized docs, or a _search response with many hits while the client buffer limit is small.","commonSituations":"Default buffer limit too small for the workload (large aggregations, scroll contexts, snapshot metadata); a misconfigured limit; a query that accidentally returns far more data than expected.","solutions":["Raise the buffer limit on RestClientBuilder (setHttpAsyncResponseConsumerFactory with a larger HeapBufferedAsyncResponseConsumer) to fit the largest expected response.","Reduce the response size: paginate (search_after / scroll), narrow the query, or request fewer fields.","Stream the response instead of buffering it if the consumer supports it."],"exampleFix":"// before: default 100MB limit, query returns 250MB\nRestClientBuilder.builder(host).setHttpAsyncResponseConsumerFactory(\n    new HttpAsyncResponseConsumerFactory.HeapBuffered(100 * 1024 * 1024));\n// after\nRestClientBuilder.builder(host).setHttpAsyncResponseConsumerFactory(\n    new HttpAsyncResponseConsumerFactory.HeapBuffered(512 * 1024 * 1024));","handlingStrategy":"validation","validationCode":"// Size the buffer to the largest expected response payload (bytes):\nlong expectedMax = estimateMaxResponseBytes();\nint limit = (int) Math.min(Integer.MAX_VALUE, Math.max(1, expectedMax));\nRestClientBuilder b = RestClient.builder(host)\n    .setHttpAsyncResponseConsumerFactory(new HttpAsyncResponseConsumerFactory.HeapBuffered(limit));","typeGuard":null,"tryCatchPattern":"try {\n    Response r = client.performRequest(req);\n} catch (ResponseException | ContentTooLongException e) {\n    // narrow query / paginate / raise limit and retry\n}","preventionTips":["Right-size queries: use pagination (search_after/scroll), narrow filters, project fewer fields.","Set the heap buffer limit based on measured peak response size, with headroom.","Monitor for ContentTooLongException in production to catch runaway queries early."],"tags":["rest-client","memory","response","content-too-long"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}