Memory usage in nginx proxy setup and use of min_uses
Maxim Dounin
mdounin at mdounin.ru
Mon May 17 14:37:38 UTC 2021
Hello!
On Sun, May 16, 2021 at 04:46:17PM +0000, Lucas Rolff wrote:
> Hi everyone,
>
> I have a few questions regarding proxy_cache and the use of
> proxy_cache_min_uses in nginx:
>
> Let’s assume you have an nginx server with proxy_cache enabled,
> and you’ve set proxy_cache_min_uses to 5;
>
> Q1: How does nginx internally keep track of the count for
> min_uses? Is it using SHM to do it (and counts towards the
> key_zone limit?), or something else?
>
> Q2: How long time does nginx keep this information for the
> number of accesses. Let’s say the file gets visited once in a 24
> hour period; Would nginx keep the counter at 1 for that whole
> period, or are there some set timeout where it’s “flushed”.
>
> Q3: If you have a user who decides to access files with a random
> query string on it; We want to prevent caching this to fill up
> the storage (The main reason for setting the
> proxy_cache_min_uses in the first place) – but are we gonna fill
> up the memory (and keys_zone limit) regardless; If yes – is
> there a way to prevent this?
>
> Basically the goal is to understand even just broadly how
> min_uses are counted, and possibly how to prevent memory from
> being eaten up in case someone decides to access the same URL
> once with millions of requests – if there’s any way to flush out
> the memory for example, for anything that haven’t yet reached
> the proxy_cache_min_uses if it indeed uses up memory.
The proxy_cache_min_uses basically means that if nginx sees a
request whose uses count not reached the specified limit yet, it
won't try to store the response to disk. It will, however, keep
the key in the keys_zone with the relevant information, notably
the number of uses seen so far. Quoting the proxy_cache_path
directive description (http://nginx.org/r/proxy_cache_path):
"In addition, all active keys and information about data are stored
in a shared memory zone, whose name and size are configured by the
keys_zone parameter. One megabyte zone can store about 8 thousand
keys."
Much like with any cache item, such keys are removed from the
keys_zone if no matching requests are seen during the "inactive"
time. Similarly, least recently used keys are removed if there is
not enough room in the keys_zone.
Much like with normal caching, you can control the cache key nginx
uses. If you don't want to take query string into account, you
may want to configure proxy_cache_key without the query string
(see http://nginx.org/r/proxy_cache_key).
--
Maxim Dounin
http://mdounin.ru/
More information about the nginx
mailing list