ELI5: In terms of web - what is a cache and how does it work?

On a computer, a 'cache' is a quick to access [but small] place, where the computer stores a few things that have recently been retrieved from a slow to access [but bigger] place, in case they're wanted again. [Because, usually, they are.]

Caches pop up in tons of contexts, due to various levels of 'fast'. The CPU has a few megabytes of 'cache' on it, to store instructions taken from RAM. And RAM is mostly used as a 'cache' for things on the hard drive. And the hard drive is used to cache things from the internet.

But... Varnish is a bit of a weird cache.

Varnish is used to cache the output of web servers. Lets use Reddit as an example.

On Reddit's servers, there's a program that 'builds' each and every web page on the site, out of various little components stored in a database. Whenever anyone visits any page, the server has to figure out what posts/comments/whatever they want to see, grab all the various metadata about them, and mash it all together into a web page.

That takes fractions of a second, but it's vastly slower than just giving the user a prebuilt page.

So Varnish caches the generated pages, when it is possible to do so. For instance, you could set varnish up so that the logged-out home page is 'cached' for 30s. This would mean that the server builds it the first time it's requested. Varnish notices that, and holds onto a copy of the generated page. Then for the next 30s, every other request is 'intercepted' by Varnish, and Varnish gives user the old page, instead of asking Reddit to make a new one. After 30s, the next request will generate a new page again.

This will make Reddit respond faster to user requests, and decrease the amount of servers Reddit would need to serve everyone promptly. [except, not really, because they already do cache various things.]

/r/explainlikeimfive Thread