On this page
System Design — Online Judge (LeetCode)
Last reviewed 14 Oct 2026
Part of the system design series. See the framework and building blocks first if you haven’t. Related: Distributed Message Queue, Gaming Leaderboard.
1. Requirements
Functional
- Browse problems; write code in an in-browser editor in ~20 languages.
- Run against sample tests (fast feedback) and Submit against the hidden test suite.
- Verdicts: Accepted, Wrong Answer, Time Limit Exceeded, Memory Limit Exceeded, Runtime Error, Compilation Error — with runtime/memory stats.
- Timed contests with a live leaderboard.
Non-functional
- Security first: every submission is untrusted, possibly hostile code. It must not read other users’ code or test data, reach the network, or affect the host.
- Fairness: the same code should get roughly the same runtime every time.
- Normal load ~hundreds of submissions/s; a contest start brings 10–50× that within minutes.
- Verdict in a few seconds at normal load; contests may queue but must never lose a submission.
2. High-level architecture
flowchart LR U[Browser editor] --> API[Submission API] API --> DB[(Submissions DB)] API --> Q[(Queue per priority:<br/>contest, submit, run)] Q --> RN[Runner fleet<br/>autoscaled on queue depth] RN --> SB[Sandbox per submission<br/>container / microVM] TS[(Test cases<br/>object storage, cached on runners)] --> RN RN -->|verdict, stats| DB RN --> PUB[Result pub/sub] PUB -->|WebSocket or poll| U RN --> LB[Contest scoring service] LB --> RZ[(Redis sorted set<br/>leaderboard)]
- The API stores the submission (
PENDING) and enqueues its id. It returns immediately; the client waits on a WebSocket or polls. - A runner takes a job, fetches code + test cases, compiles once, runs every test in a sandbox, and stops at the first failure for a Submit.
- The verdict is written to the DB and published to the user.
3. Key design decision — sandboxing untrusted code
Layers, from cheapest to strongest — production systems combine several:
| Layer | What it stops |
|---|---|
| Separate low-privilege user, no shell, read-only filesystem except a tmp dir | Reading other files, persisting anything |
| cgroups limits: CPU time, memory, PIDs (process count) | Fork bombs, memory exhaustion, infinite loops |
| Namespaces (PID, mount, network with no interfaces) | Seeing other processes, network access |
| seccomp syscall allow-list | Dangerous syscalls (ptrace, mount, raw sockets) |
| gVisor (user-space kernel) or Firecracker microVMs | Kernel exploits escaping a container |
Limits are enforced per test: wall-clock timeout (kill the process group), CPU time, memory (cgroup OOM → MLE), output size (stop runaway printing), and process count.
4. Deep dive — fairness and the verdict
- Measure CPU time, not wall time, for the TLE decision, and pin each sandbox to dedicated cores so noisy neighbours don’t steal cycles. Run one submission per core.
- Language multipliers: Python legitimately runs slower than C++; judges either use per-language time limits or a multiplier.
- Comparing output: exact match after trimming trailing whitespace, or a checker program for problems with multiple valid answers or floating-point tolerance.
- Warm images: keep per-language images pre-pulled and a pool of pre-started sandboxes so start-up doesn’t dominate a 50 ms run.
- Test data is versioned and cached on runners (it can be gigabytes for some problems); it’s never visible to user code beyond stdin.
5. Contest spikes
- Separate queues by priority: contest submissions > regular submits > “Run” (samples only). A flood of Runs must not delay contest verdicts.
- Autoscale runners on queue depth, and pre-scale before a scheduled contest start — VMs take minutes to start, and the spike takes seconds.
- Backpressure: per-user rate limits (one in-flight submission per user per problem), and show queue position rather than timing out.
- Leaderboard: score = solved count, then penalty time. Keep it in a Redis sorted set with a composite score (
solved × BIG − penalty), updated on each accepted verdict. Freeze the public board in the last hour if the contest format calls for it. See Gaming Leaderboard. - Plagiarism: after the contest, a batch job compares submissions (token or AST similarity, e.g. MOSS-style fingerprinting) — not on the hot path.
6. What real systems do today
- Judge0 is a widely used open-source code execution system; it runs code in isolate, the sandbox originally built for the International Olympiad in Informatics, which uses Linux namespaces and cgroups.
- gVisor (from Google) and Firecracker (from AWS) are the common strong-isolation runtimes for running untrusted code in multi-tenant services.
- Competitive programming sites (Codeforces, LeetCode, AtCoder) all use queued, asynchronous judging and visibly queue during contest peaks.
7. Scaling & failure
- Runner dies mid-judge → the queue message isn’t acknowledged; it’s redelivered and judged again. Judging is deterministic and idempotent (results keyed by submission id), so re-running is safe.
- Sandbox escape attempt → defense in depth; runners are disposable, replaced regularly, and network-isolated so a compromise doesn’t reach anything valuable.
- Queue backlog → users see “In queue (position 1,240)”; nothing is lost because submissions are persisted before enqueuing.
- Submissions DB slow → results can be buffered and written in batches; the result pub/sub still notifies users.
Interview follow-ups
- “Why not just run the code in a Docker container?” — Shared kernel; a kernel exploit escapes. Use gVisor/microVMs plus seccomp, cgroups, no network.
- “How do you stop
while(true){}or a fork bomb?” — CPU/wall timeouts kill the process group; cgroup PID limit. - “Same code, different runtimes on two submits?” — CPU-time measurement, pinned cores, one job per core, warm sandboxes.
- “Contest starts and submissions spike 30×.” — Priority queues, pre-scaled runners, per-user limits, async verdicts with queue position.
- “Problems with multiple correct answers?” — Custom checker programs per problem.
Further reading: Judge0 documentation and the isolate sandbox (github.com/ioi/isolate) · gVisor documentation · Firecracker design documentation · Mareš & Blackham, “A New Contest Sandbox” (Olympiads in Informatics, 2012).