Model: A Proxy Repository Cache
A proxy repository is a cache with two kinds of content, and they behave differently:
- Artifacts (
requests-2.34.2-py3-none-any.whl,lodash-4.18.1.tgz,fmt-12.1.0.tar.gz): immutable. Once downloaded, a file never needs to be fetched again. The only question is how much disk to give the cache. - Metadata (the index page listing all versions of
requests, npm's packument, Maven'smaven-metadata.xml): changes whenever upstream publishes a new version. The proxy keeps it for a metadata TTL ("Metadata Retrieval Cache Period" in Artifactory, "Maximum metadata age" in Nexus), then asks upstream again.
This lesson models a proxy for one week of CI traffic and answers the questions that come up when you run one: what hit rate to expect, what disk size is enough, how stale metadata can get, and what happens when upstream goes down.
The traffic
CI jobs request packages with a long-tail pattern: a few packages are used by every job, many are rare. The model generates a deterministic request stream (about 200 requests an hour for a week), then replays it against a proxy whose artifact cache evicts the least recently used files when it's full:
from collections import OrderedDict
PACKAGES = [f"pkg{i:03d}" for i in range(300)]
SIZE_MB = {p: 1 + (i * 7) % 20 for i, p in enumerate(PACKAGES)} # 1..20 MB each
def requests_for_hour(hour):
"""About 200 requests/hour; package k is requested with weight ~ 1/(k+1) (long tail)."""
out = []
for slot in range(200):
k = (hour * 7919 + slot * 104729) % 300
k = (k * k) // 300 // max(1, (slot % 5)) # skew toward low indices
out.append(PACKAGES[min(k, 299)])
return out
def simulate(cache_mb):
lru, used = OrderedDict(), 0
stats = dict(requests=0, hits=0, upstream_mb=0)
for hour in range(24 * 7):
for pkg in requests_for_hour(hour):
stats["requests"] += 1
if pkg in lru: # artifact cached: serve it locally
lru.move_to_end(pkg)
stats["hits"] += 1
continue
stats["upstream_mb"] += SIZE_MB[pkg] # miss: download, store, evict LRU if full
lru[pkg], used = True, used + SIZE_MB[pkg]
while used > cache_mb:
old, _ = lru.popitem(last=False)
used -= SIZE_MB[old]
return stats
no_proxy_mb = sum(SIZE_MB[p] for h in range(24 * 7) for p in requests_for_hour(h))
print(f"without a proxy: {no_proxy_mb / 1000:.1f} GB downloaded from upstream in a week")
print(f"catalogue: {len(SIZE_MB)} packages, {sum(SIZE_MB.values()) / 1000:.2f} GB")
print(f"{'cache':>8} {'hit rate':>9} {'upstream':>10}")
for cache_mb in [100, 500, 1000, 2000, 4000, 8000]:
s = simulate(cache_mb)
print(f"{cache_mb:>6}MB {s['hits'] / s['requests']:>9.1%} {s['upstream_mb'] / 1000:>8.1f}GB")without a proxy: 324.3 GB downloaded from upstream in a week
catalogue: 300 packages, 3.15 GB
cache hit rate upstream
100MB 13.8% 292.1GB
500MB 41.6% 207.4GB
1000MB 56.6% 153.9GB
2000MB 85.3% 52.8GB
4000MB 99.3% 2.5GB
8000MB 99.3% 2.5GB
- Even the smallest cache saves something, because the most popular packages stay in it, but an undersized cache keeps evicting files that are requested again a few hours later and downloads them over and over.
- Once the cache holds the whole working set (every package actually requested during the week: 2.5 GB of the 3.15 GB catalogue here), each artifact is downloaded exactly once: 2.5 GB instead of 324 GB, and doubling the disk again changes nothing. Size proxy caches to the working set plus growth, not to the size of the upstream registry.
Upstream outages
The same traffic, with upstream unreachable for six hours on day 3. Requests for files still in the cache are served; everything else fails the build:
from collections import OrderedDict
PACKAGES = [f"pkg{i:03d}" for i in range(300)]
SIZE_MB = {p: 1 + (i * 7) % 20 for i, p in enumerate(PACKAGES)}
def requests_for_hour(hour):
out = []
for slot in range(200):
k = (hour * 7919 + slot * 104729) % 300
k = (k * k) // 300 // max(1, (slot % 5))
out.append(PACKAGES[min(k, 299)])
return out
def simulate(cache_mb, outage):
lru, used, failed, served = OrderedDict(), 0, 0, 0
for hour in range(24 * 7):
for pkg in requests_for_hour(hour):
if pkg in lru:
lru.move_to_end(pkg)
served += hour in outage
continue
if hour in outage:
failed += 1
continue
lru[pkg], used = True, used + SIZE_MB[pkg]
while used > cache_mb:
old, _ = lru.popitem(last=False)
used -= SIZE_MB[old]
return served, failed
outage = range(50, 56)
for cache_mb in [100, 1000, 4000]:
served, failed = simulate(cache_mb, outage)
print(f"cache {cache_mb:>5} MB during the outage: {served} requests served, {failed} failed")cache 100 MB during the outage: 177 requests served, 1023 failed
cache 1000 MB during the outage: 791 requests served, 409 failed
cache 4000 MB during the outage: 1200 requests served, 0 failed
With a cache that holds the working set, the outage is invisible to CI. With a tiny cache, most requests fail while upstream is down. That resilience is often the main reason to run a proxy at all, ahead of speed.
Running a proxy well
- Size for the working set, watch the cache hit rate in the repository manager's metrics, and grow before it drops.
- Metadata TTL: a short TTL makes new upstream versions visible quickly, at the cost of more metadata requests; a long one keeps resolution working on the last known metadata during an outage. Minutes to an hour for fast-moving ecosystems (npm), longer where "latest" doesn't matter because builds use lockfiles; with lockfiles, metadata is rarely needed at all.
- Negative caching: remember "not found" answers for a while, so a typo in a dependency name doesn't hit upstream on every build (and remember to keep that period short).
- Never let a proxy store a different file under the same name: artifacts are immutable, and checksums from upstream are verified on download (Checksums & Signatures).
- CI and developer machines both point at the proxy, so they see the same packages, and the proxy logs show what the organisation actually uses.
Test yourself
Why does making the proxy cache larger than the week's working set not reduce upstream traffic further?
- Proxies ignore the size
- Each artifact is already downloaded only once
- Metadata dominates
- Upstream blocks large caches
What is the trade-off of a long metadata TTL?
- More disk
- New upstream versions appear later, but builds survive upstream outages longer
- Artifacts get corrupted
- Lower hit rate for artifacts
During an upstream outage, which requests still succeed?
- None
- Requests for artifacts still in the proxy cache
- Only metadata requests
- All of them