#rubyonrails
4 posts

How to optimize your website speed by improving the backend

Normalize relational databases at the design stage itself and ensure effective indexing so the indexes don’t slow down your website. In some cases, denormalization is more effective though - where there are many table joins, adding an extra field to one table may be better
Read more

How to optimize your website speed by improving the backend

  • The N+1 query problem slows down many apps when several queries are issued to linked fields in a database. You can use the ActiveRecord ORM tool in Rails that employs eager loading of all associated elements with a single query to help solve this problem.
  • Normalize relational databases at the design stage itself and ensure effective indexing so the indexes don’t slow down your website. In some cases, denormalization is more effective though - where there are many table joins, adding an extra field to one table may be better or adding calculated values you need often to a table can help if you frequently execute complicated calculations.
  • Cache carefully to speed up your site. For SQL caching in Rails, use low-level caching to store query results for a longer time. In general, prefer fragment caching of page blocks for dynamic web apps, use page caching in Rails with the actionpack-page_caching gem, but avoid it if your web has frequently updated content like news feeds. For authentication actions and error messages, use the actionpack-action_caching gem.
  • Use a content delivery network (CDN) of edge servers to cache static content like images, JavaScript, and CSS files for reduced latency across geographies, reduced operational costs compared to handling your own servers, stability and scalability.

Full post here, 11 mins read

Ruby on Rails: Ensuring security is covered in your application

Use strong parameters to accept data being sent to you from a request, supplying whitelisted values to throw an error if incorrect data comes in.
Read more

Ruby on Rails: Ensuring security is covered in your application

  • Set up authentication to verify user access. You can use devise, which uses Bcrypt, to make it difficult for hackers to compute a password. It can also help recover passwords, register and track sign-ins, lock records, etc.
  • Use strong parameters to accept data being sent to you from a request, supplying whitelisted values to throw an error if incorrect data comes in.
  • Add slugs to URLs to identify records in an easy-to-read form without releasing the id of the record.
  • Protect sensitive data, especially logins and payment pages, by enforcing https through the config file and averting cross-site scripting (XSS) attacks.
  • Check for active record exceptions and create an exception concern to sit above the application controller to guard against specific exceptions.

Full post here, 3 mins read

Lessons learned from the Ruby Refactoring Kata - Tennis Game

Refactoring mercilessly is a great learning technique to learn about what different parts of the code do. Don’t trust the initial tests completely. There are great chances they may not give you complete coverage.
Read more

Lessons learned from the Ruby Refactoring Kata - Tennis Game

“There is a certain amount of Zen to refactoring. It is hard at first because you must be able to let go of that perfect design you have envisioned and accept the design that was serendipitously discovered for you by refactoring. You must realize that the design you envisioned was a good guidepost, but is now obsolete.”
  • Refactoring mercilessly is a great learning technique to learn about what different parts of the code do.
  • Don’t trust the initial tests completely. There are great chances they may not give you complete coverage.
  • Extract method is a no-brainer refactoring with a good IDE support.
  • Simplify if conditions with Guards.
  • Preserve the public API if you have no control over client calls.
  • “Code as data” sounds exciting in theory. It isn’t too great in practice.

Full post here, 12 mins read

How to write fast code in Ruby on Rails

Cache all the things. Throttle any operation that can’t be cached. rack-attack and rack-throttle can help throttle unwanted requests. Consciously minimize dependencies. They will turn into liabilities as projects grow.
Read more

How to write fast code in Ruby on Rails

  1. In Rails
  • Cache all the things.
  • Throttle any operation that can’t be cached. rack-attack and rack-throttle can help throttle unwanted requests.
  • Consciously minimize dependencies. They will turn into liabilities as projects grow.

2. In Ruby

  • Use metaprogramming sparingly to prevent unnecessary slowness.
  • Think about how your code will scale with more data. Know the difference between O(n) and O(1).
  • Avoid mutating global state while leveraging mutation on the local state.

3. In Active Record (Rails’ default ORM)

  • Know when queries get executed and what causes them to get evaluated.
  • Index the columns you need to query.
  • Use select and pluck to select only what you need. By default, Active Record selects all columns in SQL with SELECT *

Full post here, 8 mins read