Start a Project

Comparing Varnish and Nginx as reverse proxy for Magento 2 store

When you are running a e-commerce website like Magento a lot of customers  visit your website. It is necessary to deliver contents to the users with minimum time. If your site is taking too much time to load ,you can lose a lot of customers even if the delay is for seconds. So it is necessary to improve the performance of your website by increasing it’s speed. The performance of the site also decreases when there is too much traffic on the server. So to optimise the website you must increase the speed of the website even if there is heavy traffic on the site.

How to increase the speed of website ?

To increase the speed of website you can serve static contents to clients. Static contents take less time to load so your webpage loads very fast. To store static contents you can use caching technique. In caching you can store frequently used data. Whenever someone asks for the data you can serve the data from the cache, not from the main server. There are different techniques to achieve caching. You can use Varnish or Nginx as a reverse proxy for caching content. For caching a database server you can use Redis server. Varnish and Nginx can also handle heavy traffic.

Nginx

Nginx is a free,open source,high performance HTTP server  and  reverse proxy server. Nginx can also act as a load balancer and caching server.

Nginx can proxy requests to servers that communicate using the http(s), FastCGI, SCGI, and uwsgi, or memcached protocols through separate sets of directives for each type of proxy. Whenever a user request some data nginx query the cache for data. If data is not available in cache it bypass the connection to the main backend server. The main advantage of Nginx is that it uses little resources while serving thousands of concurrent connections. Nginx does not create new thread for new connection.A single thread can serve thousand of connections. So a new user request does not have to wait. Nginx is well suited for load balancing.

Varnish

Varnish Cache is a http accelerator which increases the speed of the site. Varnish is set up in front of a http web server to cache the contents. By using varnish we can greatly improve the speed and performance of the website.

Whenever a user request some content varnish fetch it from the backend server and serves it to the user. It also saves the content in the cache. So next time other user requests the same content it fetches the content from cache.It does not request the backend for the content. In this way user get the content without making connection to the main backend server. Varnish can serve thousand of concurrent users at same time while serving the cache content. Varnish prevents increasing the load at the main backend server.

Redis

Redis is an open-source key value store that can operate as both an in-memory store and as cache. Redis is a data structure server that can be used as a database server on its own, or paired with a relational database like MySQL to speed things up. LIST,QUEUE,SETS,HASHES are used to store data. SQL like queries can not be performed on Redis.So Redis is a good choice for caching.

When first time page is loaded, a database is queried on the server. Redis caches the query. Next time other user loads the page the results are provided from the redis without quering the actual database. Redis implements a persistent object cache (no expiration). An object cache works by caching the SQL queries in memory which are needed to load the web page. When the data of main database server is updated,then corresponding key in th redis is invalidated. So the redis provides the updated data instead of caching the data.

We installed magento-2.1.0 on 3 different servers and compared the results.

The server configurations are as follows.

Os : Ubuntu 14.04 LTS
RAM : 3 Gb

1. Apache2 + Magento-2.1.0
2. Varnish + Apache2 + Redis-server + Magento-2.1.0
3. Nginx + Apache2 + Redis-server + Magento-2.1.0

programs            version
Apache2              2.4.7
Redis-server       2.8.4
Varnish               4.0.3
Nginx                  1.4.6
Mysql server      5.6.31
php                      7.0.9

We used siege tool for stress testing. We took 100, 500 and 1000 concurrent users and tested on 3 different urls for 30 seconds.

http://magento.example.com/category-10/category-11/category-12.html?product_limit=30

When you query some static pages like html Varnish perform much faster than the Nginx because of full page cache feature. Full Page Cache is not available in Magento 1.9 . With Varnish Magento 2 performs very fast even if there are 1000 concurrent users. As you can see Varnish makes a huge difference while serving the static content.

http://magento.example.com/checkout/cart

When you deal with authentication and session variables Nginx is better than Varnish. Varnish does not store cookie and session variables.So  Varnish could not supply cache variables. It asks backend for the data which causes to increase in response time.

http://magento.example.come/catalogsearch/result/?q=+product

As Varnish does not store dynamic data, Nginx works faster than the varnish. So if you hit the url for searching some contents which changes time to time Varnish can not store this data in the cache.

From these results you can see that Varnish performs better while serving static contents. If your website is consist of most of static contents then Varnish is the best choice for increasing the performance of your website. Varnish can also serve static content faster when there is high traffic on the website. But in case of serving dynamic content Varnish is not a good choice because Varnish fetches the content from the backend server every time someone requests a content. So if your website uses most of the time authentication, cookie and session variables Nginx is the best choice. Nginx can deal with cookies and session variables more effectively than Varnish. Varnish also lacks SSL support.

Next blog I will post  how to configure Varnish and Redis with Magento 2.

 

IN CASE OF ANY QUERY, CONTACT US

Exit mobile version