Add the object cache

Add the object cache

Run shared caching proxies in front of the standard bucket so repeated reads skip object storage.

The object cache is a set of caching proxies in front of the standard bucket, one set per availability zone. Nodes and Gateways send their reads of stored objects through it, so ten consumers replaying the same history cause one storage request instead of ten. Writes never go through it, and a read whose cache instance fails or is slow goes straight to object storage, so the cache cannot lose data or fail a read.

Add it when many readers replay the same history, or when Gateways serve old records from storage. Clients notice nothing while you add it, except that repeated reads of old records get faster.

What it caches

The cache holds byte ranges of objects in the standard bucket: transit bundles, which hold the recent records of many streams, and stream objects, which hold one stream's older records. One cached transit bundle serves every stream whose records it holds. Express WAL slots, in the express buckets, bypass the cache. The auditor's denorm job reads every transit bundle once, so it uses its own fetch cache on the auditor's disk instead; see The auditor.

Nodes and Gateways use the same instance list, so a page one of them fetched serves the others. Deletes also go through the cache, which drops the copy in the deleting node's zone at once.

One pool per zone

Run a separate pool in each availability zone, and give each node and Gateway the list for its own zone. A single pool for the whole region would send about two thirds of reads to another zone, and you would pay for the transfer both ways.

An object read in three zones can have three cached copies. Copies in other zones than the deleting node's, and copies left behind when instances change, expire after their one-hour lifetime. If a zone's pool is down, its readers fetch from storage directly.

Before you start

  • The proxy image Nova tests against: ghcr.io/fenos/sample-s3-hybrid-cache, pinned to a commit in deploy/docker/docker-compose.yml. Use that exact tag; a different build can pass every request through without caching it.
  • Two or three instances per zone, on machines with guaranteed network bandwidth. The cache's throughput depends on network capacity.
  • Local disk on each instance, sized for the history readers replay at once. It does not need to hold all retained history: a miss fetches from storage. The example gives each instance 10 GiB.

Run the instances

Write the two configuration files

The proxy runs in proxy-only mode, with caching off by default. A rules file then turns caching on for the Nova bucket only, so nothing else is cached by accident:

config.yaml
server:
  mode: "proxy_only"
  proxy_port: 3128

cache:
  cache_dir: "/var/cache/s3proxy"
  max_cache_size: 10737418240 # 10 GiB per instance, local disk only
  read_cache_enabled: false # allowlist: rules opt buckets in

metrics:
  enabled: true
  otlp:
    enabled: true
    endpoint: "http://prometheus:9090/api/v1/otlp/v1/metrics"
    export_interval: "15s"
cache_rules.json
{
  "rules": [
    {
      "pattern": "nova/**",
      "read_cache_enabled": true,
      "page_widening": true,
      "page_size": 16777216,
      "get_ttl": "1h"
    }
  ]
}

Name your bucket in pattern. The 16 MiB page matches the size of a stream object, so a small first read often caches the whole object. get_ttl expires any copy that a delete did not reach. The Compose stack's copy of these files, in deploy/docker/objectcache/, adds a connection_pool block for a local RustFS: a DNS server that knows container names, and plain HTTP to rustfs:9000.

Start the instances

docker run -d --name objectcache-0 --hostname objectcache-0 --network nova \
  -v ./config.yaml:/etc/s3proxy/config.yaml:ro \
  -v ./cache_rules.json:/var/cache/s3proxy/cache_rules.json:ro \
  ghcr.io/fenos/sample-s3-hybrid-cache:sha-576d4b42eb5a5dc9ac1a128be371bf44e4846832

Start objectcache-1 the same way. Each instance listens on port 3128 and keeps its cache on its own disk. Replacing an instance loses its cached copies, which are fetched again on demand.

Point the nodes and Gateways at them

Give every node and every Gateway in the zone the same list:

novad … --object-cache-instances http://objectcache-0:3128,http://objectcache-1:3128
novagateway … --object-cache-instances http://objectcache-0:3128,http://objectcache-1:3128
--object-cache-instancesDefaultempty (off)
Instance base URLs, comma-separated, each http://host:port. Cached traffic is plain HTTP.
--object-cache-load-factorDefault1.25
How far above the average an instance's open requests may go before a busy key spills to the next instance. Must be above 1.
--object-cache-connect-budgetDefault1s
Time to connect to an instance before the read goes to storage directly.
--object-cache-first-byte-budgetDefault5s
Time to the first byte before the read goes to storage directly. Keep it above your storage's slowest normal reads.

The same four flags exist on both binaries. A list entry that is not an http://host:port URL stops the process at startup.

Check that reads use it

Read old records through a node, then look at its metrics:

nova --addr nova-2:7780 read nova://orders/history --from 0 > /dev/null
curl -s nova-2:9090/metrics | grep '^nova_objectcache'
nova_objectcache_requests_total{op="delete"} 0
nova_objectcache_requests_total{op="get"} 2
nova_objectcache_ring_size 2

ring_size is the number of instances in use, and requests_total{op="get"} grows as reads go through the cache.

When an instance fails

A read that cannot connect within the connect budget, or that gets no first byte within the first-byte budget, goes to storage directly and counts a fallback with its cause: connect, first_byte, error, stall, midstream or ejected. After 3 failures in a row, a node or Gateway stops using the instance for 30 seconds, then tries it again. Readers see a slower read, never an error from the cache.

Watch it

Nova counts its use of the cache on every node and Gateway: requests, fallbacks by cause, spills to another instance, ejections, and the number of instances in use. Their full names are in the metrics catalogue. The proxy sends its own metrics by OTLP to Prometheus: hit ratio, bytes served and saved, evictions and failures. The nova-objectcache Grafana dashboard shows both.

Look into the cache when ejections last several minutes, or when the hit ratio falls while requests continue. A hit ratio near zero for Nova's traffic usually means page widening stopped working, often after a change of image.

On Kubernetes, the Constellation's objectCache block runs one pool per zone and gives each node and Gateway its zone's list; see The cluster resource.

If it goes wrong

SymptomLikely causeWhat to do
requests_total{op="get"} stays at 0The flag is missing, or reads hit the leader's memoryCheck --object-cache-instances on every node; only reads of stored objects use the cache
fallbacks_total{cause="connect"} growsAn instance is down or unreachableCheck the instance and port 3128
fallbacks_total{cause="first_byte"} growsThe instance or storage is slowCheck the instance's load; keep the first-byte budget above storage's normal latency
Hit ratio near zeroThe rules file does not name your bucket, or page widening stoppedCheck pattern in cache_rules.json and the image tag
novad or novagateway exits: object-cache-instances: "…" must be an http://host:port base URLAn entry is not an http://host:port URLFix the list

See also

On this page