Skip to content

perf(server): embed a recall or remember outside the store lock (#1518) - #1585

Merged
ajianaz merged 3 commits into
developfrom
perf/embed-outside-lock-1518
Oct 29, 2026
Merged

ajianaz merged 3 commits into
developfrom
perf/embed-outside-lock-1518

Conversation

@ajianaz

@ajianaz ajianaz commented Oct 29, 2026

Copy link
Copy Markdown
Collaborator

What

  • The embedder slot lock is released before an embed call (the slot holds an Arc), so a slow backend no longer blocks other embeds.
  • The router embeds the query of POST /recall and the content of POST /remember BEFORE it takes the store lock, through a new EmbeddingWarmer handle. The call under the lock then finds the vector in the embedding cache. It starts once the first request has built the embedder and only applies when the embedder caches results (the default cache); otherwise nothing changes.
  • The warm-up sends only what the real call would: content goes through the same validation and privacy filter as a write (secret masked; strict-mode refusal, invalid content and JSON content send nothing), a blank query is skipped. (An automated review flagged the first version, which embedded raw text; it is fixed in the second commit and covered by tests that fail on the raw behaviour.)
  • Router reads the body of these two routes ahead of the handler through a thread-local that read_body consumes, with the same size limit and errors.

Why

#1518: one lock around the whole request put the embedding (a network round trip for openai/ollama) inside the critical section, so throughput stayed flat however many clients there were.

Testing

  • cargo fmt, cargo clippy --workspace --all-targets --locked -- -D warnings, cargo test --workspace --locked, cargo run -p docgen (no diff).
  • New tests: two embeds overlap; a warm-up makes the real embed a cache hit; no warmer for a non-caching embedder; through route: concurrent requests overlap their embedding (fails when the warm-up is disabled: 964 ms vs ~400 ms), read-your-writes, a poisoned store lock does not fail later requests, a secret is masked before the warm-up, strict mode sends nothing.
  • Receipts (same harness as the baseline perf-aa75441, Modal 2 vCPU, perf-2652f0e-embed-outside-lock-*.json):
    • 50 ms mock remote embedder, local: 8 clients 19 to 141 ops/s, median 420 to 59 ms; 1 client unchanged.
    • Real ONNX (Modal): 1 client 45.7 to 48.5 ops/s; 4 clients 44.9 to 70.0; 16 clients 51.5 to 66.5; p99 at 4 clients 143 to 87 ms. No errors.
    • uteke bench (in-process, does not touch the server path): insert/s 13.5 to 10.4 at 100 rows and 52.9 to 49.8 at 1000, recall avg 12.1 to 14.6 and 15.6 to 15.1 ms; run-to-run noise at this size (the 100-row insert is dominated by model warm-up), not re-run.
  • Not touching recall ranking, import or the CLI local recall path.

The embedder slot lock is released before an embed call, and the router embeds the query or content of POST /recall and /remember before taking the store lock so the call under the lock hits the embedding cache. With a 50 ms mock embedder, 8 clients go from 19 to 141 ops/s.
…1518)

The warm-up handle carries the privacy filter: content is validated and masked like a write, a strict-mode refusal or invalid content embeds nothing, and a blank query is skipped. Adds the Modal result for the ONNX backend.
/// A warm-up handle once the embedder is built and caches its results.
/// It never initialises the embedder, so it is cheap to ask for.
pub fn embedding_warmer(&self) -> Option<EmbeddingWarmer> {
let handle = self.embedder.lock().ok()?.clone()?;
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

The handle was stored once; it now follows the store (embedder and privacy config) on each request while the store lock is held.
@ajianaz
ajianaz merged commit f66b8c9 into develop Oct 29, 2026
14 checks passed
@ajianaz
ajianaz deleted the perf/embed-outside-lock-1518 branch October 29, 2026 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants