Ch.64: GitHub System Design: Fast Clones, Stalled Pushes
Outline
- 0:00 Sharing code means publishing a revision
- 0:40 The complete repository service
- 1:12 What success must mean
- 2:21 Bytes and branch decisions have different budgets
- 3:25 Send the missing graph, not another whole copy
- 4:43 Keep incoming objects out of the published view
- 5:51 Two valid uploads can still disagree
- 6:51 A lost reply doesn't undo publication
- 8:15 Why another serving replica can slow a push
- 9:17 Independent branches needn't share one queue
- 10:27 Read workers don't get a vote on every push
- 11:37 Fresh reference, missing local objects
- 12:46 Protect the expensive work as well as the bytes
- 13:56 Cleanup must respect unpublished work
- 15:26 Follow the push, then follow the fetch
Transcript
0:00 Git lets developers save versions of a project and share changes. GitHub hosts those repositories so teammates and build jobs can retrieve them. A successful push should make your new revision available, not just finish a progress bar. We'll design that repository-hosting core: receiving changes, deciding which branch update wins, and serving the right revision afterward. Not the issue tracker or the build system. And we'll tackle an awkward scaling problem. Giving more people fast copies of the code can make publishing their changes slower.
0:30 Because those extra serving machines can accidentally become extra participants in every write. Let's separate what needs agreement from what just needs the bytes. With agreement separated from byte transfer, our service has two routes through this map. A push enters through authenticated repository routing, passes intake checks, stores its objects durably and asks to change a branch. Fetch workers take the other route. They select the published revision and assemble its data for the caller. Background maintenance keeps the stored layout efficient without deciding which revision is current. Keep those routes in mind when this map returns.
1:06 Before choosing how they're implemented, we need the promises a developer can actually rely on. With those two routes in view, the functional requirements start with familiar operations. Clone gets the initial repository data; fetch brings in more history. Push offers new revisions and asks the host to update a named branch. A branch is a reference, a movable name pointing to a revision. Think of a shelf label that can move between sealed boxes. The revision is a commit, a saved snapshot linked to its history.
1:38 Moving the label doesn't rewrite that snapshot. So receiving a commit doesn't yet mean teammates will find it through that branch. The service must check access and branch policy before publishing the change. Right. And concurrent writers mustn't silently replace each other's updates. After we acknowledge a push, its data must remain retrievable and a later fetch must see that branch revision or a newer one. For example, a teammate starts fetching just before a new push completes. Does that request need to restart to get the newer revision?
2:10 It can finish the snapshot it selected. Our freshness promise concerns a fetch that begins after acknowledgement. We aren't changing the meaning of a transfer that's already underway. Now put load behind those developer promises. Our non-functional requirements include durable accepted revisions, fresh branch selection, bounded request work and customer isolation. Growing read capacity shouldn't make each push wait for another serving machine. For a capacity exercise, take one hot repository with 200 pushes a second, spread across 20 branches.
2:40 Each push brings an average of 256 kibibytes of new packed data. That's 50 mebibytes a second of intake. Suppose 500 fetches a second return 2 mebibytes each. Their outgoing traffic is 1,000 mebibytes a second. The larger byte stream is on the read side, but the moving branch names are a separate coordination problem. Buying network capacity doesn't automatically speed up that decision. Exactly. Those are invented workload numbers, not GitHub measurements. And the average fetch size isn't the size of a full repository; clients can already have most of its history.
3:17 Then we should follow one of those partial fetches. Otherwise we'd size the service as if every caller were downloading everything from scratch. That partial-fetch question leads to Git's graph of reusable objects. File contents are blobs; trees connect names to contents; commits identify a tree and earlier commits. The object ID fixes the content it names; changing that content creates a different object. Change the project's readme file, for example. The next commit can reuse the unchanged files' objects, while its branch pointer selects the history for this request.
3:50 That reuse is what Git's transfer negotiation exploits. Client and server work out what the client already has. The server bundles the missing objects into a pack, the transfer unit, rather than sending every old file again. But a request for a familiar object ID still belongs to a particular repository. A private object's name isn't a password that unlocks it. Definitely not. Our router verifies repository access, and the fetch service applies that boundary while resolving objects. Each customer also gets a work limit for tracing object links and building packs.
4:24 I think that work limit matters as much as raw download bandwidth. A small request can still ask the server to do a lot of searching before it sends anything. And a push has its own expensive preparation. We should keep that preparation separate from the moment its branch becomes visible. Before new push bytes can become visible, keep them in isolated intake. Receive a bounded pack, verify its contents and check that the proposed revision has a complete object graph. Does the upload become part of the repository as soon as the last byte arrives?
4:59 My first instinct would be to store it and let the branch checks catch up. Store it provisionally, yes; publish it, no. Git's push receiver quarantines incoming objects until its pre-receive hook succeeds. That's the check run before branch changes, while the objects are still in a temporary area. Ah, the visibility boundary is earlier than I was drawing it. Another reader can't necessarily retrieve an object just because this intake worker can inspect it. Exactly. For instance, if an intake worker points a branch at a quarantined commit, other readers fail to find it.
5:33 That's why the hook can't publish that pointer. Our distributed service keeps that ordering principle, not a shared temporary folder on every machine. Validate the proposed graph, enforce branch policy, then make the required objects durable before publication. The direct docs are linked in the description. But durable objects don't settle concurrent branch changes. Picture this: two developers both fetched revision R. One now offers revision S; the other offers T. Both packs could pass validation.
6:01 They're two different pieces of work built on the same starting point, not necessarily corrupt data. The first request says: change this branch from R to S. The metadata transaction checks that R is still current, then commits S. And the other request still expects R. If we apply it unconditionally, we replace the first developer's published tip with T. So reject that stale expectation. The pack protocol carries the old and new object IDs. Git's update-ref command checks that the old value still matches before replacing it. That makes sense. The unhappy developer gets a conflict to resolve, rather than the team getting a quiet loss of somebody's visible work.
6:42 They can fetch the new history and decide how to combine it. The server doesn't invent a merge, and uploading first doesn't grant a reservation on the branch. That conditional update needs a recoverable publication path. First persist the validated graph in durable storage. Then a metadata transaction tests the expected branch value and commits the new tip. Give that transaction one current, fenced owner. Fencing means an old owner can't keep publishing after leadership has moved to a replacement. The transaction also checks which branch rules it evaluated.
7:15 Suppose an administrator blocks direct pushes while this upload is running. Finishing the upload doesn't preserve the old permission; publication must satisfy the current rule. Okay, suppose the worker crashes after storing the objects but before changing the branch. Have we promised a revision that nobody can find? No acknowledgement yet. The old branch remains current. The intake record protects those provisional objects until the attempt is resolved; unused data can be reclaimed safely later.
7:44 Now move the crash just past the metadata commit, before the response reaches the developer. The branch has changed even though the client didn't hear success. Right. The client has an uncertain result, not proof of failure. Fresh branch state and the stored attempt result resolve it; blindly repeating the old expected value can legitimately be rejected. And a push changing several references needs its own transaction contract. Git can negotiate atomic updates, but our one-branch example doesn't magically provide them across independent owners.
8:14 Now consider putting every branch update behind one repository-wide lock. Suppose its serialized publication step takes 20 milliseconds. Even before other overhead, that admits only 50 updates a second. Against our assumed 200. A repository can have spare bandwidth and still collect a growing queue of tiny branch updates. Yeah. And GitHub's public engineering account describes another coupling: the machines serving repository reads also participate in durable writes. Adding read replicas can add write coordination.
8:45 Wait, a quorum means enough replicas agreeing. Why would this path need every healthy one? It doesn't inherently need every response to commit. GitHub reports waiting for every healthy replica, up to a timeout, before moving on in this particular path. The repository stays serialized through that wait. That's a builder's reported design, not a property of every quorum. So the repair has two parts worth separating: reduce which changes must wait for each other, and stop making extra read capacity add participants to the durable write path.
9:17 That repository queue opens a narrower coordination question. The branch race needed ordering on one reference. Many updates to different references can proceed independently. Our 20 branches can have publication in flight at the same time, instead of each waiting for a different branch's round trip. GitHub reports moving serialization toward individual references for that reason. But one busy default branch doesn't become independent branches just because we narrow the lock. Its writers still compete over the same name.
9:44 And shared storage or validation can become the next limit. I'd keep the narrower lock because it removes unnecessary waiting, not advertise 20 times the throughput. For a small internal host, an engineer might reasonably ask for one repository owner and ordinary local storage. Fewer moving pieces, simpler recovery. When is the split worth that cost? When measured load and the required failure behavior outgrow the simpler owner. Our hot-repository workload motivates it; a quiet team's repository may not need it at all.
10:14 I think the simplicity argument wins until the workload proves otherwise. A more elaborate diagram isn't evidence that the service needs more components. Our fetch traffic provides the other reason to separate them. With independent branch updates possible, separate read scaling too. Fetch workers get disposable caches backed by durable storage. They serve published objects, but adding a worker doesn't enlarge the group that must commit a push. So when the release triggers a wave of fetches, add serving capacity without asking every new machine to become another durable participant... ...or wait for its cache to fill before anybody can publish.
10:52 The worker can retrieve the objects it needs from the authoritative storage layer. GitHub's October bridge uses a virtual write replica: a process that streams writes to object storage and acknowledges only after they're durable. It serves no reads. Separate read replicas cache the data and stay outside the write quorum. That's an incremental bridge, not its entire final architecture. Yes, and copying updates to workers in the background doesn't suit every read workload. A worker having some repository data isn't proof that it knows the latest branch value.
11:26 That's the piece I was glossing over. We've made serving machines easier to replace, but we still owe the developer a fresh view after an accepted update. That freshness promise creates a specific fetch path. Our request selects the current branch value from metadata and pins the chosen revision's objects, protecting them from cleanup until the transfer finishes. Selection and protection must be coordinated so cleanup can't remove them between those steps. For example, the branch now points to S, but this worker's cache stops at R.
11:58 How does it serve S without becoming another synchronous replica? It retrieves S's missing published objects from durable storage before building a complete response. The earlier push couldn't acknowledge until all of S's reachable data was durably available. Oh, right. The worker can be behind locally without the response being behind. Precisely. It mustn't advertise S and then hand over an incomplete graph. It also mustn't quietly substitute R just because that happens to be cached. If the durable source can't supply an object, fail the request clearly.
12:31 Calling an older branch fresh would conceal the problem from the developer and the operator. Meanwhile, another fetch already pinned to R can finish R. This per-request snapshot is our design choice; we're not filling in an undocumented GitHub implementation detail. Once workers fetch missing objects on demand, isolate their expensive work. For instance, a tenant's compact upload can expand dramatically, while a small graph request can tie up a worker before returning any data. Bound expansion and validation work, limit each customer's concurrent requests and keep authorization tied to the repository.
13:06 Naming objects by their content doesn't erase that security boundary. Nor does passing access checks at upload start settle a later publication. Long-running intake must recheck revocation-sensitive policy before its branch transaction commits. Suppose the release's fetch traffic lands on a newly added worker. Its cache misses rise, but durable storage stays fast. That looks like a cold cache, not evidence that branch publication needs another coordinator. And when storage itself slows down, adding empty workers can send it more work.
13:36 I'd check the source latency before scaling readers. Meanwhile, a growing publication queue across independent branches sends me toward shared validation or storage, not the read cache. And keeping every rejected upload forever isn't a fix for safety either. Unused intake needs a cleanup path that understands what live work still depends on. Now give cleanup two distinct jobs. Repacking arranges the same objects into a more efficient stored pack. Reclamation decides which objects remain needed. Reshelving a book is different from discarding its last copy.
14:09 We've protected live reads and unfinished uploads. How does the collector see that protection as well as the current branches? For example, an older fetch is still sending R after the branch moves on, while an unfinished push has stored S without publishing it. Both requests register the objects and stored packs they need. Git's collection docs likewise warn about concurrent work and retained history, though this distributed protection scheme is ours. Oh, man. Deleting everything outside today's branch tips could break both requests.
14:41 Their protection has to count even before S is published and after R stops being current. Our collector starts from saved branch tips, retained history and protected requests. It traces those links, marking each object as still needed. A branch update must register newly needed objects with the collector before publishing during that scan. So the scan can't finish with yesterday's picture while a new branch tip slips past it. And even if we repack the objects, the fetch sending R might still be reading its old pack.
15:10 Exactly. Before dropping that pack, recheck that needed objects have another usable copy and no protected request needs the old layout. If either check is uncertain, keep it. Separate maintenance workers can do the work; they don't get to guess. Finally, follow the developer's push again on the same complete map. Authenticated intake validates the graph, durable storage preserves its objects, and the small branch transaction publishes the permitted new tip. Then follow the teammate's fetch. It selects authoritative reference state and a read worker supplies the chosen graph, fetching missing published objects without joining the write decision.
15:47 That's why fast clones needn't make pushes wait for every serving machine. We separated byte-heavy work from the updates that actually conflict, while keeping both sides tied to a durable revision. One busy branch still needs ordering, and the simpler repository owner remains a sensible option below this workload. The separation earns its complexity only when those requirements call for it. Thanks for listening to Learning Podcasts.