Design Amazon's sales rank by category
A client resolves DNS, uses a CDN, and reaches scaled web and API servers. Sales logs go to object storage, a Sales Rank Service calculates weekly category ranks, and the Read API serves cached rank results from SQL read replicas.
Design Amazon's sales rank
Diagram after donnemartin/system-design-primer · CC BY 4.0
Client
DNS
CDN
Load Balancer
Web Server
1 · Route request
Sales API
Object Store
2 · Record sales logs
Sales Rank Service
SQL Write Master–Slave
SQL Read Replicas
3 · Calculate ranks
Read API
Memory Cache
4 · Serve ranked category
× Client What it is A user or caller that asks for popular products in a category.
Its job here It starts the request and resolves the service through DNS. It then reaches the CDN or Load Balancer.
Trade-off Public communication can use a REST API, while internal communication can use RPC.
In the primer Communication › Representational state transfer (REST) →
× DNS What it is The name lookup at the front of the service.
Its job here It receives the client lookup and directs the request to the CDN. The CDN can then handle popular content before the Load Balancer.
Trade-off DNS is one of the components introduced to address scalability issues.
In the primer Domain name system › Disadvantage(s): DNS →
× CDN What it is A content delivery network for popular content.
Its job here It receives traffic after DNS and can handle popular content. Requests that continue reach the Load Balancer.
Trade-off A CDN is added to address scalability issues.
In the primer Content delivery network › Pull CDNs →
× Load Balancer What it is A front door that spreads traffic across multiple Web Servers.
Its job here It receives requests from the CDN and passes them to a Web Server. Multiple Web Servers address a scaling bottleneck.
Trade-off Load balancing and horizontal scaling add components to address bottlenecks.
In the primer Load balancer › Horizontal scaling →
× Web Server What it is A reverse proxy that receives the client request.
Its job here It receives traffic from the Load Balancer and forwards it to the Sales API or Read API. This keeps the public request path separate from the API servers.
Trade-off The design adds multiple Web Servers to address bottlenecks.
In the primer Reverse proxy › Load balancer vs reverse proxy →
× Sales API What it is An API server that records sales activity.
Its job here It receives sales traffic from the Web Server and writes its server log files to the Object Store. Those files become input for the Sales Rank Service.
Trade-off The design stores raw server logs instead of managing a distributed file system.
In the primer Application layer › Microservices →
× Read API What it is An API server for the public popular-products request.
Its job here It receives the Web Server request and checks the Memory Cache. On a cache miss, it reads the sales_rank data from SQL Read Replicas.
Trade-off Internal communication can use RPC.
In the primer Communication › Remote procedure call (RPC) →
× Sales Rank Service What it is A service that calculates the past week's most popular products by category.
Its job here It reads Sales API log files from the Object Store and uses MapReduce. It writes sorted aggregate results to the SQL Write Master–Slave.
Examples Trade-off The design should discuss SQL or NoSQL use cases and tradeoffs.
× Memory Cache What it is A fast store for popular content and its sales rank.
Its job here The Read API checks it before SQL Read Replicas. It handles uneven traffic and traffic spikes, while cache misses continue to the replicas.
Trade-off With the large volume of reads, SQL Read Replicas might not handle all cache misses.
In the primer Cache › Cache-aside →
× SQL Write Master–Slave What it is The SQL database that holds the aggregate sales_rank table.
Its job here The Sales Rank Service writes the sorted weekly ranks here. The database replicates the data to SQL Read Replicas.
Trade-off A single SQL Write Master–Slave might be tough at higher write peaks.
In the primer Database › Master-slave replication →
× SQL Read Replicas What it is Replicated SQL data used for read traffic.
Its job here They receive replicated rank data from the SQL Write Master–Slave. They return cache-miss results to the Read API.
Trade-off The replicas might not handle all cache misses at the large volume of reads.
In the primer Database › Master-slave replication →
× Object Store What it is Durable storage for raw Sales API server log files.
Its job here It receives log files from the Sales API and supplies them to the Sales Rank Service. The service uses those files as MapReduce input.
Examples Trade-off The design can store a limited period in the database and the rest in an object store.
× Step 1 of 4
Route the request The client resolves DNS, can use the CDN, then reaches the Load Balancer and a Web Server.
× Step 2 of 4
Record sales logs The Sales API keeps raw server log files in the Object Store.
× Step 3 of 4
Calculate weekly ranks The Sales Rank Service uses MapReduce to aggregate and sort category sales, then writes SQL results.
× Step 4 of 4
Serve a ranked category The Read API checks the Memory Cache, then uses SQL Read Replicas for cache misses.
Click any box for an explanation, or use Next or Play to walk through the four steps.
1 · Route the request The client resolves DNS, can use the CDN, then reaches the Load Balancer and a Web Server.
DNS, the CDN, load balancing, and multiple Web Servers address scalability issues.
2 · Record sales logs The Sales API writes raw server log files to the Object Store.
An Object Store such as Amazon S3 avoids managing a distributed file system.
3 · Calculate weekly ranks The Sales Rank Service uses MapReduce to sum quantities by category and product, then performs a distributed sort.
The sorted list is written to the sales_rank SQL table.
4 · Serve a ranked category The Read API checks the Memory Cache and uses SQL Read Replicas on a cache miss.
Popular rank results should be handled by the cache instead of the database.