ScaleScaleScaleScale

Tips / Nginx


Nginx vs Varnish: which one is better?

Today at ScaleScale we will try to give some answers to a long vs challenge: Nginx vs Varnish. Both are wonderful technologies used by web developers who want to speed up their web applications, however both suit for different needs, and on this post we will try to answer some questions and know the lacks and pros of this two different web solutions.

Nginx

Nginx is a high performance web/proxy server that powers the most busiest and heavy traffic websites in the world. It includes caching features for both static and dynamic content, also can be setup as proxy for other web servers like Apache, Lighttpd, Cherokee, etc, and it is 100% compatible with the most popular dev trending technologies like Python, Ruby, NodeJS, PHP, etc.

Nginx powers popular websites Airbnb, Discovery Education, Dropbox, MaxCDN, Netflix, TED, WordPress.com, Zynga and much more.

Varnish

Varnish on the other hand, is an app accelerator, it’s also used as reverse HTTP proxy and includes load balancing capabilities. It uses VCL as the internal language to expand its features according to the needs of the application. VCL allows developers to decide specifically what content, from where and in which way the caching stuff will be served.

Varnish is used by popular sites like Wired, Slideshare, Zappos, SoundCloud, Weather.com, Business Insider, Answers.com, Posterous, among others.

Nginx vs Varnish: a really fair VS battle?

If you are using Nginx and Varnish only as reverse proxy, then the answer is yes, it’s fair to compare both. However, if you are trying to compare a full stack web server like Nginx vs a Cache proxy + Load Balancing like Varnish, then you are thinkig in the wrong way. Think in Nginx as the total solution for all your server needs (web server, static and dynamic cache, load balancing + mail proxy), and Varnish just as another proxy caching mechanism. All you need to clarify here are your needs, do you need a proxy cache solution or a powerful web server that already includes that kind of proxy cache solution?

Static Proxy Caching

Both Nginx and Varnish can be used as reverse proxy cache, also for load balancing between two or more Apache servers that will deliver the dynamic content. Based in our experience, Nginx performs as well as Varnish, some benchmarks show the same thing. While others  show evidence that Nginx is better serving static stuff as proxy cache.

SSL and HTTP/2.0

If you have an e-commerce website that need to encrypt critical data like credit card information, and also personal/finance data from your web users, then you are going to need to purchase and install a SSL certificate.
The good news: Nginx supports SSL, SPDY and HTTP/2.0 by default. However, Varnish does not support SSL with SPDY or HTTP/2.0.

Does it really worth it?

Nginx has a very fast learning curve, and it’s a native Web Server, while Varnish is just a proxy cache layer. Most of the functions that Varnish includes work pretty well with Nginx, as FastCGI cache or Static cache. If you are a web developer or sysadmin looking for a web stack, you will probably start with Nginx + PHP + MySQL and Memcached. Is there really a need to add another technology to your base stack? Does the time really worth to be spent on learning Varnish, while you can keep mastering Nginx features?

Overcaching

We’ve seen developers who believe that adding more caching to their apps is equal to more performance and speed, and most of the time what they get is the opposite, more delay and rountrips until the web server serves the content.
For example, having multiple caching layers that are designed for the same kind of work can lead into more roundtrips than having a one cache type, the perfect showcase are apps that use Cloudflare.

Clouflare is a web acceleration and security company that uses Nginx as proxy to cache your website content and deliver it from the closest location (like a CDN). It doesn’t have any sense to add another caching layer for the static files for example, or a CDN, because Cloudflare already include those features when you use it. The same happens with Varnish and Nginx, if you already have a RAM cache for your dynamic content, then it’s useless to add another caching layer with Varnish for the same task.

Conclusion

As we’ve seen, in some cases, Varnish can be a good alternative to Nginx as Proxy if you use Apache web server without any kind of caching, however the lack of SSL support with SPDY and HTTP/2, the time needed to learn and system resouces it uses, are things that may not be attractive for most developers and sysadmins who look not only for caching, but for SSL performance and low system resources consumption. This is another downside of Varnish: it consumes more CPU and RAM than Nginx, this gets more evident on low-end Cloud servers with tiny hardware.

At ScaleScale we believe that Nginx + FastCGI Cache + TMPFS disk cache or Memcached as alternative, should create a really good performance schema for any kind of Dedicated, Cloud or VPS Servers. What is your experience with Nginx vs Varnish? Do you preffer Nginx as stand alone or just Varnish? Please share your thoughts with us.

Popular search terms:

  • varnish vs nginx
  • nginx vs varnish
  • varnish nginx
  • squid vs nginx
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • Pham Huy Anh

    Thanks for showing that people should NOT cache blindingly

  • Timothy Williams

    Thanks for sharing 🙂

  • Alexey N

    Really good article, thanks for provided information.

  • antoniogarcia78

    From the ignorance. Memcached or Redis?

    • Different use cases entirely. TBH I’d pick one or the other and trade-off, I usually pick redis because it has a disk-backed format, and I’m not aware of how to do the same with memcached.

  • Galeria Panama

    Hi, you recommend Nginx + FastCGI Cache + TMPFS disk cache or Memcached as alternative, for a wordpress website is there any extra performance benefit in adding a cache plugin like WP Fastest Cache, Super Cache, Cachify, etc, to your recommended setup ?

    • Memcached should give you more control over the cached objects than tmpfs.

      If you are already using Nginx Fastcgi cache using a page caching plugins is useless.

      My suggestion: nginx, cdn for static stuff, w3tc with memcached + a good server optimization at kernel / network level.

  • What do you think about. Nginx as the front-end proxy/cache, and apache,mysql, php in backend? I plan to use varnish as the cache , and nginx as the revere proxy for handling the SSL, but I feel like it’s a another redundant step.

  • Thanks! very useful info. great read.

  • bsienn

    This is really nice article. Thanks for sharing