skip to content

Why does an embedded vector store usually allow only one writing process, and how do you work within that without changing products?

level: middleimportance: must knowfreq 50%

answer

  1. a library writing local files
  2. no server to order writers
  3. lock error or silent damage
  4. one writer, many readers
  5. funnel writes through one owner

basics

~20 s

An embedded vector store is a library writing local files with no server to order writers, so most lock out a second writer or assume one. Funnel all writes through one owning process, or run the same engine as a server.

solid answer

~50 s

An embedded vector store is an engine linked into your process that keeps its state in local files. Its in-memory structures (the index graph, a write buffer, the list of live files) are coordinated only inside that one process, so a second process holds its own stale view and both may append to or rewrite the same files. Stores typically react with an exclusive lock that makes the second open fail, one writer with many read-only openers, versioned commits that reject conflicting writers, or no lock at all, which means lost writes or damaged files. Typical triggers are forked web workers, an ingestion job beside the API, or two containers on one volume. Without switching products, make one process the only writer, fed by a queue, open everyone else read-only and have them reopen to see new data, or use the same engine's server mode if it has one.

code

pseudocode · 14 lines
pseudocode
# illustrative: one ingestion worker is the only writer
store = open_store(path, mode = "read_write")
loop:
    batch = queue.take(max_items = 500, wait_seconds = 2)
    if batch is empty:
        continue
    store.upsert(batch.records)      # keyed by stable record ID
    store.flush()                    # batch is durable on disk
    queue.ack(batch)                 # crash before this line -> batch is replayed

# serving processes never write
reader = open_store(path, mode = "read_only")
every 30 seconds:
    reader.refresh()                 # pick up files the writer published

go deeper

for a junior

Recall that an embedded store is a library writing local files, and that most allow only one process to write at a time.

for a middle

Explain why: in-memory state, file rewrites and index mutation are coordinated inside one process only. Name the three ways a store reacts to a second writer.

for a senior

Diagnose the forked-workers and shared-volume cases from their symptoms, and lay out the single-writer queue with acknowledge-after-flush and read-only openers.

for a principal

Decide when the single-writer pattern has run out, typically writes from several machines or an availability need beyond one host, and move to a server shape on purpose rather than by accident.

## What "embedded" means An **embedded vector store** is a storage engine shipped as a library: it runs inside the application's process and keeps its data in files on a local disk, the way an embedded relational database keeps a single file. Compared with an in-process index it adds **persistence** (the data survives a restart) and usually metadata storage and filtering. Compared with a server it lacks a **single long-running owner** that every client goes through. That missing owner is the whole story of this question. ## Why one writer A server serialises writes because every write passes through it. An embedded engine has no such choke point, and several pieces of its state are hard to share across processes: 1. **In-memory structures.** The graph or partition lists, a buffer of recent writes, caches and counters live in the writing process's memory. A second process builds its own copy and never sees the first one's changes until it reloads. 2. **File rewrites.** Many engines append new data files and periodically merge or rewrite them, then update a small manifest that lists which files are live. Two processes doing this independently can each publish a manifest that forgets the other's files. 3. **Index mutation.** Inserting into a graph index rewires neighbours. Two writers editing overlapping regions without a shared lock produce a structure neither intended. 4. **No arbiter.** Cross-process locking needs either an operating-system file lock or a coordinator, and file locks are only as reliable as the file system under them. So most embedded engines either **take an exclusive lock** or **document a single-writer assumption**. Being thread-safe inside one process says nothing about several processes. ## How the second writer shows up Teams rarely decide to run two writers; they discover one: - A web server **forks four worker processes**, and each opens the store at startup. - A **background ingestion job** opens the store while the API process already has it open. - Two **containers mount one shared volume** for "high availability". - The volume is a **network file system** whose locks are advisory or unsupported, so even a store that locks lets both writers in. ## How a store reacts to a second writer | Store behaviour | What you see with two writers | How you find out | |---|---|---| | Exclusive lock | Second open fails with a lock or busy error | Immediately, at startup | | One writer, many readers | Second writer fails; readers open read-only | Immediately, if writers are opened in write mode | | Versioned commits | Conflicting commits are retried or rejected; only some operations run concurrently | At write time, as conflict errors under load | | No cross-process lock | Writes vanish, results go stale, files fail to open later | Days later, as missing documents or a corrupt store | The last row is the dangerous one, because nothing errors at the time of the mistake. The versioned-commit row is why the question says "usually": a few engines that write immutable file versions allow several writers, at the price of conflict handling. ## Working within it without changing products 1. **One writer process.** Pick one process, usually an ingestion worker, as the only one that opens the store for writing. Everything else that wants to write puts a request on a queue that the worker drains. 2. **Read-only openers.** Serving processes open the store read-only, where the engine supports it, and reopen or refresh on an interval to pick up files the writer published. When a refresh makes new data searchable is a separate subject; here the point is that readers never write. 3. **Same engine, server mode.** Many embedded engines also ship a server mode. Running it turns the server into the single owner and the application processes into clients, without migrating data to another product. 4. **Acknowledge after flush.** The writer acknowledges a queued request only after the batch is durable, so a crash replays the batch instead of losing it. Writes keyed by a stable ID make the replay harmless. ## What you still own Even with one writer, the embedded shape leaves several duties with the team, because the files sit on a disk it controls: - **Durability**: the data is as safe as that disk and whatever copies of it the team makes. - **Disk space** for data files, plus room for the merges and rewrites the engine runs in the background. - **Format changes**: a new engine version may need a migration step before old files open, so upgrades are planned, not automatic. - **Availability**: the store answers only while the writer's host is up, and a second host cannot take over writing without a hand-off. When several machines must write, or the writer's host becomes a single point of failure the product cannot accept, the single-writer pattern has run out and the store needs a real server, self-run or managed.

  • What changes if the two processes run on different machines sharing a network volume?
    File locks on network file systems may be advisory, unsupported or unreliable after a network blip, so a store that locks correctly on a local disk can still let two writers in. Treat a shared network volume as unsafe for an embedded store's write path: keep one machine as the only writer, or move to a server that owns the files on its own disk.
  • Can you scale reads while keeping one writer?
    Yes, up to a point. On one machine, many processes can open the files read-only and refresh periodically. Across machines, the writer publishes copies of the files and readers load them, which adds a propagation delay. When readers on many machines need fresh data continuously, a server shape is simpler than shipping files around.

  • The store is thread-safe, so several processes can write to it as well.
  • No error appeared, so two writing processes must be working fine.
  • A shared network volume lets every container write one embedded store safely.
  • The only fix for a second writer is migrating to a different vector database.
  • Readers in other processes always see new writes at once without reopening.