Generate Consistent Outputs for Similar Inputs
Charta HealthSoftware Engineer · OnsiteLast updated
Design a semantic caching and computation deduplication system that identifies similar inputs, prevents duplicate expensive processing under concurrent load, and maintains consistent, versioned outputs.
Scenario
In an automated medical chart review and data extraction platform, users submit clinical text snippets, notes, or standardized queries to generate structured records (such as risk factor extraction or billing code suggestions). Generating these outputs requires costly model inference and deterministic post-processing. Often, incoming requests contain minor lexical variations, whitespace differences, or synonymous phrasing that represent the exact same semantic intent.
Design a system that returns identical, consistent outputs for semantically similar inputs, caches previous generation results safely, and avoids redundant computation during traffic spikes.
Requirements
Functional Requirements
- Input Processing: Accept raw text queries or clinical input payloads and return the structured generation result.
- Similarity Detection: Determine whether an incoming input is identical or semantically equivalent to a previously evaluated input based on a configurable similarity threshold.
- Output Consistency: Guarantee that similar inputs map to the same canonical output for a given pipeline version.
- Versioning & Updates: Support model or rule-set updates, ensuring stale outputs are either purged, tagged with version numbers, or recomputed when the generation logic evolves.
Non-Functional Requirements
- Latency:
- Under 100 ms p99 for similarity cache hits.
- Raw computation takes ~1.5 to 3 seconds p99 when cache misses occur.
- Throughput: Handle an average of 3,000 queries per second (QPS) with peak loads of 8,000 QPS.
- Concurrency Control: Prevent "thundering herd" / cache stampedes when dozens of identical or near-identical inputs arrive simultaneously.
- Consistency & Durability: Canonical records and mappings must be durably stored and resilient to node failures.
What Your Solution Should Cover
- Similarity Strategy: How similarity is determined (e.g., canonicalization rules, locality-sensitive hashing, or embedding vector search) and where the boundary between identical and divergent outputs lies.
- Concurrency & Request Coordination: How concurrent near-identical requests are collapsed into a single compute job (e.g., SingleFlight, distributed locking, or pub/sub).
- Data Model & Storage: Schemas for storing canonical representations, embeddings/indexes, cached outputs, and version metadata.
- Lifecycle & Invalidation: How changes to prompt templates, model checkpoints, or extraction rules are rolled out without returning inconsistent results.
Hints
Hint 1
Consider a two-tier similarity approach: deterministic normalization/hashing for exact/trivial variations, followed by vector similarity search for semantic variations.
Hint 2
To handle concurrent identical or near-identical inputs without recomputing, look into coordination patterns like Singleflight or distributed locks with waiting queues.
Hint 3
Include the model and pipeline version as a mandatory part of your cache keys and similarity search namespaces to isolate version rollouts.
Follow-up questions
- How do you establish and dynamically tune the similarity threshold to minimize false positive groupings without dropping cache hit rates?
- If an upstream model is updated with a breaking change, how would you design a zero-downtime, staged backfill or warm-up of the similarity cache?
- How does your architecture handle edge cases where two concurrent inputs are mutually similar to each other, but each would map to slightly different nearest neighbors in your vector index?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.