dgraph-io/badger · error
%s with size %d exceeded %d limit. %s: %s
Error message
%s with size %d exceeded %d limit. %s: %s
What it means
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.
Source
Thrown at txn.go:347
readTs: txn.readTs,
entries: entries,
reversed: reversed,
}
}
func (txn *Txn) checkSize(e *Entry) error {
count := txn.count + 1
// Extra bytes for the version in key.
size := txn.size + e.estimateSizeAndSetThreshold(txn.db.valueThreshold()) + 10
if count >= txn.db.opt.maxBatchCount || size >= txn.db.opt.maxBatchSize {
return ErrTxnTooBig
}
txn.count, txn.size = count, size
return nil
}
func exceedsSize(prefix string, max int64, key []byte) error {
return fmt.Errorf("%s with size %d exceeded %d limit. %s:\n%s",
prefix, len(key), max, prefix, hex.Dump(key[:1<<10]))
}
func (txn *Txn) modify(e *Entry) error {
const maxKeySize = 65000
switch {
case !txn.update:
return ErrReadOnlyTxn
case txn.discarded:
return ErrDiscardedTxn
case len(e.Key) == 0:
return ErrEmptyKey
case bytes.HasPrefix(e.Key, badgerPrefix):
return ErrInvalidKey
case len(e.Key) > maxKeySize:
// Key length can't be more than uint16, as determined by table::header. To
// keep things safe and allow badger move prefix and a timestamp suffix, let'sView on GitHub (pinned to 2a001d466f)
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
Example fix
// before
txn.Set(bigCompositeKey, value)
// after
if len(bigCompositeKey) >= 65000 {
h := sha256.Sum256(bigCompositeKey)
txn.Set(h[:], append(bigCompositeKey, 0x00, byte(0))) // full key data in value
} else {
txn.Set(bigCompositeKey, value)
} Defensive patterns
Strategy: validation
Validate before calling
const maxKeySize = 65000
func keyFits(key []byte) bool { return len(key) < maxKeySize }
if !keyFits(key) { // move payload to value or hash the key before txn.Set } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
AI-assisted analysis of dgraph-io/badger@2a001d466f (2026-09-05).
Data as JSON: /api/errors/149b23bbb97a1fa3.
Report an issue: GitHub.