Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-06-12
Problem Statement
Design a system that fetches, caches, and provides product prices from Amazon product pages or APIs.
The system should be designed to efficiently fetch prices for given product IDs or URLs, support caching to reduce redundant calls, handle rate limits, and ensure data freshness.
🧠 Functional Requirements
Fetch Product Price
•Given an Amazon product URL or ASIN (Amazon Standard Identification Number), fetch its current price.
•Support both manual fetch (on-demand) and scheduled background refresh.
Cache Prices
•Cache fetched prices in-memory or persistently (based on design).
•Return cached price if it is fresh (within TTL), otherwise refresh the price.
Handle Rate Limits
•Amazon APIs or pages have rate limits.
•Implement request throttling or backoff strategy.
Data Freshness / TTL
•Prices must be refreshed after a configurable time-to-live (TTL) period.
Error Handling
•If fetching fails (e.g., due to API error or network issue), return the last known price if available.
Concurrency
•Multiple requests for the same product may come simultaneously — handle them efficiently without redundant network calls.
Extensibility
•The system should support adding other e-commerce sites (Flipkart, Walmart) in the future.
🚫 Non-Functional Requirements
•Fast reads for cached prices.
•Thread-safe cache and fetch logic.
•Pluggable architecture (easy to support multiple sources).
•Retry and exponential backoff for network failures.
•Configurable TTL and rate limits.
🧱 Suggested Components / Classes
ClassResponsibility
PriceFetcher (Interface)
Defines method to fetch price from a product source (e.g., Amazon).
AmazonPriceFetcher
Implements PriceFetcher — performs actual HTTP/API call, parses the price.
PriceCache
In-memory or persistent cache that stores (ProductID → PriceRecord) mappings with expiry time.
PriceRecord
Model class that contains product ID, price, currency, last updated time, TTL.
RateLimiter
Controls number of outgoing fetches per second/minute.
FetcherService
Main orchestrator that uses cache + fetcher + rate limiter to serve requests.
Scheduler
Periodically triggers background price refresh for popular products.
Logger / Metrics
Tracks fetch failures, cache hits/misses, and refresh latency.
🔄 Example Flow
User Request: getPrice("B0D4X8RXYM")
1.FetcherService checks cache:
•Cache hit & valid → return cached price.
•Cache miss or expired → proceed to step 2.
2.Check RateLimiter → if allowed, continue.
3.AmazonPriceFetcher fetches price from Amazon (mock API or HTML parsing).
4.Update PriceCache with new price and expiry time.