Wednesday, November 18, 2015

useful Linux commands while developing rails web applications

Rails application best optimization concepts

Optimization is MUST of a Rails application specially for the high traffic Ruby On Rails application and for good UX. Here are some optimization tips which I already applied in my recent Rails projects.

Optimization of Rails Application and application server
Optimization of web server
  • Use nginx instead of Apache for better static resources(images,videos,css,javascript,text, html, etc) response
  • For static resources cached on client side for specific time(for example, 30 days), use this configuration
    For Apache, use the Location directive into your virtual host(be sure you have included expires and headers modules)
    Header unset ETag
    FileETag None
    ExpiresActive On
    ExpiresDefault "access plus 30 days"
For nginx location ~ ^/assets/ { expires 30d; add_header Cache-Control public; add_header ETag ""; break; }
view rawgistfile1.txt hosted with ❤ by GitHub
  • Make sure your web server is responding with gzip data
  • If your application faces heavy traffic then use load balancing. In one of my project, I used nginx as balancer on front end and 4 apache web servers on backend nodes. If you use cloud server like rackspace then you can use rackspace's builtin load balancer with your node servers or you can create your own load balancer
  • Use http reverse proxy like varnish cache server so that most of the requests are being served by the cache server instead of hitting your application server, see this link for varnish configuration
  • You can use http cache but be careful, its risky! because already requested requests(from a client) will not hit your application server(because of cache validation time), instead, the client will get response from its local cached data. To avoid this, you can change url.

  • Client Side Optimization
    • Try to use more Asynchronous requests using AJAX for better user experience and responsiveness in your application where you can.
    • Try to reduce number of requests which is the key optimization on client side
    • On production environment, use Rails asset pipeline feature so that your JavaScripts and css files are being combined and minified or compressed
    • Use css stripe to avoid loading more images(same type of images) so that you can reduce number of http requests for images
    • Try to reduce static resources weight(reduce size in KB or Byte)
    • You can use jquery lazy load plugin to load images lazily or you can use jquery appear plugin to load images/contents on when screen appears to view
    • To measure your application's load time and performance, use this google tool
    • For better inspection of your server's resources on browser, use firefox's firebug adons in firefox browser and see how much time all resources are taking to load and see how weight of resources are and see what are the unnecessary http requests and then reduce resources weight and load time and reduce unnecessary http requests as you can.
    Database Optimization
    • Apply database indexing on table's those columns which are being used for join, foreign key, ordering or on where clause
    • Apply data reporting, I mean instead of pulling big data result set on the fly, prepare them before they are being used, you can use any scheduler gem like rufus scheduler or delayed job to make stuff pre-ready.
    • Inspect slow query from your database server log file and optimize it
    You can use NewRelic for details servers logs, graphs, database logs and many many stuff to analyze more.