API design for e-commerce product recommendation system

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2020-10-09

Problem Statement

Design a scalable and personalized recommendation system API for an e-commerce platform that suggests relevant products to users based on their behavior, history, and real-time interactions. The system should handle millions of users and products, ensuring low latency, high availability, and relevance accuracy.


๐Ÿงพ Context


E-commerce platforms (like Amazon, Flipkart, or Myntra) use recommendation systems to improve user engagement and conversion rates by showing products users are likely to buy.


Recommendations can be based on:


โ€ขUser behavior (past purchases, clicks, cart activity),
โ€ขProduct similarity (category, price, features),
โ€ขGlobal trends (popular items, deals), or
โ€ขCollaborative filtering (what similar users liked).


Your task is to design a high-level architecture and API design that supports personalized product recommendations at scale.


๐Ÿงฑ Problem Statement

Design an E-commerce Product Recommendation API that fetches and delivers personalized product suggestions for users in real time, based on behavioral and contextual data.
The system must be scalable, fault-tolerant, and low-latency, capable of serving millions of users concurrently.


๐Ÿ“‹ Functional Requirements


1.Core API Endpoints
โ€ขGET /recommendations/{user_id} โ†’ Returns a list of recommended products for a user.
โ€ขGET /recommendations/trending โ†’ Returns globally trending or top-selling products.
โ€ขPOST /events โ†’ Logs user activity (e.g., product view, cart add, purchase).
โ€ขGET /recommendations/similar/{product_id} โ†’ Returns products similar to a given product.
2.Recommendation Types
โ€ขPersonalized recommendations based on user history.
โ€ขSimilar product suggestions.
โ€ขPopular or trending products (for new users).
โ€ขRecently viewed items (session-based).
3.Personalization
โ€ขCombine content-based filtering and collaborative filtering.
โ€ขSupport cold-start users (with little or no data).
4.API Performance
โ€ขResponse time < 100ms per request.
โ€ขPagination and filtering support (e.g., by category, price range).
5.Monitoring & Analytics
โ€ขLog recommendation performance (CTR, conversions).
โ€ขTrack latency, hit/miss ratio, and model effectiveness.


โš™๏ธ Non-Functional Requirements


RequirementDescription


Scalability

Must scale horizontally to handle millions of concurrent requests.


Low Latency

API must return recommendations in under 100 ms.


High Availability

Design for 99.99% uptime.


Personalization Accuracy

Continuously improve model using real-time user data.


Extensibility

Easy to add new algorithms or product categories.


Observability

Logging, metrics, and health checks.


๐Ÿงฉ High-Level Architecture


Core Components


1.API Gateway
โ€ขReceives client requests and routes them to appropriate services.
โ€ขHandles authentication, rate limiting, and logging.
2.Recommendation Service
โ€ขCore business logic for generating personalized and trending product recommendations.
โ€ขUses machine learning models (e.g., collaborative filtering, embeddings, or hybrid models).
3.Feature Store
โ€ขStores precomputed user and product features (e.g., embeddings, popularity scores).
4.Model Training Pipeline
โ€ขPeriodically trains and updates ML models using user behavior data.
โ€ขCould use Spark, TensorFlow, or SageMaker.
5.Cache Layer
โ€ขStores frequently accessed recommendations to reduce response times (e.g., Redis).
6.Data Sources
โ€ขUser Behavior DB (views, clicks, purchases).
โ€ขProduct Catalog DB (metadata, pricing, categories).
โ€ขAnalytics DB (feedback loop for improving recommendations).


Architecture Diagram (Text Representation)

1 2 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” 3 โ”‚ Client (Web/Mobile) โ”‚ 4 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ 5 โ”‚ 6 โ–ผ 7 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” 8 โ”‚ API Gateway โ”‚ 9 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ 10 โ”‚ 11 โ–ผ 12 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” 13 โ”‚ Recommendation Serviceโ”‚ 14 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ 15 โ”‚ 16 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” 17 โ”‚ โ”‚ โ”‚ 18 โ–ผ โ–ผ โ–ผ 19โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” 20โ”‚ Cache Layer โ”‚ โ”‚ Feature Store โ”‚ โ”‚ Product DB โ”‚ 21โ”‚ (Redis) โ”‚ โ”‚ (User/Product)โ”‚ โ”‚ (Catalog) โ”‚ 22โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ 23 24 โ”‚ 25 โ–ผ 26 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” 27 โ”‚ Model Training Pipeline โ”‚ 28 โ”‚ (Batch / Stream Updates) โ”‚ 29 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜


๐Ÿงฎ Data Model Examples


UserBehavior Table


user_idproduct_idaction_typetimestamp

U123

P789

VIEW

2025-10-22 14:03

U123

P456

PURCHASE

2025-10-22 14:15


ProductCatalog Table

product_idcategorybrandpricerating

P123

Electronics

Sony

1999

4.5


Feature Store Example

user_idembedding_vectorrecent_categorieslast_active

U123

[0.12, 0.83, ...]

["Electronics", "Books"]

2025-10-22


โš™๏ธ Flow Example


1.User logs into app โ†’ sends GET /recommendations/U123.
2.API Gateway forwards request to Recommendation Service.
3.Service fetches cached results; if not present:
โ€ขRetrieves user embedding from Feature Store.
โ€ขQueries similar products using vector similarity.
โ€ขRanks results and stores in cache.
4.Response returned to client in under 100ms.


๐Ÿง  Key Challenges & Discussion Points


ChallengeDiscussion


Cold Start Problem

How to recommend to new users or new products?


Data Freshness

How frequently should recommendations be updated?


Real-time Personalization

Should recommendations change immediately after each user action?


Scalability

How to scale training and serving infrastructure?


Caching Strategy

Per-user caching or global trending cache?


Ranking Logic

How to balance relevance vs popularity?


๐Ÿงฐ Tech Stack Example


ComponentSuggested Technologies


API Layer

Node.js / FastAPI / Spring Boot


Cache

Redis / Aerospike


Feature Store

Cassandra / DynamoDB / Feast


Model Training

Spark MLlib / TensorFlow / PyTorch


Data Streaming

Kafka / Kinesis


Storage

PostgreSQL / S3 / Elasticsearch


Monitoring

Prometheus + Grafana


๐Ÿ“Š Scaling Strategies


โ€ขHorizontal Scaling: Deploy multiple API instances behind a load balancer.
โ€ขSharding: Partition user data by region or user ID hash.
โ€ขBatch + Real-time Updates: Use a Lambda architecture โ€” periodic batch training + real-time event ingestion.
โ€ขEdge Caching: Cache recommendations close to users (e.g., via CDN).


โœ… Deliverables


Deliverable and Description


Architecture Diagram

High-level system components and flow.


API Contracts

Input/output specs with examples.


Data Model

Key schema for user, product, and behavior data.


Scaling & Fault Tolerance Plan

Approach for high traffic and failures.


๐Ÿ”น Example Summary

Design an API-driven Product Recommendation System for an e-commerce platform that provides personalized, low-latency, and scalable product suggestions to users.
The design must cover API interfaces, data pipelines, machine learning integration, and caching strategies for optimal performance.


Solution

Comments