Multithreaded Web Crawler (LLD)
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-05-04
Problem Statement
Design and implement a thread-safe web crawler that fetches and processes web pages concurrently. The crawler should respect domain constraints, avoid duplicate visits, and allow scalable crawling using multiple threads.
Functional Requirements
1.<span></span>Core APIs
2.<span></span>submit(seed_urls) → Start crawling from a list of seed URLs.
3.<span></span>get_next_url() → Retrieve the next URL to crawl (from frontier).
4.<span></span>process_page(url) → Download the page content and extract links.
5.<span></span>visited(url) → Check if a URL has already been crawled.
6.<span></span>Concurrency Requirements
7.<span></span>Multiple worker threads should be able to fetch and process URLs in parallel.
8.<span></span>The same URL must not be crawled more than once (deduplication).
9.<span></span>Ensure thread-safe access to:
10.<span></span>URL frontier (queue of pending URLs).
11.<span></span>Visited set (to track already crawled URLs).
12.<span></span>Constraints / Features
13.<span></span>Limit crawling to a maximum depth from the seed URLs.
14.<span></span>Limit crawling to specific domains (e.g., avoid going outside example.com).
15.<span></span>Support configurable number of worker threads.
16.<span></span>Gracefully stop when there are no more URLs to crawl.
17.<span></span>Performance Goals
18.<span></span>Efficient synchronization to minimize contention (avoid global locks).
19.<span></span>High throughput: maximize pages crawled per second.
20.<span></span>Handle at least 100+ worker threads concurrently.
Example Workflow
1.<span></span>User provides seed URL: https://example.com.
2.<span></span>Thread-1 dequeues and fetches https://example.com.
3.<span></span>Extracted links: https://example.com/about, https://example.com/contact.
4.<span></span>Thread-2 processes /about while Thread-3 processes /contact.
5.<span></span>If a page has already been visited, it is skipped.
6.<span></span>Crawling continues until the depth limit or frontier is empty.
Deliverables
1.<span></span>Class design (e.g., WebCrawler, CrawlerWorker, UrlFrontier, VisitedSet).
2.<span></span>Thread-safe implementation of URL queue and visited set.
3.<span></span>A configurable crawler that runs with N worker threads.
4.<span></span>Tests verifying:
5.<span></span>No duplicate URL fetches.
6.<span></span>Correct depth/domain restrictions.
7.<span></span>Proper termination when no URLs remain.
8.<span></span>Benchmark showing scalability with increasing worker threads.