Theme

Interactive diagram · architecture

Design a key-value cache to save the results of the most recent web server queries — interactive diagram

Design a key-value cache to save recent query results A client resolves DNS, then requests pass through a load balancer and web server to the Query API. The API checks the memory cache. On a cache miss it uses the reverse index service and document service, then stores the result in the cache. Design Query Cache Diagram after donnemartin/system-design-primer · CC BY 4.0 Client DNS Load Balancer Web Server Query API Memory Cache 1 · Cache hit Reverse IndexService DocumentService 2 · Cache miss 3 · Cache refresh
✕

STEP 1

Cache hit

The Query API finds a popular query in memory and returns the cached result.

Flow

Client → Web Server → Query API → Memory Cache.

✕

STEP 2

Cache miss

The API finds matching documents, gets titles and snippets, then saves the contents in the cache.

Flow

Query API → Reverse Index Service → Document Service → Memory Cache.

✕

STEP 3

Cache refresh

Cached entries expire so page changes, removals, additions, and rank changes can be refreshed.

✕

Load Balancer

What it is

A load balancer spreads incoming requests.

Its job here

It receives requests from the Client. It sends each request to a Web Server.

Trade-off

Load balancers add another component to operate.

In the primer

Load balancer › Layer 7 load balancing →

✕

Query API

What it is

The Query API parses and processes a search query.

Its job here

It receives the web request and checks the Memory Cache. On a miss, it asks the Reverse Index Service for matching documents.

In the primer

Application layer › Microservices →

✕

Reverse Index Service

What it is

This service finds documents that match a query.

Its job here

It receives the normalized query from the Query API. It ranks matching results and passes the top ones to the Document Service.

In the primer

Database › Use good indices →

✕

Document Service

What it is

This service returns document titles and snippets.

Its job here

It receives top matches from the Reverse Index Service. It returns contents for the Query API to place in the Memory Cache.

In the primer

Database › Document store →

✕

Memory Cache

What it is

The Memory Cache holds query results in memory.

Its job here

It receives lookups from the Query API. On a hit it moves the entry to the front of the LRU list and returns cached contents; on a miss, the Query API uses the Reverse Index Service.

Examples

  • Redis
  • Memcached

Trade-off

Limited memory means older entries need to expire.

In the primer

Cache › Cache-aside →

←/→ step · Space play/pause · R replay · Home/End jump

Detail · follows playback, or click any box or step to pin it

Click any box for an explanation, or use Next or Play to walk through the three steps.

1 · Cache hit

Popular queries return from the Memory Cache.

The API updates the entry’s position at the front of the LRU list and returns cached contents.

2 · Cache miss

The API finds, ranks, and enriches matching documents.

The result is placed at the front of the Memory Cache’s LRU list.

3 · Cache refresh

A time to live refreshes cached entries.

Use it when page contents, pages, or page ranks change.