ITZMA is still taking shape, and its API can change between releases. Version 0.2.1: serde_with security fix
Docs / User guide

Writing

Batched upserts and deletes, safe with many concurrent writers, with optional idempotency keys.

On this page

Upsert and delete

upsert and delete validate everything first, so one bad document refuses the whole call with nothing written. Then the batch is written as one object with put-if-absent at the next sequence number, which the call returns. Upserting an existing id replaces the whole document.

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}},
    ]
)
let doc = |v: f32| Document { id: "a".into(), vector: vec![v], attrs: None, sparse: None };
assert_eq!(ns.upsert(vec![doc(1.0)]).await?, 1);
assert_eq!(ns.upsert(vec![doc(5.0)]).await?, 2); // replaces "a"

A delete takes ids and is one WAL entry too:

rust
ns.delete(vec!["a".into()]).await?;
assert!(ns.query(&[1.0], 10).await?.is_empty());

What happens when writers race?

If another writer took the next sequence number, the writer looks past it and tries again, up to 32 times (WriteContention). There is no coordinator, lock or leader. Concurrent writers get distinct sequence numbers, and the higher one wins for the same id.

Batch size

A batch is one object and is refused above 64 MiB uncompressed (BatchTooLarge). Split bigger loads into several batches; each is atomic, the whole is not.

In Python, a big unkeyed write is split into batches for you. If one fails, the exception’s last_seq tells you how far it got.

How does a retry avoid writing twice?

A write that fails after the request was sent may or may not have landed. A plain retry can append it twice, which is harmless for the end state of upserts and deletes but can overwrite a newer write to the same id. For loads that must land once, use a key. The first attempt passes the key; a retry passes the same key and the sequence number it started from, and gets the original’s sequence number back if the first attempt landed.

start = seq
try:
    ns.upsert([{"id": "important", "vector": [1.0] * DIM}], key="load-2026-09-29")
except itzma.ItzmaError:
    ns.upsert(
        [{"id": "important", "vector": [1.0] * DIM}],
        key="load-2026-09-29",
        retry_from=start,
    )
let seq = ns.upsert_keyed(&WriteKey::new("load-7/batch-1"), batch()).await?;

// The acknowledgement never arrived, so the caller tries again from
// the last seq it saw committed (here nothing, so 1).
let retry = WriteKey::retry("load-7/batch-1", 1);
assert_eq!(ns.upsert_keyed(&retry, batch()).await?, seq);

In Rust the keyed forms are upsert_keyed and delete_keyed; in Python, key= and retry_from= on upsert and delete. Keyed writes do not stop two submits of one key running at the same time.

NumPy, Arrow and Polars

Python writes take three shapes: a list of dicts, NumPy columns (ids=, a 2-D vectors= matrix of any float dtype, attrs=), or an Arrow table or stream from pyarrow, Polars or DuckDB.

# numpy: ids, a 2-D matrix of any float dtype, and one attrs dict per row.
n = 10_000
seq = ns.upsert(
    ids=[f"np-{i}" for i in range(n)],
    vectors=rng.standard_normal((n, DIM)),
    attrs=[{"shard": i % 4} for i in range(n)],
)

# Arrow: an `id` column, a fixed-size list `vector` column, and one column per attribute.
vectors = rng.standard_normal((1_000, DIM)).astype(np.float32)
table = pa.table(
    {
        "id": [f"pa-{i}" for i in range(1_000)],
        "vector": pa.FixedSizeListArray.from_arrays(pa.array(vectors.ravel()), DIM),
        "shard": [i % 4 for i in range(1_000)],
    }
)
ns.upsert(table)

# Polars frames (and DuckDB results) go in the same way.
ns.upsert(pl.from_arrow(table).with_columns(pl.col("id").str.replace("pa-", "pl-")))

An Arrow table or stream is read one record batch at a time and written in chunks of about target_bytes (32 MiB by default), so a load of any size runs in bounded memory.

In Rust, upsert_columns takes a DocColumns (ids, a row-major f32 matrix, typed attribute columns) as one batch, and upsert_stream takes a stream of them, cut into objects of about StreamOptions::target_bytes and written in order. A failed stream reports how much landed.

rust
// Ids "a" and "b", vectors [1, 0] and [0, 1].
let batch = DocColumns::new(b"ab".to_vec(), vec![0, 1, 2], vec![1.0, 0.0, 0.0, 1.0], 2)?;
assert_eq!(ns.upsert_columns(&batch).await?, 1);

Both have keyed forms, upsert_columns_keyed and upsert_stream_keyed, with the rules of upsert_keyed. A keyed stream writes chunk n under <key>#<n>, so its key can be at most 107 bytes. Don’t give a plain keyed write a key of that form, or the two can be taken for each other. A retry with WriteKey::retry, the same input and the same target_bytes cuts the same chunks, finds the ones that landed and writes only the rest, however the input is batched the second time. Start it from the failed attempt’s first_seq, or, when that is None, from the namespace’s next sequence number before the first attempt.

Docs
esc

Loading the docs…