Design an Amazon Price Fetcher Service

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.
5.Return latest price to user.


🧪 Example Input / Output


Input:

1 2getPrice("B07PGL2ZSL")


Output:

1 2{ 3 "productId": "B07PGL2ZSL", 4 "price": 15999, 5 "currency": "INR", 6 "lastUpdated": "2025-10-24T12:15:00Z" 7}


⚙️ Constraints


•Number of products tracked ≤ 100,000
•TTL (price validity): 5–60 minutes (configurable)
•Max API calls: e.g., 10 requests/sec (configurable)
•Each fetch should complete < 2 seconds (including retry)


🎯 Goals


•Efficient price retrieval and caching.
•Safe concurrent access.
•Modular, extensible design for future sites (Amazon → Flipkart → Walmart).
•Clear separation of fetching, caching, and rate limiting concerns.


🚀 Optional Extensions


•Add persistent storage (Redis, DB) for cache.
•Add web UI or API endpoint to view latest prices.
•Support price change notifications (observer pattern).
•Maintain price history for analytics.


Solution

Comments