Bird
Raised Fist0

Design a Vector Database - Embedding Storage, ANN Search, HNSW Index

HLDhardInterview freq: medium
Choose your preparation mode4 modes available
▶
Request Trace
Step 1/8
Client/API Gateway

Receive embedding write request

Client sends vector embedding and metadata for storage

Protocol:WebSocket
BeforeClient has embedding vector and metadata ready to send
AfterWrite Ingest Service receives embedding write request
Write Ingest Service

Publish embedding write event

Write Ingest Service serializes and publishes embedding to Kafka topic

Protocol:Kafka Produce
BeforeEmbedding write request received but not yet persisted
AfterEmbedding write event durably stored in Kafka
Kafka Cluster

Deliver embedding write event

Kafka delivers embedding event to consumers for storage and indexing

Protocol:Kafka Consume
BeforeEmbedding event stored in Kafka topic partitions
AfterEmbedding DB and HNSW nodes receive embedding event for processing
Embedding DB (Cassandra)

Store embedding vector

Persist embedding vector with partition key for fast retrieval

Protocol:Cassandra Write
BeforeEmbedding event received by Embedding DB but not stored
AfterEmbedding vector durably stored in Cassandra
HNSW Index Search Nodes

Update HNSW index

Add new embedding vector to HNSW graph index for ANN search

Protocol:HNSW Index Update
BeforeEmbedding stored in Cassandra but not indexed in HNSW graph
AfterHNSW index updated with new vector node
Metadata DB (Elasticsearch)

Index metadata

Update Elasticsearch index with embedding metadata for search

Protocol:ES Index Update
BeforeMetadata not yet indexed in Elasticsearch
AfterMetadata indexed and searchable in Elasticsearch
Client/API Gateway

Receive ANN search query

Client sends approximate nearest neighbor search query

Protocol:WebSocket
BeforeClient has query vector ready
AfterSearch Query Service receives ANN search query
Search Query Service

Query HNSW index

Search Query Service sends query vector to HNSW nodes for ANN search

Protocol:gRPC
BeforeSearch query received but no results yet
AfterANN search results returned from HNSW nodes to Search Query Service
🏗️
Architecture Diagram
Architecture Diagram
┌───────────────────────┐
│    Client/API Gateway  │
└─────────────┬─────────┘
              │ WebSocket
              ▼
┌───────────────────────┐
│   Write Ingest Service │
└─────────────┬─────────┘
              │ Kafka Produce
              ▼
┌───────────────────────┐
│     Kafka Cluster     │
└─────────────┬─────────┘
              │ Kafka Consume
              ▼
┌────────────────────────┐
│  Embedding DB (Cassandra)│
└─────────────┬───────────┘
              │ Cassandra Write
              ▼
┌────────────────────────┐
│  HNSW Index Search Nodes│
└─────────────┬───────────┘
              │ HNSW Index Update
              ▼
┌───────────────────────┐
│  Metadata DB (Elasticsearch) │
└─────────────┬─────────┘
              │ ES Index Update
              ▼
┌───────────────────────┐
│  Search Query Service  │
└─────────────┬─────────┘
              │ gRPC
              ▼
┌───────────────────────┐
│  HNSW Index Search Nodes│
└───────────────────────┘

This architecture shows a vector database system designed for embedding storage and approximate nearest neighbor (ANN) search using a Hierarchical Navigable Small World (HNSW) index. The client sends embedding vectors via WebSocket to the Write Ingest Service, which publishes them to a Kafka cluster for durability and decoupling. Consumers read from Kafka to store embeddings in Apache Cassandra, optimized for high write throughput and fast retrieval. The HNSW Index Search Nodes maintain the ANN graph index, updating it with new vectors for efficient similarity search. Metadata is indexed in Elasticsearch to support text-based queries. For search, the client sends queries to the Search Query Service, which queries the HNSW nodes over gRPC to retrieve nearest neighbors. This design separates ingestion, storage, indexing, and query serving, enabling scalable, low-latency vector search.

Primary Focus

✓

Practice

(1/5)
1. In the News Feed system, at step 4 the Delivery Worker reads from the fan-out queue to push updates to followers. What happens if the follower's cache entry for the feed is expired at this step?
easy
A. The Delivery Worker fetches the latest feed snapshot from the database before pushing
B. The Delivery Worker sends a stale cached feed to the follower
C. The Delivery Worker retries the message later without updating the cache
D. The Delivery Worker skips sending the update and marks it as delivered

