Inner workings of nginx cache manager

Maxim Dounin mdounin at
Thu Nov 29 14:50:45 UTC 2018


On Thu, Nov 29, 2018 at 03:23:51PM +0100, Stefan Safar wrote:

> Hi there,
> I'd like to know a little bit more about the inner workings of the cache
> manager. I looked through the code, and if I understand it correctly, when
> it runs out of disk space specified by max_size, it tries to run the cache
> manager, which looks at a queue of last-accessed URLs and tries to remove
> the least used URL from the queue and from the disk.
> My question is, whether the queue is somehow persisted on disk, or I
> misunderstood something? What I'm trying to know is what happens when an
> nginx instance runs out of disk space and it's restarted - how does nginx
> know what it should or shouldn't delete? I don't think I saw any code that
> would scan through the disk and that would be a rather slow way to deal
> with this.

The LRU queue is only mantained in memory.  When nginx is 
restarted, details of which cache items were access last are lost, 
and nginx will have to remove some items due to max_size reached, 
it will remove mostly arbitrary items unless they were accessed 
after the restart.

Note though that there is a code which scans though the disk to 
find out which items are in the cache (and how much space they 
take).  The cache loader process does this, see for a high level description 
of how it works.

Maxim Dounin

More information about the nginx mailing list