Design of the LeetCode System
Explore the design, components, and workflows of the LeetCode system to understand how it is built, enabling efficient, secure, and scalable coding problems and contests.
We'll cover the following...
In this lesson, we'll explore the high-level and detailed design of a service similar to LeetCode. We’ll learn how different components, such as code execution, plagiarism checks, and leaderboards, work together to deliver a scalable, reliable, and secure platform for coding problems and contests.
Let’s start with the high-level design.
High-level design of LeetCode
The following diagram illustrates the high-level architecture of the system:
The core components of this system are detailed below:
Component | Role in the System |
Load balancer | Distributes incoming requests evenly across multiple application servers |
Application server | Receives client requests and routes them to appropriate backend services |
Pub-sub service | Manages asynchronous communication between services |
Code execution service | Runs user-submitted code in secure, isolated environments and captures the results |
Plagiarism checker | Verifies the originality of code submissions using similarity detection algorithms |
Leaderboard and metadata service | Manages leaderboard data and contest-related metadata |
Blob storage | Stores large unstructured files, primarily user-submitted source code |
Database | Stores all structured data, including user profiles, problem statements, contest information, and references to files in blob storage |
Let’s now look at how these components work together to process a user’s code submission, both for contests and standalone practice problems. While the overall execution pipeline remains the same, certain steps, such as leaderboard updates or plagiarism checks, apply only to contest submissions.
Workflow
The following steps outline the flow of a single contest submission:
When a user submits a solution to a coding problem during a contest, the load balancer sends this request to an application server.
The application server forwards the submission to the pub-sub service, where the request is queued for processing.
The code execution service consumes the queued event, runs the submitted code in a secure environment. It stores the execution results (status, runtime, memory) in the database and the raw output logs in blob storage. Once execution is complete, a completion event is published to the results-topic in the pub-sub service.
Along with the code execution services, the plagiarism checker also receives the code in parallel and evaluates its originality. Results are returned to the pub-sub system for further processing.
The leaderboard and metadata service collect the results from both the code execution and plagiarism checker and update the leaderboard in real time. It stores this data for later retrieval and analysis by users.
This workflow ensures that the system can handle large volumes of simultaneous submissions while maintaining performance, fairness, and data integrity.
Now, let’s explore the key APIs that facilitate communication with the various LeetCode services.
API design
The platform’s functionality is exposed through APIs managed by various microservices. A dedicated API gateway handles request routing, authentication, and rate limiting, forwarding requests to the appropriate backend service.
List questions APIs
List problems: This API lists the problems based on different difficulty levels and topics. The
listProblems()API sends input parameters to the backend system, which processes them and returns a paginated list of available problems based ...