How to Improve Ruby on Rails Application Performance

How to Improve Ruby on Rails Application Performance

The two significant factors in application performance are size of the database and application structure. Let’s identify the reasons that hinder the applications performance and architectural solutions and techniques to improve applications performance. Reasons that hinder performance of RoR applications:

• Selection of a slow session container

• More use of inefficient helper methods

• Execution of tasks on per request basis instead of calling everything necessary during startup • Reading frequently and too many from database

• Repetition of identical computation while processing requests

If you focus on the problem areas mentioned above, you can easily enhance the performance of Ruby on Rails applications. Better Session container:

Rails framework comes with several built-in session containers. PStore and ActiveRecordStore are two widely used containers. PStore stores session information in a separate file while ActiveRecordStore stores session information in the database. Both session containers slow down action-cached pages, which degrade application performance. You have two better alternatives SQLSessionStore and MemCacheStore. These alternatives improve the performance of action-cached pages leading to faster execution of the code that improves application performance. Reading Cached data:

You should not use class-level caching if you are using same data repeatedly during single process requests. You can simply read the data in the cache, which is much faster and prevents repeated calculations. Request independent computations at startup:

If any data does not change or any change in it requires server startup, you can cache this data in some variable in some class of application. Another option is to disable logging in Ruby on Rails on a per action basis by using silence method. Optimize queries:

Rails have well-defined associations between Model classes. However, the built-in generated accessors are not optimized for performance. The generated accessor methods cause N number of additional queries to the database and thereby degrading the performance of the application. You can resolve this performance issue by adding an: include =>: author to your query parameters, which results in single SQL statement and immediate construction of author objects. Avoiding the use of slow helpers:

All helpers in Rails invoke the routing module to work with URL. The process of invoking routing module and going through several routes in the route file is a time-consuming process. Instead, you can code the piece in HTML directly and avoid the slow helper. Optimizing a database:

Interacting with a database is a time-consuming process and frequent querying the database severely degrades the performance of the RoR application. Reducing the number of SQL queries is one way to improve the performance of RoR application. The other way is optimizing the database, which can be done by following actions:

• Remove unused indexes

• Add appropriate indexes for foreign and private keys and fields used in filtering statements

• Revise and optimize SQL queries

• Partition large tables

• Use stored procedures

• Use eager loading of associations

• Avoid use of transactions when not necessary

These are only some architectural solutions and techniques to improve application performance. Measure the application performance after taking steps to improve it.