In-memory state database for Go
StateDB is an in-memory database for Go. The database is built on top of Persistent Adaptive Radix Trees.
StateDB is/supports:
In-memory. Objects and indexes are stored in main memory and not on disk. This makes it easy to store and index any Go data type.
Multi-Version Concurrency Control (MVCC). Both objects and indexes are immutable and objects are versioned. A read transaction has access to an immutable snapshot of the data.
Cross-table write transactions. Write transactions lock the requested tables and allow modifying objects in multiple tables as a single atomic action. Transactions can be aborted to throw away the changes.
Multiple indexes. A table may have one or more indexers for objects, with each indexer returning zero or more keys. Indexes can be unique or non-unique. A non-unique index is a concatenation of the primary and secondary keys.
Watch channels. Changes to the database can be watched at fine-granularity via Go channels that close when a relevant part of the database changes. This is implemented by having a Go channel at each of the radix tree nodes. This enables watching an individual object for changes, a key prefix, or the whole table.
To support lockless readers and transactionality StateDB relies on both the indexes
and the objects themselves being immutable. Since in Go you cannot declare fields
const we cannot stop mutation of public fields in objects. This means that care
must be taken with objects stored in StateDB and not mutate objects that have been
inserted into it. This means both the fields directly in the object and everything
referenced from it, e.g. a map field must not be modified, but must be cloned first!
StateDB has a check in Insert() to validate that if an object is a pointer then it
cannot be replaced with the same pointer, but that at least a shallow clone has been
made. This of course doesn't extend to references within the object.
For "very important objects", please consider storing an interface type instead that contains getter methods and a safe way of mutating the object, e.g. via the builder pattern or a constructor function.
Also prefer persistent/immutable data structures within the object to avoid expensive
copying on mutation. The part package comes with persistent Map[K]V and Set[T].
Here's a quick example to show how using StateDB looks like.
…Read on for a more detailed guide or check out the Go package docs.
StateDB can be used directly as a normal library, or as a Hive Cell.
For example usage as part of Hive, see reconciler/example. Here we show a standalone example.
We start by defining the data type we want to store in the database. There are
no constraints on the type and it may be a primitive type like an int or a
struct type, or a pointer. Since each index stores a copy of the object one should
use a pointer if the object is large.
import (
"github.com/cilium/statedb"
"github.com/cilium/statedb/index"
"github.com/cilium/statedb/part"
)
type ID = uint64
type Tag = string
type MyObject struct {
ID ID // Identifier
Tags part.Set[Tag] // Set of tags
}
// Define header for a formatted table (db/show command)
func (o *MyObject) TableHeader() []string {
return []string{"ID", "Foo"}
}
// Define how to show the object in a formatted table
func (o *MyObject) TableRow() []string {
return []string{strconv.FormatUint(uint64(o.ID), 10), o.Foo}
}With the object defined, we can describe how it should be indexed. Indexes are
constant values and can be defined as global variables alongside the object type.
Indexes take two type parameters, your object type and the key type: Index[MyObject, ID].
Additionally you define two operations: FromObject that takes your object and returns
a set of StateDB keys (zero or many), and FromKey that takes the key type of your choosing and
converts it to a StateDB key.
// IDIndex is the primary index for MyObject indexing the 'ID' field.
var IDIndex = statedb.Index[*MyObject, ID]{
Name: "id",
FromObject: func(obj *MyObject) index.KeySet {
return index.NewKeySet(index.Uint64(obj.ID))
}
FromKey: func(id ID) index.Key {
return index.Uint64(id)
}
// Above is equal to just:
// FromKey: index.Uint64,
Unique: true, // IDs are unique.
}The index.Key seen above is just a []byte. The index package contains many functions
for converting into the index.Key type, for example index.Uint64 and so on.
A single object can also map to multiple keys (multi-index). Let's construct an index for tags.
var TagsIndex = statedb.Index[*MyObject, Tag]{
Name: "tags",
FromObject: func(o *MyObject) index.KeySet {
// index.Set turns the part.Set[string] into a set of keys
// (set of byte slices)
return index.Set(o.Tags)
}
FromKey: index.String,
// Many objects may have the same tag, so we mark this as
// non-unique.
Unique: false,
}With the indexes now defined, we can construct a table.
func NewMyObjectTable(db *statedb.DB) (statedb.RWTable[*MyObject], error) {
return statedb.NewTable[*MyObject](
db,
"my-objects",
IDIndex, // IDIndex is the primary index
TagsIndex, // TagsIndex is a secondary index
// ... more secondary indexes can be passed in here
)
}The NewTable function takes the database, the name of the table, a primary
index and zero or more secondary indexes. The table name must match the regular
expression "^[a-z][a-z0-9_\-]{0,30}$".
NewTable returns a RWTable, which is an interface for both reading and
writing to a table. An RWTable is a superset of Table, an interface
that contains methods just for reading. This provides a simple form of
type-level access control to the table. NewTable may return an error if
the name or indexers are malformed, for example if IDIndex is not unique
(primary index has to be), or if the indexers have overlapping names.
Additionally, it may return an error if another table with the same name
is already registered with the database.
With the table defined, we can now create the database and start writing and reading to the table.
db := statedb.New()
myObjects, err := NewMyObjectTable(db)
if err != nil { return err }To insert objects into a table, we'll need to create a WriteTxn. This locks
the target table(s) allowing for an atomic transaction change.
…Now that there's something in the table we can try out reading. We can
read either using a read-only ReadTxn, or we can read using a WriteTxn.
With a ReadTxn we'll be reading from a snapshot and nothing we do
will affect other readers or writers (unless you mutate the immutable object,
in which case bad things happen).
txn := db.ReadTxn()The txn is now a frozen snapshot of the database that we can use
to read the data.
…In addition to the radix tree based indexes StateDB also supports bitwise Longest Prefix Match (LPM) indexes. These are useful for example for finding the object with the closest IP prefix for a given IP address.
The NetIPPrefixIndex is an LPM index specialized for netip.Prefix:
…The LPMIndex allows defining an index for an arbitrary bit prefix:
…Needless to say, one should keep the duration of the write transactions
as short as possible so that other writers are not starved (readers
are not affected as they're reading from a snapshot). Writing in
batches or doing first a ReadTxn to compute the changes and committing
with CompareAndSwap is a good way to accomplish this as shown above
(optimistic concurrency control).
One should also avoid keeping the ReadTxn around when for example waiting
on a watch channel to close. The ReadTxn holds a pointer to the database
root and thus holding it will prevent old objects from being garbage collected
by the Go runtime. Considering grabbing the ReadTxn in a function and returning
the watch channel to the function doing the for-select loop.
It is fine to hold onto the iter.Seq2[Obj, Revision] returned by the queries
and since it may be iterated over multiple times, it may be preferable to pass
and hold the iter.Seq2 instead of collecting the objects into a slice.
The part package contains persistent Map[K, V] and Set[T] data structures.
These, like StateDB, are implemented with the Persistent Adaptive Radix Trees.
They are meant to be used as replacements for the built-in mutable Go hashmap
in StateDB objects as they're persistent (operations return a copy) and thus
more efficient to copy and suitable to use in immutable objects.
Here's how to use Map[K, V]:
…And here's Set[T]:
…This repository comes with a generic reconciliation utility that watches a table for changes and performs a configurable Update or Delete operation on the change. The status of the operation is written back into the object, which allows inspecting or waiting for an object to be reconciled. On failures the reconciler will retry the operation at a later time. Reconciler supports health reporting and metrics.
See the example application in reconciler/example for more information.
No open issues yet, or sync has not completed.