dgraph-io/badger · error

Cannot use GetSequence in read-only mode.

Error message

Cannot use GetSequence in read-only mode.

What it means

Returned by DB.GetSequence when the database was opened with read-only options. Sequence leasing requires writing lease records to the value log/SSTables to guarantee monotonically increasing integers, which is impossible without write access, so the read-only mode guard rejects the call before any sequence object is created.

Source

Thrown at db.go:1432

			return err
		}
		seq.leased = lease
		return nil
	})
}

// GetSequence would initiate a new sequence object, generating it from the stored lease, if
// available, in the database. Sequence can be used to get a list of monotonically increasing
// integers. Multiple sequences can be created by providing different keys. Bandwidth sets the
// size of the lease, determining how many Next() requests can be served from memory.
//
// GetSequence is not supported on ManagedDB. Calling this would result in a panic.
func (db *DB) GetSequence(key []byte, bandwidth uint64) (*Sequence, error) {
	if db.opt.managedTxns {
		panic("Cannot use GetSequence with managedDB=true.")
	}
	if db.opt.ReadOnly {
		panic("Cannot use GetSequence in read-only mode.")
	}

	switch {
	case len(key) == 0:
		return nil, ErrEmptyKey
	case bandwidth == 0:
		return nil, ErrZeroBandwidth
	}
	seq := &Sequence{
		db:        db,
		key:       key,
		next:      0,
		leased:    0,
		bandwidth: bandwidth,
	}
	err := seq.updateLease()
	return seq, err
}

View on GitHub (pinned to 2a001d466f)

Solutions

  1. Open the DB without badger.ReadOnly if writes (and sequences) are needed
  2. In read-only deployments, generate sequences elsewhere (a writable DB, a counter service)
  3. Guard the call with a check of db.opt.ReadOnly

Example fix

// before
opt.ReadOnly = true
seq, _ := db.GetSequence([]byte("k"), 100) // panics
// after
if !db.opt.ReadOnly {
    seq, _ = db.GetSequence([]byte("k"), 100)
} else { id = externalCounter() }
Defensive patterns

Strategy: validation

Validate before calling

if db.opt.ReadOnly { return errors.New("GetSequence unavailable in read-only mode") }

Try / catch

defer func() {
    if r := recover(); r != nil {
        if strings.Contains(fmt.Sprint(r), "read-only") { /* use external counter */ }
    }
}()

Prevention

When it happens

Trigger: Calling db.GetSequence on a DB opened with badger.ReadOnly option, e.g. opening a shared DB directory as a secondary read-only reader.

Common situations: Read-only replicas mounted for inspection; read-only backup/restore tools that also try to allocate IDs; forgotten ReadOnly flag in config.

Related errors


AI-assisted analysis of dgraph-io/badger@2a001d466f (2026-09-05). Data as JSON: /api/errors/a3abb47e450cb5fa. Report an issue: GitHub.