{"record":{"id":"21de0ba4b1d08cab","repo":"dgraph-io/dgraph","slug":"while-writing-to-buffer-encoded-response-size-d","errorCode":null,"errorMessage":"while writing to buffer. Encoded response size: %d is bigger than threshold: %d","messagePattern":"while writing to buffer\\. Encoded response size: (.+?) is bigger than threshold: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/outputnode.go","lineNumber":1220,"sourceCode":"\tenc.fixOrder(n)\n\n\t// According to GraphQL spec response should only contain data, errors and extensions as top\n\t// level keys. Hence we send server_latency under extensions key.\n\t// https://facebook.github.io/graphql/#sec-Response-Format\n\n\t// if there is a GraphQL field that means we need to encode the response in GraphQL form,\n\t// otherwise encode it in DQL form.\n\tif field != nil {\n\t\t// if there were any GraphQL errors, we need to propagate them back to GraphQL layer along\n\t\t// with the data. So, don't return here if we get an error.\n\t\terr = sg.toGraphqlJSON(newGraphQLEncoder(ctx, enc), n, field)\n\t} else if err = sg.toDqlJSON(enc, n); err != nil {\n\t\treturn nil, err\n\t}\n\n\t// Return error if encoded buffer size exceeds than a threshold size.\n\tif uint64(enc.buf.Len()) > maxEncodedSize {\n\t\treturn nil, fmt.Errorf(\"while writing to buffer. Encoded response size: %d\"+\n\t\t\t\" is bigger than threshold: %d\", enc.buf.Len(), maxEncodedSize)\n\t}\n\n\treturn enc.buf.Bytes(), err\n}\n\nfunc (sg *SubGraph) toDqlJSON(enc *encoder, n fastJsonNode) error {\n\tif enc.children(n) == nil {\n\t\tx.Check2(enc.buf.WriteString(`{}`))\n\t\treturn nil\n\t}\n\treturn enc.encode(n)\n}\n\nfunc (sg *SubGraph) toGraphqlJSON(genc *graphQLEncoder, n fastJsonNode, f gqlSchema.Field) error {\n\t// GraphQL queries will always have at least one query whose results are visible to users,\n\t// implying that the root fastJson node will always have at least one child. So, no need\n\t// to check for the case where there are no children for the root fastJson node.","sourceCodeStart":1202,"sourceCodeEnd":1238,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/outputnode.go#L1202-L1238","documentation":"Dgraph caps the encoded JSON size of a single query response at maxEncodedSize (default 1MB). When toFastJSON finishes and the buffer exceeds the threshold, it refuses to return a giant response and errors instead, protecting the server and client from unbounded memory use.","triggerScenarios":"Any query (or ToJson call) whose encoded result exceeds the server's maxEncodedSize — e.g. deep recursion, large @normalize output, expand(_all_) on dense nodes, or many predicates per node.","commonSituations":"Recursive queries without depth/branch limits; expand(_all_) on wide schemas; clients on instances with default --limit \"encoded-size=1mb\" fetching bulk exports; dot-product of reverse edges in @normalize queries.","solutions":["Raise the encoded size limit: --limit \"encoded-size=<bytes>\" (or LIMIT option encoded-size) on the server and/or per-query via the limit directive","Paginate: add first/offset or paginate roots and fetch in batches","Avoid expand(_all_)/deep recurse; project only needed predicates","Reduce fan-out with @filter on nested edges"],"exampleFix":"# before (server flags)\ndgraph alpha --limit \"query-edge=1000000\"\n# after\ndgraph alpha --limit \"query-edge=1000000;encoded-size=10485760\"","handlingStrategy":"try-catch","validationCode":"const [{ count }] = await dgraph.query('{ c(func: type(Person)) { count(uid) } }');\nconst estBytes = count * avgNodeBytes;\nif (estBytes > 1024 * 1024) throw new Error('query would exceed encoded-size; paginate');","typeGuard":null,"tryCatchPattern":"try {\n  return await dgraph.query(q);\n} catch (e) {\n  if (String(e).includes('is bigger than threshold')) {\n    return await fetchInBatches(q); // retry with first/offset pagination\n  }\n  throw e;\n}","preventionTips":["Set --limit \"encoded-size=<bytes>\" appropriate to workload","Paginate large reads instead of expand(_all_)/unbounded recurse","Project only needed predicates in queries","Use async export endpoints for full-dataset reads rather than /query"],"tags":["dgraph","query","limit","response-size"],"backgroundTag":"response-size-limit-exceeded","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}