Skip to content

storage: the in-memory file metadata cache has no size limit, and copies and decodes every entry on each read #913

Open

Description

@pbleser-oc

The file metadata cache of the storage drivers uses go-micro's memory store by default (STORAGE_USERS_FILEMETADATA_CACHE_STORE=memory in opencloud). Two findings from reading the code, low priority, but worth keeping track of:

1. The cache has no size limit

The memory store is a thin wrapper around github.com/patrickmn/go-cache (go-micro.dev/v4/store/memory.go), a plain map without a size limit. Entries are only removed by the TTL: cacheStore.PushToCache (pkg/storage/cache/cache.go:193) gives every entry an expiry of cache_ttl, 24 hours by default (STORAGE_USERS_FILEMETADATA_CACHE_TTL), counted from the last write.

So the cache holds the metadata of every node read or written in the last 24 hours, and its memory use depends on how many files were touched, not on anything an admin can configure. A full opencloud posixfs scan, or a sync client walking a large tree, puts every node in it. Entries also vary a lot in size: a file with many shares carries all its grants.

A bounded cache would make the memory use predictable. Evicting entries is safe, as a miss only reads the metadata from disk again. Suggestions:

  • limit the cache by bytes, not by number of entries, as entries vary in size;
  • keep the TTL: with several storage-users instances it is also the only bound on staleness (a cache hit is not compared with the disk, see MessagePackBackend.loadAttributes, pkg/storage/pkg/decomposedfs/metadata/messagepack_backend.go:212, and the same in the hybrid backend).

2. Every read copies and decodes the entry (to be measured)

The go-micro Store interface only stores bytes. A cache read:

  1. looks up the key under go-cache's lock (cache.Get);
  2. copies the stored value into a new record (memoryStore.get);
  3. decodes it with msgpack into a new map (cacheStore.PullFromCache, cache.go:189).

Every write encodes and copies again. That is several allocations and a msgpack round trip for every metadata read, i.e. for every stat, PROPFIND entry and download. My guess is that this costs more than the locking, but it has not been measured: before changing anything, CPU, allocation and mutex profiles (pprof) of storage-users under load, e.g. a PROPFIND-heavy sync, would show whether the cache matters at all.

If it does, a maintained concurrent cache library with a byte limit and approximate LRU eviction (e.g. otter or ristretto), holding the decoded attributes as immutable values, would address both points. Check the write semantics of the chosen library: if writes are applied asynchronously, a read right after a write could return the previous metadata, e.g. a stale nodestatus right after an upload finished.

go-cache itself has had no release since 2017.

Not needed: sharding the map

go-cache protects its map with a single sync.RWMutex (cache.go:43). A sharded map looks like the obvious improvement, but is unlikely to help, and has costs:

  • Readers already run in parallel, and the critical sections are tiny: one map lookup or assignment. The copies and the msgpack decoding happen outside the lock. The lock would only be contended with many cores at a very high request rate, which a mutex profile can confirm or rule out.
  • Operations over the whole map get more complex: the store interface has prefix reads and List, and go-cache has its expiry pass, Items() and Flush(). With shards, each of them has to visit every shard and is no longer atomic. The file metadata cache only uses exact keys, but the memory store serves other caches too.
  • Eviction becomes per shard: with a size limit, a busy shard evicts entries that a global policy would keep.
  • A strict LRU would make it worse, not better: it updates its recency list on every read, so reads would need the exclusive lock. That is why concurrent caches approximate LRU (CLOCK, sampling, W-TinyLFU); a library from point 2 handles this, which a hand-written sharded LRU would have to solve again.

One real cost of the current lock: go-cache's expiry pass (DeleteExpired, every 5 minutes) holds the write lock while it walks the whole map, so it blocks all reads and writes for a time that grows with the cache. A bounded cache shortens that too.

All of the above is from reading the code (reva main, opencloud main with the vendored go-micro fork and go-cache v2.1.0); nothing was measured.

Activity

  1. pbleser-oc commented on Oct 25, 2026

    @pbleser-oc
    MemberAuthor

    Some numbers on what the unbounded cache costs and what it brings, measured with opencloud posixfs scan, which opens its own posix driver with the same file metadata cache as the server (STORAGE_USERS_FILEMETADATA_CACHE_STORE). opencloud main (8acaef28e2), single binary stopped, only NATS running; trees of 200 directories with 1,000 small files each; every configuration twice, in alternating order:

    memory (default) noop
    first scan, 200,000 new files 434 s / 446 s, peak 396 / 399 MB 531 s / 534 s, peak 109 MB
    rescan, 2,000 of the 200,000 files changed 75 s / 71 s, peak 111 MB 71 s / 72 s, peak 108 MB
    • The cache pays off: the first scan is about 20% faster with it, probably thanks to the repeated reads of the parent directories while the scan propagates tree sizes and mtimes. For a rescan with few changes it makes no difference.
    • It's unbounded: the scan keeps every new file and every directory in the cache until it exits, about 290 MB more for 200,000 files, i.e. ~1.4 KB per new file. Extrapolated, a first scan of 10 million new files, e.g. after a migration, would use on the order of 14 GB (not measured). The server's memory cache grows the same way with the files it touches within the TTL (24 h by default).
    • noop isn't an alternative today: posix refuses to start with a noop file metadata cache ("the posix driver requires a file metadata cache", pkg/storage/fs/posix/posix.go), since 688e625 in Fix race condition #650, which also switched the posix integration tests from noop to memory because "posixfs doesn't work with noop" (f42720a). Why isn't documented yet; for the measurement above, I removed the check in a local build. Every file got an ID with noop as well, but that says nothing about the races Fix race condition #650 fixed.

    So a bounded cache looks like the best of both: it should keep most of the ~20%, as the entries that are read again are mostly the directories, a small fraction of all nodes (an expectation, not measured), keeps the memory predictable, and keeps whatever noop breaks on posix out of the picture. Until the reason for the noop check is known, noop shouldn't be the way to bound the memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions