{"record":{"id":"1cab5586a7a4c3b3","repo":"dgraph-io/badger","slug":"errconflict","errorCode":"ErrConflict","errorMessage":"Transaction Conflict. Please retry","messagePattern":"Transaction Conflict\\. Please retry","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"errors.go","lineNumber":31,"sourceCode":"const (\n\t// ValueThresholdLimit is the maximum permissible value of opt.ValueThreshold.\n\tValueThresholdLimit = math.MaxUint16 - 16 + 1\n)\n\nvar (\n\t// ErrValueLogSize is returned when opt.ValueLogFileSize option is not within the valid\n\t// range.\n\tErrValueLogSize = stderrors.New(\"Invalid ValueLogFileSize, must be in range [1MB, 2GB)\")\n\n\t// ErrKeyNotFound is returned when key isn't found on a txn.Get.\n\tErrKeyNotFound = stderrors.New(\"Key not found\")\n\n\t// ErrTxnTooBig is returned if too many writes are fit into a single transaction.\n\tErrTxnTooBig = stderrors.New(\"Txn is too big to fit into one request\")\n\n\t// ErrConflict is returned when a transaction conflicts with another transaction. This can\n\t// happen if the read rows had been updated concurrently by another transaction.\n\tErrConflict = stderrors.New(\"Transaction Conflict. Please retry\")\n\n\t// ErrReadOnlyTxn is returned if an update function is called on a read-only transaction.\n\tErrReadOnlyTxn = stderrors.New(\"No sets or deletes are allowed in a read-only transaction\")\n\n\t// ErrDiscardedTxn is returned if a previously discarded transaction is reused.\n\tErrDiscardedTxn = stderrors.New(\"This transaction has been discarded. Create a new one\")\n\n\t// ErrEmptyKey is returned if an empty key is passed on an update function.\n\tErrEmptyKey = stderrors.New(\"Key cannot be empty\")\n\n\t// ErrInvalidKey is returned if the key has a special !badger! prefix,\n\t// reserved for internal usage.\n\tErrInvalidKey = stderrors.New(\"Key is using a reserved !badger! prefix\")\n\n\t// ErrBannedKey is returned if the read/write key belongs to any banned namespace.\n\tErrBannedKey = stderrors.New(\"Key is using the banned prefix\")\n\n\t// ErrThresholdZero is returned if threshold is set to zero, and value log GC is called.","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/errors.go#L13-L49","documentation":"ErrConflict is returned from Txn.Commit (commitAndSend; detected via orc.newCommitTs at txn.go:532) when another committed transaction modified keys this transaction read — Badger's optimistic concurrency control. The transaction was NOT committed; the caller must retry with a fresh one.","triggerScenarios":"Two concurrent read-write transactions read the same key and one commits first; CommitAt in managed mode on a conflicting txn (txn_test.go:818); long read-modify-write transactions racing frequent writers.","commonSituations":"Counters/rate-limiters under concurrency, optimistic loops without retry, batch jobs colliding with live traffic.","solutions":["Retry the entire transaction in a loop on ErrConflict with a fresh transaction","Shorten transactions: read late, write promptly, commit fast","Avoid reading keys you don't mutate (shrinks the conflict fingerprint)","Use external locking or managed mode with explicit timestamps for hot keys"],"exampleFix":"// before\nerr := db.Update(func(txn *Txn) error { ...read-modify-write... }) // fails once on conflict\n// after\nfor {\n    err := db.Update(func(txn *Txn) error { ...read-modify-write... })\n    if err == badger.ErrConflict { continue }\n    return err\n}","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"for attempt := 0; attempt < maxRetries; attempt++ {\n    err := db.Update(func(txn *Txn) error { ... })\n    if errors.Is(err, badger.ErrConflict) {\n        time.Sleep(backoff(attempt))\n        continue\n    }\n    return err\n}\nreturn ErrTooManyConflicts","preventionTips":["Always wrap read-modify-write workloads in a conflict-retry loop with backoff","Keep transactions short and read only the keys you write","Avoid holding transactions open across network calls"],"tags":["badger","transaction","concurrency","optimistic-locking"],"backgroundTag":"transaction-conflict-retry","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"}