Find your way in the Rails optimization jungle

by Andreas Knöpfle almost 3 years ago
Wan't to optimize your Ruby on Rails application and don't know where to start? This should give you some hints to help you out

Today I have something special for you. I know programmers love diagramms so I made one about how to tune your Ruby on Rails application if you encounter performance problems.

It won't cover anything I guess, but it should cover some basic questions you have to ask yourself and steps to get your app running smothly. In the diagramm i placed some numbers that point to longer explanations if you need any. Additionally I plan on making special blog posts for some of the numbers to help you out. You can click on the thumbnail above if you want a larger version of the diagramm.

  1. Use tools to find out where your bottlenecks are. ruby-prof and new_relic are tools I use to find out where my apps consume to much memory. 
  2. ActiveRecord tends to bloat your Memory by allocating to much objects. Sometimes you don't need that much. Refer to my blog post or to this source to get some help.
  3. Is your stack exploding because you mess with recursive functions or do you allocate objects that you don't need. Watch out how long you objects live ( ruby-prof ) and tune your garbage collection.
  4. Key-Value-Stores like redis are very fast and can help you saving memory by reducing the amount of objects that you need to keep there. Serialize and deserialize your objects to a store like this.
  5. Background Processing can help you when your app-servers are to busy or if requests just do to much things. With an asynchron design and queing you can work off a great bunch of things. Just be careful that you keep your jobs small. I normally use a spawner job that creates a many small jobs that do the work. Go for resque or sidekiq
  6. Use a multithreaded server, that your requests can run in parallel. Passenger or Puma can help you out.
  7. Just look at the awesome rails docs.
  8. Use rails update_all and delete_all helpers to update or delete a bunch of records with SQL without getting them to memory first. Careful this won't trigger observers.
  9. A caching-system is optimized for the task. Rails uses a file based cache per default which is way slower than using memcached with dalli for example.
  10. Slim is faster than Haml if you believe performance benchmarks. ERB is even faster.
  11. Index servers can help you building better queries and even proper searching. Counting of records is way faster there than in usual RDBMS Systems because they don't use transaction concepts.
  12. Endless scrolling prevents you from calculating the maximum number of pages, which is a reason for slow pagination for millions of records in RDBMS Systems.
  13. Pagination with Kaminari or will_paginate.
  14. Turbolinks or wiselinks make all your pages load faster without much efford. They use ajax queries to beat normal browser loading. You can even implement small parts yourself (for example if you display information from an external slow service like RSS).
  15. Both SASS and LESS offer you support for bootstrap mixins. If you use them you can implement your views way cleaner and spare load time.
  16. Try to avoid redundant HTML containers and use a clear HTML markup. This will reduce your CSS and HTML code and will make your pages load faster.
  17. I can't help you here ... I you find more mail me and I will add this to this image.
  18. Precompiling assets should be on in production, espacially if you use things like SASS or coffescript.
Back

Announcing treasures.io

Discover the internet of islands and sail through the online ocean.


asdf Version Manager & Postgres UUID

Compiling Postgres with UUID to use in asdf


Lombok Impressions

Our trip to the Indonesian island Lombok in October 2015


Deploying Phoenix app with dokku-alt and Dockerfile

My experiences in deploying a Elixir Phoenix app on my dokku-alt instance.


Moved to netcup and dokku-alt

How i moved this page to run on netcups vhosts with dokku-alt Docker instances