How Varnish Works: High‑Performance In‑Memory HTTP Caching Explained

Varnish is a high‑performance open‑source reverse proxy and HTTP cache that stores data in memory for faster retrieval, using a master process to manage child workers that handle requests, allocate, serve, and evict cached objects based on LRU policies.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How Varnish Works: High‑Performance In‑Memory HTTP Caching Explained

What is Varnish

Varnish is a high‑performance open‑source reverse‑proxy and HTTP‑caching server. Its functionality is similar to Squid, but Varnish stores cached data in memory rather than on disk, which makes it faster while the cache is lost on server restart.

How Varnish Works

Initialization Process

The master process reads the configuration file, creates a storage space of the size specified by the administrator (e.g., 2 GB), and launches child processes. Child processes handle subsequent tasks and allocate threads for accepting HTTP requests, allocating cache space, cleaning expired objects, and performing space‑fragmentation cleanup.

HTTP Request Handling

A dedicated thread constantly listens on the request port. When a request arrives, it wakes a worker thread. The worker parses the request URI, checks the cache for the object, and if found returns it directly. If not, the request is forwarded to the backend server, the response is cached, and then returned to the client.

Cache Allocation

When an object needs to be cached, Varnish searches the free cache area for the smallest suitable free block. The object is placed there; any remaining space becomes a new free block. If no free space exists, Varnish evicts objects based on a least‑recently‑used policy to make room.

Cache Release

A dedicated thread periodically scans cached objects and removes those that have not been accessed within a configured lifetime, freeing their memory. After freeing space, the thread checks adjacent free blocks and merges them into larger blocks to reduce fragmentation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

reverse proxyBackend PerformanceMemory CacheHTTP CachingVarnish
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.