ktsu.GitLfsCache
A caching reverse proxy for the Git LFS HTTP API. Point lfs.url at it and object transfers come from a local store instead of crossing the internet twice.
Introduction
Many short-lived clients pulling the same large objects is the situation this exists for: continuous integration pods cloning one repository over and over, where upstream bandwidth and clone latency dominate everything else.
ktsu.GitLfsCache sits between those clients and their forge. Every Batch API call is relayed upstream with the client's own credentials, so upstream remains the sole authority on who may read what. The transfer URLs in the response are rewritten to point back at the proxy, and object bytes are served from a local content-addressed store. A miss is fetched from upstream once and written to the store on the way through, so the next request for that object is local.
It is not an LFS server. It never becomes the authority for an object it did not obtain from an upstream, and it makes no access decisions of its own.
Features
- Batch relay, never short-circuited. Every Batch call reaches upstream, even when the proxy already holds every object, so a cached object can never let a client skip an access check.
- One upstream fetch per object. Concurrent misses for the same object are coalesced: the first request fetches, the rest wait and then read from the store.
- Verified before it is stored. Content is digested while it streams and published only if it hashes to the object id it claims, so a truncated or tampered transfer can never become a cache hit.
- Store-on-write for pushes. An upload is relayed upstream and cached at the same time, so a just-pushed object is not a guaranteed miss.
- Stateless replicas. Rewritten URLs carry their own encrypted, authenticated context, so any replica can serve any URL with no shared cache and no sticky sessions.
- Bounded disk use. A byte budget with least-recently-used eviction, sweeping to a low-water mark so it does not thrash at the boundary.
- Many upstreams from one deployment. Each is addressed by the first path segment.
- One lock listing for everyone.
GET .../locks is answered from a snapshot, so clients polling for lock state stop multiplying that traffic upstream. Upstream still authorizes every reader.
- Batched locking. A proxy extension takes many paths at once and fans them out in parallel under a rate limiter, instead of one sequential request per path.
- Transparent to what it does not model. Any other Git LFS endpoint is relayed verbatim.
- One binary, two shapes. The same build is the
gitlfscache dotnet tool and the container image, so they cannot drift.
Installation
As a dotnet tool
dotnet tool install --global ktsu.GitLfsCache.Tool
As a container
docker pull ghcr.io/ktsu-dev/gitlfscache:latest
Usage
Running locally
gitlfscache --port 8080 \
--store /var/cache/gitlfscache \
--max-size 100GB \
--upstream github=https://github.com \
--allow github='**'
Or point it at a configuration file and pass nothing else:
gitlfscache --config /etc/gitlfscache.json
Every flag except --allow is optional, and --allow is only required when the upstream it belongs to was declared on the command line rather than in configuration. Omit --store and it uses a per-user application data directory. Omit --token-key and one is generated for the run, with a warning: transfer URLs already handed out stop working when the process restarts, and a second instance cannot serve them.
--upstream is repeatable, and the name becomes the first path segment clients address:
gitlfscache --upstream github=https://github.com --allow github='studio/**' \
--upstream ado=https://dev.azure.com/myorg --allow ado='myproject/**'
--allow is required at least once per upstream, and is also repeatable. It names the repository paths that upstream may be used for, and the proxy refuses to start without it. Pass '**' to allow every repository.
Requiring it is deliberate. Without a list, anyone who can reach the proxy can make it cache objects from any repository they can read, spending the byte budget and evicting the working set that the cache was deployed for. It is not an access control and never grants anything: upstream still authorizes every call with the client's own credential.
Pointing a client at it
git config lfs.url https://lfs-cache.example.com/github/owner/repo.git/info/lfs
One setting is enough. The proxy is a complete replacement for the upstream LFS endpoint, not only a Batch interceptor, so anything it does not terminate itself is relayed.
One thing that catches people out: the git credential helper keys credentials by host. A client pointing lfs.url at the proxy must have its upstream token stored against the proxy host, not the forge host. Otherwise git has no credential to send, the proxy has none to relay, and upstream refuses the batch call.
Configuration
Nothing has to be passed as a flag. Configuration comes from three places, each overriding the one before it:
appsettings.json in the directory you run from. Note that this is the working directory, not where the tool is installed, so the file ships with the container but a tool user provides their own.
- A file named with
--config, which can live anywhere: gitlfscache --config /etc/gitlfscache.json. It is layered over the working-directory file rather than replacing it, so an explicit file only has to carry what differs. A path that does not exist is reported by name and the process exits rather than starting on defaults.
- Environment variables, using
__ as the section separator (GitLfsCache__Store__MaxSize, GitLfsCache__Upstreams__github__BaseUrl). This is how the Kubernetes base configures everything.
The flags are a convenience over the same settings and win over all three, so --max-size 3GB beats a --config file asking for 9GB. --allow replaces rather than adds to: when it names an upstream, that upstream allows exactly the patterns given on the command line, and any Repositories entries for it from the three sources above are discarded. Upstreams no --allow names keep their configured list.
{
"GitLfsCache": {
"PublicBaseUrl": "https://lfs-cache.example.com",
"TokenKeys": ["<base64, 32 bytes>"],
"TokenLifetime": "01:00:00",
"Store": {
"Root": "/var/lib/gitlfscache",
"MaxSize": "500GB",
"LowWaterMark": 0.9,
"StagingMaxAge": "06:00:00",
"MaintenanceInterval": "00:05:00"
},
"Fetch": { "FollowerTimeout": "00:05:00" },
"Locks": {
"Enabled": true,
"ListTtl": "00:00:15",
"AdmissionTtl": "00:01:00",
"MaxFanOutConcurrency": 8
},
"Upstreams": {
"github": { "BaseUrl": "https://github.com", "Repositories": ["studio/**"] },
"ado": { "BaseUrl": "https://dev.azure.com/myorg", "Repositories": ["myproject/**"] }
}
}
}
| Setting |
Meaning |
PublicBaseUrl |
The URL clients actually use. Optional: when unset, each transfer URL is derived from the incoming request, which requires the ingress to send X-Forwarded-Proto and X-Forwarded-Host. Setting it explicitly is safer, because only the operator knows for certain what clients addressed. |
TokenKeys |
Base64 encoded 32 byte keys protecting rewritten transfer URLs. A list, so a key can be rotated without breaking transfers in flight: put the new key first and keep the old one until the token lifetime has elapsed. |
TokenLifetime |
How long a rewritten transfer URL stays valid, at most. When upstream says its own URL expires sooner, the rewritten one expires 30 seconds before it, or halfway to it when less than a minute is left. A URL with under a second left is passed through to upstream unchanged. |
Store:MaxSize |
Byte budget, accepting decimal (500GB) and binary (500Gi) suffixes. |
Store:LowWaterMark |
The fraction of the budget a sweep reduces the store to. |
Store:StagingMaxAge |
How long an orphaned staging file from a crashed write survives. |
Store:MaintenanceInterval |
How often the store is swept for eviction and orphaned staging files (at most about 49.7 days, the longest a timer period can be). |
Fetch:FollowerTimeout |
How long a request waits for another request's fetch before fetching upstream itself (at most one day). |
Locks:Enabled |
Whether the proxy terminates any part of the locking API. Set false to relay every lock route, which is what it did before this existed and the fallback if the cache is ever suspected of being wrong. |
Locks:ListTtl |
How long a lock listing is served before it is refreshed. Short deliberately: see below. |
Locks:AdmissionTtl |
How long an upstream authorization is trusted before it is proven again. Must be at least ListTtl, and startup refuses otherwise. |
Locks:RefreshTimeout |
How long a request waits for another request's listing walk before walking itself (at most one day). |
Locks:MaxSnapshotLocks |
Above this many locks a repository is relayed rather than cached, so one enormous repository cannot consume memory without bound. |
Locks:MaxSnapshots |
The most lock listings held in memory at once, one per repository and ref a client has listed. Listings older than ListTtl are dropped first, then the oldest of the rest. |
Locks:MaxFanOutConcurrency |
How many lock calls may be in flight against one upstream at a time, across every request in the process. The right value per forge has to be found by measurement. |
Locks:MaxFanOutPaths |
The most paths one batched request may carry. Beyond this the request is refused rather than accepted and throttled part way through. |
Locks:MaxFanOutRetries |
How many times a throttled lock call is retried before it is reported as failed. |
Locks:MaxThrottlePause |
The longest an upstream's Retry-After may pause batched lock calls to it (default 60 seconds, at most one day). A call told to wait longer pauses the upstream for this long and is reported as failed at once with upstream's status. |
Upstreams:<name>:Repositories |
Required. Path patterns this upstream may serve, matched against the whole path after the upstream key, with * inside one segment and ** across segments. An entry normally ends in **, because the path continues into info/lfs/.... Use ** to allow everything. |
Configuration is validated at startup and the process refuses to run on a bad value, reporting every problem at once and naming the setting each came from. That includes a store root that is not writable: a cache proxy whose volume is missing looks healthy from the outside and then fails every transfer, which is worse than a pod that will not start and says why.
Kubernetes
A kustomize base lives in deploy/k8s/. Create the token key secret first, since it is deliberately not in the base:
kubectl create secret generic gitlfscache-tokens \
--from-literal=tokenKey="$(head -c 32 /dev/urandom | base64)"
kubectl apply -k deploy/k8s
Three things about that base are worth knowing before you change it.
It is a StatefulSet, not a Deployment. The cache lives on a read-write-once volume, which cannot be shared between pods. Transfer URLs are stateless so any replica can serve any URL, but each replica holds its own cache and the hit rate dilutes as replicas grow. Start at one replica with a large volume and scale out only when a single pod saturates.
The ingress annotations are load bearing. Nginx buffers request bodies by default, which defeats the streaming upload tee and caps object size at whatever the controller is willing to buffer. proxy-request-buffering: "off" and proxy-body-size: "0" are what let a large push stream through, and the generous read and send timeouts are what stop a large transfer being cut off partway. These are nginx-specific: a different ingress controller needs its own equivalents, and the failure mode if they are missed is silent, with small objects working and large ones failing.
Provision the volume above the byte budget. Eviction runs on an interval, so the store can sit over budget between sweeps.
Memory does not scale with object size, because nothing is ever buffered whole. A 20 GB object and a 20 MB object have the same memory profile, which makes the resource limits straightforward.
File locking
The locking API is part of the Git LFS HTTP API, addressed as a suffix of whatever lfs.url points at, so a client already sends its lock traffic here. The proxy terminates two parts of it.
The listing is cached. GET .../locks