{"record":{"id":"149b23bbb97a1fa3","repo":"dgraph-io/badger","slug":"s-with-size-d-exceeded-d-limit-s-s","errorCode":null,"errorMessage":"%s with size %d exceeded %d limit. %s:\n%s","messagePattern":"(.+?) with size (.+?) exceeded (.+?) limit\\. (.+?):\n(.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"txn.go","lineNumber":347,"sourceCode":"\t\treadTs:   txn.readTs,\n\t\tentries:  entries,\n\t\treversed: reversed,\n\t}\n}\n\nfunc (txn *Txn) checkSize(e *Entry) error {\n\tcount := txn.count + 1\n\t// Extra bytes for the version in key.\n\tsize := txn.size + e.estimateSizeAndSetThreshold(txn.db.valueThreshold()) + 10\n\tif count >= txn.db.opt.maxBatchCount || size >= txn.db.opt.maxBatchSize {\n\t\treturn ErrTxnTooBig\n\t}\n\ttxn.count, txn.size = count, size\n\treturn nil\n}\n\nfunc exceedsSize(prefix string, max int64, key []byte) error {\n\treturn fmt.Errorf(\"%s with size %d exceeded %d limit. %s:\\n%s\",\n\t\tprefix, len(key), max, prefix, hex.Dump(key[:1<<10]))\n}\n\nfunc (txn *Txn) modify(e *Entry) error {\n\tconst maxKeySize = 65000\n\n\tswitch {\n\tcase !txn.update:\n\t\treturn ErrReadOnlyTxn\n\tcase txn.discarded:\n\t\treturn ErrDiscardedTxn\n\tcase len(e.Key) == 0:\n\t\treturn ErrEmptyKey\n\tcase bytes.HasPrefix(e.Key, badgerPrefix):\n\t\treturn ErrInvalidKey\n\tcase len(e.Key) > maxKeySize:\n\t\t// Key length can't be more than uint16, as determined by table::header.  To\n\t\t// keep things safe and allow badger move prefix and a timestamp suffix, let's","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/txn.go#L329-L365","documentation":"This panic/error is produced by exceedsSize in txn.go:347 when a key (or value) submitted to a transaction exceeds the maximum allowed size (maxKeySize = 65000 for keys). Badger enforces key size limits to keep its internal data structures (memtables, indexes, LSM keys) efficient; oversized keys are rejected before being written. The message includes a hex dump of the first 1KB of the offending key to help identify it.","triggerScenarios":"Calling txn.Set() (or txn.Modify) with a []byte key whose length is >= 65000 bytes, while in non-managed mode where modify() calls exceedsSize with maxKeySize. Also triggered by SetEntry/Add with oversized keys.","commonSituations":"Storing composite or serialized data in the key instead of the value; concatenating many fields into one key; encrypting keys (which grows them past the limit); accidentally passing the value as the key argument to Set.","solutions":["Check key length before txn.Set: if len(key) >= 65000, move data to the value and use a short/hash key","Hash or truncate long keys (e.g. sha256 of the logical key) and keep the full data in the value","Split the oversized key into a key plus part of the payload stored as value","If the limit seems wrong for your workload, review whether you are running in managed mode or with a badger version with different limits"],"exampleFix":"// before\ntxn.Set(bigCompositeKey, value)\n// after\nif len(bigCompositeKey) >= 65000 {\n    h := sha256.Sum256(bigCompositeKey)\n    txn.Set(h[:], append(bigCompositeKey, 0x00, byte(0))) // full key data in value\n} else {\n    txn.Set(bigCompositeKey, value)\n}","handlingStrategy":"validation","validationCode":"const maxKeySize = 65000\nfunc keyFits(key []byte) bool { return len(key) < maxKeySize }\nif !keyFits(key) { // move payload to value or hash the key before txn.Set }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep keys small and fixed-shape; store bulk data in values","Assert key length in a helper wrapper around txn.Set in your data-access layer","Be careful with encodings/encryption that inflate key size"],"tags":["badger","key-size-limit","lsm","storage"],"backgroundTag":"key-size-limit-exceeded","analyzedSha":"2a001d466f6b71a917319a1db41f99860e16e269","analyzedAt":"2026-09-05T13:00:02.264Z","contentChangedAt":"2026-09-05T13:00:02.264Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}