Design GitHub
Understanding the Problem
🐙 What is GitHub? GitHub hosts git repositories and builds collaboration on top of them. Developers push and pull code with the
gitcommand line, browse files and history on the web, propose changes as pull requests that teammates review and merge, and search code across every repository they can see. Under the familiar UI it's a storage system for hundreds of millions of repositories, each of which is a small content-addressed database that only git knows how to read efficiently.
GitLab, Bitbucket and internal code hosts are the same problem. The interviewer usually wants you to go deep on one or two of: how repositories are stored and replicated, how pull requests work, or how code search works. We'll cover all three.
Functional Requirements
Core Requirements
- Users should be able to create repositories and push, clone and fetch them with git over HTTPS and SSH.
- Users should be able to browse files, commit history and diffs in the browser.
- Users should be able to open pull requests, review them with line comments, and merge them.
- Users should be able to search code across all repositories they have access to.
Below the line (out of scope)
- Issues, wikis, packages and project boards.
- CI runners (Actions). See /system-design/ci-cd. We'll emit the events they consume.
- Git LFS for large binaries, beyond a note.
Non-Functional Requirements
Ask about scale. Let's design for 300 million repositories, 100 million users, and peaks of tens of thousands of git operations per second, most of them clones and fetches from CI systems.
Core Requirements
- A push that git reports as successful must never be lost. Durability is the top priority.
- The system should be highly available for reads (clone, fetch, browse): 99.95% or better.
- Web pages for files, commits and diffs should load in under 500 ms at p99 for typical repositories.
- Code search should answer in under 1 second, and a push should be searchable within minutes.
- The system should absorb clone storms on popular repositories without hurting everyone else on the same servers.
Below the line (out of scope)
- Strong consistency for web views across regions. A web page a second behind a push is fine.
Some math. Repository sizes follow a power law: most are tiny, and a few (big monorepos, game engines) are many gigabytes. If the average is around 30 MB, 300M repos is about 9 PB, and three replicas make it 27 PB. That's big but not exotic, and it fits on a few thousand storage servers with large local disks.
Traffic is lopsided. People push a few times a day, but CI systems clone and fetch on every build, so reads outnumber writes by something like 10 to 1 or more. A fetch isn't a simple file read either: the server has to work out which objects the client is missing and compress them into a packfile, which costs CPU and memory proportional to the repository's size. A single popular repository being cloned by thousands of CI jobs a minute can saturate the machine it lives on.
The key observation: git data isn't blobs you can throw into S3. Git operations need fast random access to a repository's objects and CPU next to the data to compute packs. That pushes us toward sharding whole repositories onto servers with local disks, and makes replication and hot repositories the hard parts.