Solution

  1. Step 1: Trace Delivery Worker decision at Step 4 -- follower cache TTL expired

    If the cache is expired, the Delivery Worker must fetch fresh data to ensure the follower receives the latest feed snapshot.
  2. Step 2: Evaluate other options

    Skipping (A) causes missed updates; retrying without cache update (C) delays delivery; sending stale data (D) violates freshness guarantees.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Expired cache triggers DB fetch for fresh feed [check]
Hint: Expired cache triggers DB fetch before delivery [check]
Common Mistakes:
2. A company wants to hide internal client IP addresses from external servers while allowing clients to access the internet. Which proxy type should be used to achieve this?
easy
A. DNS proxy, because it resolves domain names for clients
B. Reverse proxy, because it hides the backend servers from clients
C. Load balancer, because it distributes requests evenly across servers
D. Forward proxy, because it hides client IPs from external servers

Solution

  1. Step 1: Identify proxy role in hiding client IPs

    A forward proxy acts on behalf of clients, masking their IP addresses from external servers.
  2. Step 2: Eliminate other options

    Reverse proxies hide backend servers, not clients; load balancers distribute load but don't hide clients; DNS proxies only resolve names.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Forward proxy hides client IPs from external servers [check]
Hint: Forward proxy hides clients; reverse proxy hides servers [check]
Common Mistakes:
3. For a real-time ML inference system requiring minimal latency and high availability during model updates, which architecture is preferable and why?
medium
A. Monolithic model server that loads new models sequentially, blocking requests during reload
B. Single model server with periodic batch reloads and request queueing during reload
C. Blue-green deployment with two identical model serving clusters switching traffic atomically
D. Client-side model loading with periodic updates pushed to clients

Solution

  1. Step 1: Evaluate architectures for latency and availability

    Monolithic (A) and single server batch reload (C) block requests during reload, increasing latency. Client-side loading (D) adds complexity and inconsistent versions.
  2. Step 2: Understand blue-green deployment benefits

    Blue-green (B) runs two clusters; new model loads on inactive cluster, then traffic switches atomically, ensuring zero downtime and minimal latency impact.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Blue-green enables atomic switch with zero downtime [check]
Hint: Blue-green switches traffic atomically, avoiding reload blocking [OK]
Common Mistakes:
4. You need to model a social media feed with millions of users and high write throughput. Which data modeling approach is best to optimize for write scalability and read performance?
medium
A. Embed all posts and comments inside each user document to minimize joins.
B. Use a graph database to model user relationships and posts for efficient traversal.
C. Store all posts in a single collection with a compound index on user and timestamp for fast queries.
D. Use a referencing pattern with separate collections for users, posts, and comments, and denormalize popular posts into user feed documents.

Solution

  1. Step 1: Analyze write scalability and read patterns

    High write throughput and millions of users require scalable writes and efficient feed reads.
  2. Step 2: Evaluate referencing with denormalization

    Referencing separates concerns; denormalizing popular posts into feeds optimizes read performance without large documents.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Referencing plus denormalization balances write scale and read speed [check]
Hint: Reference plus denormalize popular data for scalable writes and fast reads [check]
Common Mistakes:
5. At 10x scale, which component in Netflix's adaptive streaming pipeline is most likely to fail first and why?
hard
A. Personalization service because recommendation model retraining becomes too slow
B. Origin servers because storage I/O bandwidth saturates under high request volume
C. CDN edge servers because WebSocket connections for adaptive bitrate are stateful and cannot be easily sharded
D. Client devices because decoding multiple bitrate streams consumes excessive CPU

Solution

  1. Step 1: Identify stateful bottlenecks at scale

    Personalization retraining (Personalization service because recommendation model retraining becomes too slow) can be scaled horizontally with distributed training. Origin storage (Origin servers because storage I/O bandwidth saturates under high request volume) can be scaled with sharding and CDN caching. Client devices (Client devices because decoding multiple bitrate streams consumes excessive CPU) are outside server control.
  2. Step 2: Analyze CDN edge server scaling challenges

    WebSocket connections for adaptive streaming are stateful and bound to specific edge servers, limiting horizontal scaling and resharding, causing edge server overload at 10x scale.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Stateful WebSocket limits CDN edge server scalability [check]
Hint: Stateful WebSocket limits CDN edge scaling first [check]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard