Cache invalidation
The proxy caches the result of every mapping lookup in memory, so that established accounts resolve without reading the mapping store. The cache has a side effect: a change made directly in the store does not take effect at once. An account in the cache keeps resolving to its previous destination until its entry expires. When an account moves and must follow at once, its cache entry is therefore invalidated.
Cache lifetimes
Section titled “Cache lifetimes”The lifetimes are set in [mapping]. positive_ttl, 10 minutes by default, applies to a lookup that found a mapping. negative_ttl, 30 seconds by default, applies to a lookup that found none. A failed lookup is cached for 5 seconds or negative_ttl, whichever is shorter, as described under Caching.
Without intervention, an account whose mapping changes in the store picks up its new destination within one of these lifetimes: within positive_ttl when it had a mapping before, and within negative_ttl when it resolved to the default destination.
Not every change needs intervention. Mappings added, changed or deleted through the web interface or the management API invalidate the account’s cache entry themselves, and need no further step. A configuration reload that changes [mapping] (including the lifetimes), routing.default_destination or the set of declared destinations empties the cache. So does a reload that finds the mapping file changed. A reload that changes none of these keeps the cache.
Invalidating one account or the whole cache
Section titled “Invalidating one account or the whole cache”On the cache page of the web interface, Invalidate removes the cached lookup of one account and Invalidate all clears the whole cache. The lookup panel on the mappings page also offers Invalidate cache for the account it shows. Through the API, the cached lookup of one account is removed with:
TOKEN=$(sudo cat /etc/proxy/admin-token)curl -sk -X POST -H "Authorization: Bearer $TOKEN" \ https://127.0.0.1:9443/api/v1/cache/invalidateThe response is {"invalidated":"one"}, and the account’s next connection reads the store and picks up its current destination. The identifier goes in the JSON body; a query string is ignored, so a request with the identifier in the URL clears the whole cache instead.
Without a body, or with an empty JSON object, the same call clears the whole cache and answers {"invalidated":"all"}, so that every account is looked up again on its next connection. This is the step that follows a bulk change of the store. Expect a burst of store queries afterwards, one per account as it reconnects.
Invalidating an account’s entry also releases a login delay on that account, and clearing the whole cache releases every delay. Invalidation does not close open sessions: they keep their backend until they end, or until they are kicked from the connections page or with POST /api/v1/connections/kick.
Reloading the mapping file
Section titled “Reloading the mapping file”Invalidating the cache does not read the store again. This matters for the file store, which holds the file’s content in memory. After the file is edited on the host, reload it first, with Reload store on the mappings page or through the API, and then clear the cache:
curl -sk -X POST -H "Authorization: Bearer $TOKEN" \ https://127.0.0.1:9443/api/v1/mappings/reloadcurl -sk -X POST -H "Authorization: Bearer $TOKEN" \ https://127.0.0.1:9443/api/v1/cache/invalidateThe reload reads and checks the file, and replaces the table in memory only when the file is valid. It also releases every login delay, but it keeps the cache, which is why the second call follows. A file that cannot be read or parsed is reported with 502 and the code reload_failed, and the previous table stays in use. The Redis and SQL stores are read on every cache miss, and have nothing to reload. For them, invalidating the cache is enough to make a change in the store visible.
A bulk switch of accounts therefore takes three steps:
- Write the new entries to the store: import them through the web interface or
POST /api/v1/mappings/import, which also invalidates their cache entries; or edit and reload the file; or write to Redis or SQL directly and then clear the cache. - Check a few accounts with the route lookup on the mappings page, or
GET /api/v1/mappings/lookup. - Close the switched accounts’ sessions with a kick, so that their clients reconnect to the new destination.