Embedded vector database for Rust and Python
How a vector database that runs inside your process works, what it asks of your application, and when it fits.
On this page
What does “embedded” mean?
An embedded database is a library, not a service. ITZMA links into your Rust or Python application and runs in its process. There is no database server to deploy, no port to open and no cluster to size.
What makes ITZMA different from other embedded databases is where the data lives. ITZMA keeps everything in an object store such as Amazon S3, Google Cloud Storage or Azure Blob Storage. The bucket is the only durable state, so any process that can reach it can write, and any process can query: a web server, a batch job and a notebook can all open the same namespace.
With ITZMA’s local filesystem backend, processes need access to the same directory. With a supported cloud object store, processes on different machines can open the same namespace. The local filesystem backend supports creating namespaces, writing and querying. Compaction and indexing require conditional updates, which this backend does not support. Object storage backends and requirements lists what each backend supports.
There is still no coordinator, lock or leader between them. Correctness rests on two operations the object store already provides: put-if-absent, which writes an object only if its key is free, and conditional update, which replaces an object only if nobody changed it first.
A working example
A namespace holds documents that share a schema: a vector dimension, a distance metric and typed attributes, all fixed when the namespace is created. This example uses the in-memory store; swap in an s3://, gs:// or az:// URL (Python) or a cloud ObjectStore (Rust) and nothing else changes.
import itzma
from itzma import attr
store = itzma.Store("memory://") # or "file:///var/lib/itzma", "s3://bucket/prefix", "gs://bucket/prefix", "az://container/prefix"
schema = itzma.Schema(
dim=4,
metric=itzma.Metric.COSINE,
attrs={"tag": itzma.AttrType.STRING, "year": itzma.AttrType.NUMBER},
)
ns = itzma.Namespace.create(store, "docs", schema)
ns.upsert(
[
{"id": "a", "vector": [1, 0, 0, 0], "attrs": {"tag": "news", "year": 2024}},
{"id": "b", "vector": [0, 1, 0, 0], "attrs": {"tag": "blog", "year": 2021}},
]
)
hits = ns.query([0.9, 0.1, 0, 0], top_k=5, filter=attr("year") >= 2022)
for hit in hits:
print(hit.id, round(hit.distance, 4), hit.attrs)
# A second handle in the same process opens the namespace by id.
again = itzma.Namespace.open(store, "docs")
print(again.schema) use std::sync::Arc;
use itzma_core::{AttrSchema, AttrType, Document, Metric, Namespace, Schema};
use object_store::memory::InMemory;
#[tokio::main]
async fn main() -> itzma_core::Result<()> {
let store = Arc::new(InMemory::new());
// Dimension, metric and the type of every attribute are fixed for the life
// of the namespace.
let schema = Schema::new(3, Metric::Cosine)
.with_attrs(AttrSchema::new().field("title", AttrType::Text));
let ns = Namespace::create(store.clone(), "articles", schema).await?;
// One call is one atomic write. It returns the sequence number.
let seq = ns
.upsert(vec![
Document {
id: "intro".into(),
vector: vec![0.9, 0.1, 0.0],
attrs: Some(serde_json::json!({ "title": "Introduction" })),
sparse: None,
},
Document {
id: "faq".into(),
vector: vec![0.0, 1.0, 0.2],
attrs: None,
sparse: None,
},
])
.await?;
assert_eq!(seq, 1);
// Closest first. Lower distance means closer, for every metric.
let hits = ns.query(&[1.0, 0.0, 0.0], 2).await?;
assert_eq!(hits[0].id, "intro");
ns.delete(vec!["faq".into()]).await?;
// A second handle in the same process sees the same data.
let other = Namespace::open(store, "articles").await?;
assert_eq!(other.query(&[0.0, 1.0, 0.0], 5).await?.len(), 1);
Ok(())
}Getting started lists the dependencies for each language.
How do several processes share a namespace?
A Namespace value is a handle: the store, the namespace id, the schema and a few hints. It holds no lock, so any number of handles, in one process or many, can work on one namespace at once.
Each upsert or delete call validates its whole batch, then writes it as one object at the next sequence number with put-if-absent. If another writer took that number first, the call moves past it and tries again. Concurrent writers get distinct sequence numbers, and for the same id the higher one wins. Once the call returns, the batch is in the object store, and a query that starts after that sees it, from any handle in any process.
This script starts four worker processes. Each opens its own handle and writes a batch to the same namespace, on a local directory:
from multiprocessing import Pool
import itzma
URL = "file:///tmp/itzma-demo"
def write(worker: int) -> int:
# Each process opens its own handle. Nothing is shared but the store.
ns = itzma.Namespace.open(itzma.Store(URL), "events")
rows = [{"id": f"w{worker}-{i}", "vector": [worker, i, 0, 1]} for i in range(100)]
return ns.upsert(rows)
if __name__ == "__main__":
store = itzma.Store(URL)
itzma.Namespace.create(store, "events", itzma.Schema(dim=4, metric=itzma.Metric.L2, attrs={}))
with Pool(4) as pool:
print("sequence numbers:", sorted(pool.map(write, range(4))))
# A handle opened afterwards sees every batch that returned.
ns = itzma.Namespace.open(store, "events")
print(ns.query([2, 50, 0, 1], top_k=3).ids)It prints four distinct sequence numbers, [1, 2, 3, 4], whatever order the workers ran in.
In a web server, open each namespace once per process and share the handle across requests. Under a forking server such as gunicorn, open it in each worker, not in the master: a handle opened before a fork refuses to work in the child and says so. Python API shows a FastAPI app and a gunicorn hook. Guarantees lists exactly what a write and a query can rely on.
What does the application have to run?
A server-based database compacts and indexes in its own background threads. An embedded one runs only when your code calls it, so that work is the application’s job.
Writes land in a log, and queries read the log too, so new data is searchable at once. But a long log makes queries slower, and an index is only built for compacted segments. Three operations keep a namespace in shape:
compactfolds the log into immutable segments, and merges segments when there are too many.indexbuilds an HNSW or SQ8 index for each segment that has none.gcdeletes segments that a merge replaced, once they are old enough that no reader still needs them.
maintain is the one call to schedule: it compacts when the log is long enough, then collects. Run it and index from cron, a loop or a worker process. In Python, start_maintenance runs both on a background loop that you start explicitly:
m = ns.start_maintenance(timedelta(seconds=60)) # maintain + index every minute
...
m.stop() # or ns.close()Compaction and indexing publish a new manifest with a conditional update, so they need a store that has one: S3, GCS, Azure or memory. A local directory has no conditional update, so there you can create, write and query, but not compact or index. Several processes can run maintenance against one namespace; racing compactors cannot publish different answers. Maintenance covers the options and the defaults.
When does an embedded vector database fit?
It fits when:
- Your application is in Rust or Python, and vector search is one part of it rather than a service of its own.
- You already have object storage and would rather not run, secure and upgrade a database server.
- Several processes, jobs or short-lived workers need the same vectors, and the bucket is the one thing they all reach.
- You want durability from the object store: a write that returned is in the bucket.
It fits less well when:
- Callers in other languages need the data. ITZMA has Rust and Python APIs only, so other languages would need a service you write around it.
- Every query must be fast from cold. A query that misses the cache reads from object storage, which costs round trips; give long-lived processes a cache (Caching and read budgets).
- Nobody can own maintenance. Something has to schedule
maintainandindex. - You need features ITZMA does not have: vector queries do not paginate, and attribute fields cannot be added after a namespace is created. Limits lists the rest.
ITZMA is in beta, and its APIs may change between releases.
Where to go next
- Getting started: install it and run a first query in Rust or Python.
- Concepts: namespaces, documents, the log, segments and the manifest.
- Vector search on object storage: what ITZMA reads and writes in the bucket, and what that costs.
