Debian Codegraph @

docs/store_caching.rst

raw
1 Store caching
2 =============
3
4 The ``Store`` can optionally use a second backend as a local cache for selected
5 namespaces, which is especially useful when the primary backend is remote,
6 slower or otherwise more "expensive" than the cache.
7
8 Configuration
9 -------------
10
11 - ``cache_url`` or ``cache_backend``: where cached data is stored
12 - ``config``: mapping of namespace to its configuration dict, containing
13 nesting levels and cache policy settings.
14
15 Each namespace configuration dictionary can have:
16
17 - ``levels``: a required list of integers specifying nesting levels.
18 - ``cache``: optional cache mode, accepting ``CacheMode`` values or string
19 aliases:
20
21 - ``CacheMode.C_OFF`` or ``"off"``: bypass cache completely (default).
22 - ``CacheMode.C_MIRROR`` or ``"mirror"``: always read from primary backend,
23 but update the cache after successful primary backend reads and writes.
24 - ``CacheMode.C_WRITETHROUGH`` or ``"writethrough"``: read-through +
25 write-through. For now, only content-hash addressed namespaces should
26 use this mode.
27
28 - ``max_age``: optional maximum age expressed in seconds since last access.
29 The default is ``None`` (no age limit).
30 - ``size``: optional maximum size in bytes. It sets a per-namespace cache
31 size budget enforced by evicting least-recently-used items until the
32 namespace total size is within the configured budget. Items bigger than
33 ``size`` are not cached.
34
35 Example::
36
37 from borgstore.store import Store, CacheMode
38
39 store = Store(
40 url="sftp://user@host/repo",
41 config={
42 "data": {
43 "levels": [2],
44 "cache": "writethrough",
45 "max_age": 3600,
46 "size": 4 * 1024**3,
47 },
48 "meta": {
49 "levels": [1],
50 "cache": CacheMode.C_MIRROR,
51 },
52 },
53 cache_url="file:///home/user/.cache/borgstore/repo",
54 )
55
56 Behavior
57 --------
58
59 - Cache keys are identical to primary backend keys (same nesting).
60 - Soft-deleted items are cached under the same ``.del`` name as primary.
61 - Soft delete/undelete renames cache entries as well.
62 - Cache failures are non-fatal and logged as warnings.
63
64 Eviction
65 --------
66
67 For each namespace that has a ``max_age`` or ``size`` limit, the ``Store`` keeps
68 an in-memory index of the cached items (name, size, time of last use) while it
69 is opened. The index is ordered by the last use *by this store*: a cache hit,
70 putting an item into the cache or moving it counts as using it.
71
72 - On ``Store.open()`` and ``Store.close()``, the namespace is scanned (listed) and
73 cleaned up. When scanning, items the store did not know yet are ordered by
74 their ``ItemInfo.atime``.
75 - Before an item is put into the cache (by ``store()`` or by a ``load()`` that
76 was a cache miss), room is made for it, so the namespace total size stays
77 ``<= size`` also while the store is in use.
78
79 Cleanup order per namespace is:
80
81 1. remove expired cache objects when ``max_age`` is configured,
82 2. if ``size`` is configured, evict the least-recently-used remaining items
83 until the namespace total size (plus the size of the item to put into the
84 cache) is ``<= size``.
85
86 Expired entries are always removed first, even if total size is already below
87 the ``size`` limit. A cache hit never expires anything: an expired item that is
88 still in the cache is served (and that counts as using it).
89
90 Shared caches
91 -------------
92
93 Multiple clients (``Store`` instances, also in different processes) may use the
94 same cache at the same time, e.g. a cache directory shared by several
95 processes working with the same content-hash addressed data:
96
97 - The posixfs backend stores items atomically, so a client never sees or
98 evicts an incompletely written cache item.
99 - If another client has evicted an item, that is a cache miss and the item
100 gets cached again.
101 - A client only knows what it has put into the cache itself and what it has
102 seen when it last scanned the namespace. Thus, a namespace with a ``size``
103 limit is scanned again after the client has put more than ``size / 4`` bytes
104 into it. So, while N clients are putting items into the cache, the namespace
105 total size usually is above ``size``, it can reach about
106 ``size * (1 + N / 4)``. It is within ``size`` again when the last of these
107 clients has closed the store.
108 - Scanning a namespace with a lot of items takes a while. To not block the
109 store for that long, the scan is done in steps while the store is in use:
110 each item that is put into the cache continues the scan for about 5 ms, the
111 store uses the cache as usual between these steps. ``Store.open()`` and
112 ``Store.close()`` scan the namespace in one go.
113 - Clients do not see each other's cache hits (see the ``atime`` limitation
114 below), so a client might evict an item another client frequently uses.
115 - If the clients use different limits for the same namespace, the smallest
116 limits win.
117
118 Manual Cache Invalidation
119 -------------------------
120
121 If you need to programmatically clear or invalidate parts of the cache (for
122 example, to resolve stale objects after primary backend deletes by other
123 clients, or if cache corruption is suspected), you can use the
124 ``cache_invalidate`` method:
125
126 - To invalidate a single item::
127
128 store.cache_invalidate("data/00000000")
129
130 - To invalidate all cached items in a specific namespace (e.g. ``"data/"``)::
131
132 store.cache_invalidate("data/")
133
134 - To invalidate all cached items across all configured namespaces, pass
135 ``ROOTNS``::
136
137 from borgstore.constants import ROOTNS
138 store.cache_invalidate(ROOTNS)
139
140 Limitations
141 -----------
142
143 - No proactive cache validation/revalidation.
144 - If an object is deleted in the primary backend by another client, the local
145 cache will still have a stale object.
146 - For items a ``Store`` has not used itself since it was opened (items cached
147 in a previous session or by another client), ``max_age`` and LRU-by-``size``
148 depend on the timestamps the cache backend gives in ``ItemInfo``:
149
150 - ``atime`` (``posixfs``, ``sftp`` and ``REST`` backends): filesystems often
151 do not update the atime for each read (e.g. ``relatime`` or ``noatime``
152 mounts), so it can be older than the real last use.
153 - If ``atime`` is 0 (not implemented), ``mtime`` is used instead (``s3``
154 backend). That is the time when the item was put into the cache, so
155 ``max_age`` refers to that time and ``size`` evicts the items first that
156 were cached first.
157 - If ``mtime`` is 0 also (``rclone`` backend), using ``max_age`` would remove
158 these items from the cache when it is scanned and using ``size`` would not
159 evict these items in LRU order, because their order can't be determined.
160 - If a partial range ``load`` call for an object in a cached namespace causes
161 a cache miss, the full object will be read from the primary backend and the
162 cache will be populated with the full object (if it is not bigger than
163 ``size``).
164
165 Statistics
166 ----------
167
168 ``Store.stats`` includes cache counters:
169
170 - ``backend_load_volume``
171 - ``backend_store_volume``
172 - ``backend_load_calls``
173 - ``backend_store_calls``
174 - ``backend_delete_calls``
175 - ``cache_disabled``
176 - ``cache_hits``
177 - ``cache_misses``
178 - ``cache_hit_ratio``
179 - ``cache_errors``
180 - ``cache_load_volume``
181 - ``cache_store_volume``
182 - ``cache_load_calls``
183 - ``cache_store_calls``
184 - ``cache_delete_calls``