Squeezing Every Byte: Squid Cache Optimization
As we approach the turn of the millennium, web traffic is skyrocketing. If you're serving images and scripts directly from your Apache 1.3 servers, you're wasting precious CPU cycles and RAM on process forking. You need a dedicated reverse proxy. Squid 2.2 is the industry standard for a reason, but the default squid.conf is far from optimal for a high-load environment.
Memory Management: The cache_mem Trap
Many admins think setting cache_mem to a huge value will make Squid faster. Wrong. cache_mem only sets the limit for in-transit objects and "hot" objects. If you set it too high, the OS will swap, and your latency will go through the roof. For a 256MB RAM server, keep it around 64MB. Let the filesystem cache handle the rest.
Disk I/O: Use aufs
The standard ufs storage module is blocking. Every time Squid writes a cache hit to disk, the whole process waits. On Linux, switch to aufs (Async UFS). It uses threads to handle disk I/O, allowing the main loop to keep serving requests.
# squid.conf
cache_dir aufs /var/spool/squid 2000 16 256
Fine-Tuning Refresh Patterns
Don't trust the origin server's headers blindly. Many older CGIs don't send proper Expires tags. Use refresh_pattern to force caching of static types.
refresh_pattern \.gif$ 1440 20% 10080
refresh_pattern \.jpg$ 1440 20% 10080
refresh_pattern \.js$ 1440 20% 10080
The File Descriptor Limit
By default, many Unix systems limit processes to 256 or 1024 file descriptors. On a busy site, Squid will hit this in minutes. You must increase this at the OS level and recompile Squid with --enable-poll.
ulimit -n 4096
# Then start squid
If you aren't monitoring your hit ratio, you're flying blind. A well-tuned Squid should be hitting 40-60% of requests, taking a massive load off your backend database and application servers.
Aunimeda provides DevOps engineering and infrastructure services - CI/CD pipelines, containerization, cloud deployments, and monitoring setups.
Contact us to discuss your infrastructure needs. See also: DevOps Services, Custom Software Development