Why git-cache-proxy exists, how it compares to the alternatives, how it works internally, and a throughput benchmark. For usage see the README.
- A generic HTTP cache doesn't work:
git fetchis a negotiated pack transfer (POST /git-upload-packresponses are computed per request), so there's nothing static to cache. - A scheduled full mirror (Gitea pull-mirror, etc.) means enumerating and replicating every repo, and it can lag the exact commit CI just pushed.
This proxy is lazy and pull-only: it caches on first request, and on every
request checks freshness before serving, using a TTL-coalesced incremental
git fetch. Optional background refresh updates recently used existing mirrors;
it never discovers or clones repositories. Nothing is pushed to the origin.
That pull-only property is what makes it viable against a sensitive origin.
| Approach | Caches fetch [1] |
Lazy [2] | Fresh ref [3] | Delta-only WAN [4] | Pull-only [5] | Any origin [6] |
|---|---|---|---|---|---|---|
| git-cache-proxy | yes | yes | yes | yes | yes | yes |
| Generic HTTP cache (nginx / Varnish) | no | n/a | no | no | yes | yes |
| Scheduled mirror (Gitea / GitLab pull-mirror) | yes | no | lag | no | no | yes |
git clone --reference / alternates |
n/a | no | seed | yes | yes | yes |
| GitLab Geo / server geo-replication | yes | no | yes | no | no | no |
- Caches the negotiated
git-upload-packresponse, not just static objects - a plain HTTP cache can't, because every fetch is computed per request. - Caches on first request; no repo enumeration or replication schedule to run.
- Serves the exact ref asked for. The proxy fetches upstream before serving; a scheduled
mirror can lag its refresh interval (
lag);--referenceis only as fresh as its local seed (seed). - Only new objects cross the WAN per request; the bulk history is served from the local mirror.
- Never writes to the origin. With background refresh disabled (the default), upstream pulls serve client requests only. Optional refresh pulls recently used existing mirrors without discovering or cloning repositories.
- Works against any unmodified Git smart-HTTP origin. GitLab Geo needs GitLab on both ends.
All git work is delegated to the system git binary, so protocol v2,
shallow, and partial (filtered) clones are git's responsibility rather than a
reimplemented wire format.
GET <repo>/info/refs?service=git-upload-pack
1. resolve <repo> -> upstream URL + local bare mirror dir
2. clone --mirror (first time) or fetch --prune (coalesced per repo)
3. serve `git upload-pack --advertise-refs` from the mirror
POST <repo>/git-upload-pack
-> stream `git upload-pack --stateless-rpc` from the mirror (packfile)
anything git-receive-pack -> 403 (read-only)
Concurrent clients for the same repo are serialized so a burst triggers a single upstream fetch; a short TTL coalesces repeated requests.
Upstream clone/fetch retries recognized transient network failures up to three
times after the first attempt, waiting 1, 2, then 4 seconds. This includes TLS
EOF, connection resets/timeouts, temporary DNS failures, and HTTP 408, 429,
500, 502, 503, and 504. Authentication, missing repositories, certificate
validation, local filesystem errors, and unknown failures are not retried.
Classification uses Git's English stderr diagnostics; the child locale is fixed
to C. Retry logs contain the operation, attempt, delay, and error category.
An operation that still fails is logged once, as upstream operation failed,
with its operation, trigger (request or background refresh), repo,
error_kind, elapsed time, and a summary of git's stderr. The summary is the
last 8 lines, capped at 1 KiB, because remote: lines are upstream-controlled
text. Git already strips credentials from URLs in its own diagnostics; the proxy
repeats that, redacts Authorization and Cookie values and the credential in
use, and removes inherited GIT_TRACE* and GIT_CURL_VERBOSE so trace output
never reaches stderr. The stderr stays
out of the returned error, which callers may surface. A cancelled operation (the
client went away, or shutdown) is logged at debug, not as a failure.
Retries stay inside the existing per-repository lock. Each failed clone's staging
directory is removed before another attempt; an existing fetch mirror is kept.
Only success updates the freshness timestamp. After retries are exhausted the
request still fails with 502; stale refs are not served as a fallback. The retry
budget limits attempts and adds at most seven seconds of backoff. Each upstream
Git operation also has one --upstream-timeout-seconds budget (default 3600)
across all attempts and backoff, including background refresh. LFS and local
upload-pack operations are outside this retry policy and operation budget.
If a client wants a commit by SHA that the mirror never captured - typically a
GitHub pull-request merge commit, which lives only under the unadvertised
refs/pull/<n>/merge and so is what actions/checkout fetches on a PR build - the
proxy fetches that SHA from upstream on demand and serves it, instead of failing
with not our ref. This relies on the upstream serving arbitrary SHAs (GitHub's
uploadpack.allowAnySHA1InWant, which is on); an upstream that refuses simply
leaves the request to fail as it would without the proxy. Ordinary branch/tag
clones are unaffected - they pay only a cheap local object check, never an extra
upstream call. Each fetched SHA is pinned under a reserved ref so the mirror can
keep serving it; --max-wants bounds how many such pins a mirror retains, pruning
the oldest so they cannot accumulate without bound (like the mirror-level LRU, but
scoped to a single mirror's pins). If a single request needs more new SHAs than
--max-wants, only that many are fetched and the excess wants are left to fail with
not our ref (the proxy logs a capping want-by-sha fetch warning); such a request
would immediately have its pins pruned anyway, so raise --max-wants if you expect
that many unadvertised objects at once.
--upstream-timeout-seconds (GITCACHEPROXY_UPSTREAM_TIMEOUT_SECONDS, default
3600) bounds each clone, full-ref fetch and SHA backfill across attempts and
retry delays. The value must be positive and fit in a monotonic deadline.
Library users can set the same budget with GitCache::with_upstream_timeout.
The budget starts when the operation starts, after the repository lock is acquired.
SIGTERM and Ctrl-C reject new upstream work and cancel active commands, retry
backoff and repository lock waits before HTTP drain. Upstream owners kill process
groups on Unix and wait for the direct child before releasing repository locks.
Dropped requests also cancel their upstream owner. Library hosts call
GitCache::shutdown before draining HTTP and wait_for_shutdown after draining.
Active local upload-pack responses continue to drain. This does not impose a hard
shutdown bound on active uploads or blocking filesystem work. On non-Unix targets,
cancellation terminates only the direct child.
When serving client clone/fetch requests, the proxy writes negotiation input to local git upload-pack while streaming its response to the client. Writing the request has a five-minute deadline. The operation also stops after 30 minutes without reading response data from Git, including during pack generation. Each read resets this inactivity deadline. Once the request is written, active transfers have no total duration limit.
These fixed limits are separate from upstream retries. Cancellation, pipe
errors and timeouts trigger subprocess cleanup. Failures after response
headers are sent terminate the response body. On Unix targets with
waitid(NOWAIT), leader exit is observed independently of stdout EOF and
remaining group members are stopped before the leader is reaped. Other
targets wait for request completion and stdout EOF before observing exit.
Optional background refresh keeps recently used existing mirrors warm without repository discovery or cold clones. See Periodic refresh for the flags and defaults.
Eligibility. Usage is kept only in memory and reset on restart. A request
counts as use after successful cache preparation (ensure_fresh returning
Ok), including TTL hits, not after the full HTTP response or pack stream.
Failed, cancelled, and auth-rejected preparation leaves prior successful use
unchanged, and concurrent completions cannot move the timestamp backwards.
Refresh itself never records use, so periodic work cannot keep a mirror active
forever. When disabled, requests neither record usage nor lock refresh state.
Each round selects mirrors used within the active window, most recent first.
Scheduling. Rounds start every interval, measured from the previous round's start. At the round deadline no new repository starts and running fetches finish; the next round waits until both that deadline and all previous fetches have completed. A deadline skip is not a fetch failure. Refresh shares the per-repository fetch lock and TTL with requests and eviction, so a request arriving during refresh waits for that fetch. Shutdown instead cancels upstream operations and reaps child processes; library users must await both request owners and the refresh handle.
Eviction. Background fetches do not promote the eviction LRU. A successful fetch marks the tracked entry for deferred size measurement and capacity enforcement by the existing evictor; a TTL hit does not. Entries already selected for eviction are not recreated, and an eviction cancels a refresh fetch running for that mirror instead of waiting for it.
Credentials and failures. Refresh uses only the static upstream auth header,
or anonymous access when none is configured, so it never stores or reuses
client credentials. An upstream auth rejection blocks further refresh attempts
for that mirror until a fetch using the same server credential succeeds, or the
process restarts; a client credential success clears ordinary failure backoff
but preserves this mark. The first ordinary failure is eligible in the next
scheduled round. Repeated failures wait interval * 2^(failures - 1) after
failure completion, capped at 32 intervals. Any successful upstream fetch
clears ordinary failure and backoff state. Request serving ignores refresh
backoff.
LFS objects use a different HTTP API from the git protocol, so they are cached separately:
POST <repo>/info/lfs/objects/batch
-> forward to upstream, then rewrite each object's download URL back to this
proxy so the object fetch is cached here
GET <repo>/info/lfs/objects/<oid>
-> serve from the on-disk cache, or on a miss fetch it from upstream once
(verify sha256 == oid, store), then serve
anything with operation=upload -> 403 (read-only)
Objects are content-addressed and immutable, so a cached object is never stale and
is shared across every repo that references the same oid; the first fetch across the
fleet pays the WAN cost, the rest are served locally. The upstream LFS transfer is
in-process over HTTPS (reqwest + rustls, no OpenSSL), so the binary stays statically
linked. Cached objects share the --cache-max-mb budget and LRU eviction with the
git mirrors. No configuration is needed: LFS is served on the same endpoints the git
client already routes through the proxy.
The cache is self-managing. Mirrors are created on first request, and with
--cache-max-mb set, least-recently-used idle ones are evicted in the background
once the total exceeds the cap - that cap is the intended way to reclaim disk. There
is no endpoint to delete a specific mirror, and none is needed: an evicted or missing
mirror is transparently re-cloned on its next request, so eviction is a cache
concern, never a correctness one.
Deleting a mirror directory by hand works but is not the intended lever. The proxy
assumes it owns the cache root, so a manual deletion reaches the cache-size metrics
only once the background task reconciles it (within a minute) or the next restart
rescans. Prefer --cache-max-mb.
For a fleet of ephemeral clients fetching the same content, only the first fetch pays the WAN cost; the rest are served locally. Over an emulated 20 Mbit/s, 60 ms-RTT link - cloning a 64 MB repo, and fetching a 32 MB git-LFS object:
| Scenario | Time | WAN bytes |
|---|---|---|
| Clone, direct (today) | 28.1 s | 64 MB |
| Clone, via proxy cold (runner 1) | 28.5 s | 64 MB |
| Clone, via proxy warm (2..N) | 0.6 s | ~0 MB |
| LFS object, cold (runner 1) | 12.8 s | 32 MB |
| LFS object, warm (2..N) | 0.2 s | ~0 MB |
The first runner sees no penalty and every subsequent runner is served in-region
while nothing crosses the WAN; the saving scales with fleet size and link cost.
Reproduce or retune (TOTAL_MB, LFS_MB, RATE_MBIT, RTT_MS) with
bench/run.sh - see bench/README.md for
the method and its caveats.