← Back to Blog
Engineering

True Serverless Vector Search: Optimizing for 40ms Latency on 3GB RAM

How we achieved 10x speedup in serverless vector search without upgrading hardware, using Rust SIMD, parallel I/O, and smart caching.

Published
Read
8 min read
Author
Majid Fekri, CTO

The Constraint Box

In the vector database world, the standard architecture is Stateful & Heavy. To get sub-50ms latency, the industry standard is to provision a Redis or Elasticsearch cluster with 30GB+ of RAM, load the entire HNSW graph into memory, and keep it running 24/7.

At Moorcheh, our architecture is Stateless & Serverless. We run our search engine inside ephemeral functions (like AWS Lambda) with strict resource limits:

  • RAM: 3GB (Fixed)
  • vCPU: 2 Cores
  • State: None (Cold starts possible)

Early in our beta, we hit a wall. Our search endpoint was averaging 400ms latency. For a background job, that's fine. For a real-time AI Agent trying to hold a conversation, it's an eternity.

The advice we got was unanimous: "You can't do high-performance vector search on Lambda. Just spin up a dedicated cluster."

We refused. Dedicated clusters mean "Idle Costs" for our users. We wanted to preserve our Scale-to-Zero utility pricing. So, instead of throwing hardware at the problem, we decided to engineer our way out of it.

The Autopsy: Where was the 400ms going?

We profiled our request lifecycle and found three distinct bottlenecks, each eating ~120-180ms:

  1. The Auth Layer: We were performing redundant, heavy security checks on every stateless invocation.
  2. I/O (Index Fetching): Because we refuse to use HNSW approximations (we demand full-scan accuracy), we have to fetch the binary index from object storage on every request. This fetch was saturating the network throughput.
  3. Compute (Distance Calc): Calculating distance across 10,000 vectors (1024 dimensions) using standard libraries was CPU-bound on the limited 2 vCPUs.

Request Lifecycle Breakdown

Before400ms
Auth 120ms
I/O 180ms
Compute 100ms
After40ms
5ms
16ms
14ms

↓ 10x speedup across all components

Here is how we crushed all three.

Optimization 1: Parallel I/O & Custom Sharding

The biggest cost in a stateless architecture is moving data from Disk (S3/Object Store) to RAM.

We analyzed our MIB (Maximally Informative Binarization) files and realized we were treating them as monolithic blobs. We rewrote our storage layer to implement Micro-Sharding.

Instead of fetching one large index file, we break the binary index into optimized 4KB-aligned chunks that match the Lambda's filesystem block size. We then use aggressive Parallel Range Requests to saturate the Lambda's network interface card (NIC).

Result: We reduced the "Time-to-First-Byte" for the index load by 65%.

Optimization 2: The Rust Rewrite (SIMD)

Python and Node.js are great for APIs, but they are too slow for bitwise math at scale.

We identified the "Hot Path"—the exact loop where the Efficient Distance Metric (EDM) is calculated—and rewrote it in Rust.

But we didn't just write standard Rust. We utilized SIMD (Single Instruction, Multiple Data) intrinsics. Since our vectors are binarized (composed of bits, not floats), we can leverage CPU-level instructions (POPCNT, XOR) to compare 512 bits in a single CPU cycle.

rust
// Simplified view of our SIMD Hamming calculation
pub fn hamming_distance_simd(a: &[u8], b: &[u8]) -> u32 {
    a.iter()
     .zip(b.iter())
     .map(|(x, y)| (x ^ y).count_ones()) // Hardware XOR + POPCNT
     .sum()
}

This effectively allowed us to perform a "Full Scan" of the database faster than other engines can traverse an HNSW graph.

Optimization 3: Streamlined Auth

We realized our authentication middleware was performing a database lookup for API key validation on every single request. In a 400ms window, that DB roundtrip was costly.

We redesigned the Auth layer to use a Stateless Cryptographic Signature approach (similar to JWTs but optimized for API keys) combined with an ultra-fast localized cache. This removed the network hop entirely for valid, active sessions.

The Result: 40ms End-to-End

After deploying these three changes, we re-ran the benchmarks on the exact same hardware (3GB RAM, 2 vCPU).

  • Previous Latency: 400ms
  • New Latency: 40ms
  • Accuracy Loss: 0.0%

We achieved a 10x speedup without upgrading the servers, without introducing "Approximate" search errors, and without forcing our users to pay for idle RAM.

Constraints don't limit innovation; they force it. When you can't scale vertically (more RAM), you must innovate laterally (better algorithms).

§ More field notes

All posts →