Product View Tracking :
The system should track the products that users view and store this data efficiently for further analysis.
Frequently Viewed Together Identification:
The system should analyze user behavior to identify products frequently viewed together, providing insights that can be used to recommend these products to users.
Display Recommendations:
The system should display products frequently viewed together on the product page to facilitate cross-selling.
Efficient Querying:
The system should allow for querying frequently viewed products based on different time windows (e.g., last 24 hours, last week, last month).
The system must be scalable to handle spikes in traffic, especially during peak shopping seasons like Black Friday or holiday sales with recommendation engine operating with low latency.
This API retrieves products frequently viewed together with a specific product.
Endpoint:
GET /api/v1/products/{productId}/frequently-viewed
Request Parameters:
productId (string): The ID of the product which user is viewing as of now related to which frequently viewed products should be shown.timeWindow (string): The time window to filter frequently viewed products (e.g., 24h, 7d, 30d).Request Example:
GET /api/v1/products/98765/frequently-viewed?timeWindow=7d
{
"productId": "98765",
"frequentlyViewed": [
{
"productId": "67890",
"viewCount": 345,
"lastViewed": "2024-09-14T15:30:00Z"
},
{
"productId": "54321",
"viewCount": 298,
"lastViewed": "2024-09-13T12:15:00Z"
}
]
}
(A, B) is treated the same as (B, A) to avoid duplication. The application logic should enforce an ordering rule (e.g., always store the pair where productAId < productBId).ProductView table could be partitioned by userId to distribute user data across multiple nodes. The FrequentlyViewedPair table could be partitioned by product ID to distribute the load of frequently viewed queries.ProductView table can be archived or deleted after a set time (e.g., 30 days) to manage storage and keep the system performant.This flow explains how the system fetches frequently viewed products for a particular product.
GET request to the API Gateway.The Recommendation Service is responsible for retrieving frequently viewed products based on user behavior. It analyzes product view data and serves recommendations with low latency.
function getFrequentlyViewedProducts(productId, timeWindow):
cacheKey = "frequently_viewed_" + productId + "_" + timeWindow
result = cache.get(cacheKey)
if result is not None:
return result
frequentlyViewedPairs = analyticsDatabase.queryFrequentlyViewedPairs(productId, timeWindow)
cache.set(cacheKey, frequentlyViewedPairs, TTL=5min)
return frequentlyViewedPairs
productId. This allows the Recommendation Service to retrieve cached data from multiple cache nodes, improving performance under high load.