<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Life Christian</title>
    <description>The latest articles on Forem by Life Christian (@lifelofranco).</description>
    <link>https://forem.com/lifelofranco</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F327034%2F19c4f5e3-f909-487f-a615-9c9bde47259d.jpg</url>
      <title>Forem: Life Christian</title>
      <link>https://forem.com/lifelofranco</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lifelofranco"/>
    <language>en</language>
    <item>
      <title>21 tips for building Rails applications</title>
      <dc:creator>Life Christian</dc:creator>
      <pubDate>Mon, 23 Nov 2020 14:39:45 +0000</pubDate>
      <link>https://forem.com/lifelofranco/21-tips-when-building-rails-applications-5gmk</link>
      <guid>https://forem.com/lifelofranco/21-tips-when-building-rails-applications-5gmk</guid>
      <description>&lt;p&gt;This list is a collection of things I wish I knew when I was starting out as a Ruby on Rails developer. While I won't go in depth, this hopefully gives a good overview of things to look out for and some useful performance tips for anyone building their applications in Rails. &lt;/p&gt;

&lt;p&gt;This is a long list, so better get ready!&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Use &lt;code&gt;includes&lt;/code&gt; and &lt;code&gt;eager_load&lt;/code&gt; to eliminate &lt;code&gt;n + 1&lt;/code&gt; queries.
&lt;/h4&gt;

&lt;p&gt;Every experienced Rails developer should know this but it is still worth mentioning. When querying a list that spans across multiple tables, use &lt;code&gt;includes&lt;/code&gt;. For example, &lt;code&gt;Product.map{ |product| product.brand.name }&lt;/code&gt; gets a list of brand names that would generate SQL statements equal to the number of products in the database. To get around this, simply change it to &lt;code&gt;Product.includes(:brand)&lt;/code&gt;, which would tell Rails to eager load the list of brands first. While the initial query will be heavier, it is best to avoid running multiple SQL statements when you can get all the data that you need in one go.&lt;/p&gt;

&lt;p&gt;Use the &lt;a href="https://github.com/flyerhzm/bullet"&gt;bullet&lt;/a&gt; gem in your development environment to help find those nasty optimization leaks. I highly recommend including this gem at the start of any Rails project to catch these problems early.&lt;/p&gt;

&lt;p&gt;Read more about it here:&lt;br&gt;
&lt;a href="https://scoutapm.com/blog/activerecord-includes-vs-joins-vs-preload-vs-eager_load-when-and-where"&gt;https://scoutapm.com/blog/activerecord-includes-vs-joins-vs-preload-vs-eager_load-when-and-where&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  2. Do batch inserts and updates
&lt;/h4&gt;

&lt;p&gt;Similar to the tip above, do not loop with multiple SQL statements when creating and updating records. I had a use case where we had to insert or update thousands of records every few minutes and doing that inside a loop was just painful.&lt;/p&gt;

&lt;p&gt;If you're on Rails 6, it has added support for doing bulk inserts by introducing these three methods: &lt;code&gt;insert_all&lt;/code&gt;, &lt;code&gt;insert_all!&lt;/code&gt; and &lt;code&gt;upsert_all&lt;/code&gt;. If you're using an older version of Rails, you can also use the &lt;a href="https://github.com/zdennis/activerecord-import"&gt;activerecord-import&lt;/a&gt; gem which serves the same purpose.&lt;/p&gt;
&lt;h4&gt;
  
  
  3. Know the differences of common Ruby methods
&lt;/h4&gt;

&lt;p&gt;While in most cases they won't make or break your application, it is still important to understand the differences of the common Ruby methods. It's the source of confusion for most Rails developers because Rails keeps on changing their behavior. 🤷‍♂️ Thankfully, things have stabilized since Rails 5.1.&lt;/p&gt;
&lt;h5&gt;
  
  
  Present vs Exists vs Any
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;present?&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Loads all the records (if not already loaded) and checks if there is at least one record present.&lt;/li&gt;
&lt;li&gt;This is usually very slow if the record is not yet loaded.&lt;/li&gt;
&lt;li&gt;Produces a &lt;code&gt;SELECT *&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;exists?&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Will always query&lt;/strong&gt; the database to check if at least one record is present.&lt;/li&gt;
&lt;li&gt;This is the quickest for instances when you only need to check for existence, but this can also be the slowest by one whole SQL call if the record is already loaded.&lt;/li&gt;
&lt;li&gt;Produces a &lt;code&gt;SELECT 1 LIMIT 1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;any?&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Produces a &lt;code&gt;SELECT COUNT(*)&lt;/code&gt; in Rails 4.2 and &lt;code&gt;SELECT 1 LIMIT 1&lt;/code&gt; in Rails 5.1 and up.&lt;/li&gt;
&lt;li&gt;Behaves like &lt;code&gt;present?&lt;/code&gt; if records are already loaded.&lt;/li&gt;
&lt;li&gt;Behaves like &lt;code&gt;exists?&lt;/code&gt; if the records aren't loaded.&lt;/li&gt;
&lt;li&gt;Still slower than &lt;code&gt;.exists?&lt;/code&gt; in Rails 5.0 and below. For Rails 5.1 and up, &lt;code&gt;any?&lt;/code&gt; will generate the same query as &lt;code&gt;.exists?&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;
  
  
  Rule of thumb:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;When only checking for existence, always use &lt;code&gt;exists?&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If the record is already loaded or when you would be using the result, use &lt;code&gt;present?&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In Rails 4.2 and below, there is not much of a use case for &lt;code&gt;any?&lt;/code&gt; if you understand when to use &lt;code&gt;exists?&lt;/code&gt; or &lt;code&gt;present?&lt;/code&gt; . However, for newer versions of Rails, it is generally recommended to use &lt;code&gt;any?&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;
  
  
  Blank? vs Empty?
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;blank?&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Negation of &lt;code&gt;present?&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Same rule applies as &lt;code&gt;present?&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;empty?&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Same as &lt;code&gt;any?&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Produces a &lt;code&gt;SELECT COUNT(*)&lt;/code&gt; in Rails 4.2 and and &lt;code&gt;SELECT 1 LIMIT 1&lt;/code&gt; in Rails 5.1 and up.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;
  
  
  Rule of thumb:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;In Rails 4.2 and below, negate the &lt;code&gt;exists?&lt;/code&gt; if you want to check for existence. For newer versions, you can use &lt;code&gt;.empty?&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;As same with &lt;code&gt;present?&lt;/code&gt;, use &lt;code&gt;blank?&lt;/code&gt; only if the record is already loaded.&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;
  
  
  Count vs Size vs Length
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;length&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Much like &lt;code&gt;present?&lt;/code&gt;, loads the association into memory if not yet loaded.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;count&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Will always generate a &lt;code&gt;COUNT&lt;/code&gt; SQL query. Similar behavior with &lt;code&gt;exists?&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;size&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Like any?, behaves like &lt;code&gt;length&lt;/code&gt; if the array is already loaded. Otherwise, it defers to using a &lt;code&gt;COUNT&lt;/code&gt; query.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;
  
  
  Rule of thumb:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;It is generally recommended to use &lt;code&gt;size&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the record is already loaded or when you need to load the association in memory, use &lt;code&gt;length&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Caveat: Since &lt;code&gt;count&lt;/code&gt; will always generate a SQL query, the result might not always be the same with the &lt;code&gt;size&lt;/code&gt; or &lt;code&gt;length&lt;/code&gt;. Best to double check!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Read more about it here:&lt;br&gt;
&lt;a href="https://www.speedshop.co/2019/01/10/three-activerecord-mistakes.html"&gt;https://www.speedshop.co/2019/01/10/three-activerecord-mistakes.html&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  5. Spend time tuning your Puma config
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://github.com/puma/puma"&gt;Puma&lt;/a&gt; is the default web server for rails applications. By default, Puma runs on a single worker process and uses threads to serve multiple requests. Spend some time tuning Puma to run in clustered mode with multiple workers and multiple threads. Every app is different, so do some load testing (remember to do it in a production-like environment) to find the ideal number of workers and threads.&lt;/p&gt;

&lt;p&gt;Most people would recommend using 1 worker per CPU core and while this is a good starting point, do not set this as a hard rule. You can easily go up to three or four times the number of your CPU cores and see significant performance gains. However, this would increase CPU or RAM so have a close look at it during load testing. Rails apps in general are known to consume a lot of memory so make sure you don't run out.&lt;/p&gt;

&lt;p&gt;No one size fits all but aim to have at least &lt;strong&gt;3 workers per server or container with 3 to 5 threads&lt;/strong&gt;. If you cannot have 3 workers because of CPU or memory constraints, consider moving to a more powerful instance or dyno type. You can also learn more about tuning your web server config by listening to this great &lt;a href="https://www.youtube.com/watch?v=itbExaPqNAE"&gt;talk&lt;/a&gt; by &lt;a href="https://www.speedshop.co/"&gt;Nate Berkopec&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Also consider taking a look at  Puma's v5 experimental features! I had great success in using the &lt;code&gt;wait_for_less_busy_worker&lt;/code&gt; and the &lt;code&gt;nakoyoshi_fork&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Read more about it here: &lt;a href="https://www.speedshop.co/2017/10/12/appserver.html"&gt;https://www.speedshop.co/2017/10/12/appserver.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/puma/puma/blob/master/5.0-Upgrade.md"&gt;https://github.com/puma/puma/blob/master/5.0-Upgrade.md&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  6. Use a fast json serializer
&lt;/h4&gt;

&lt;p&gt;I would stay away from using the still popular &lt;a href="https://github.com/rails-api/active_model_serializers"&gt;active_model_serializer&lt;/a&gt; gem. Use &lt;a href="https://github.com/jsonapi-serializer/jsonapi-serializer"&gt;fast_json_api&lt;/a&gt; serializer (originally from Netflix) or something similar. Also use the &lt;a href="https://github.com/ohler55/oj"&gt;oj&lt;/a&gt; gem which offers faster json processing. This can make a significant difference in processing large amounts of JSON.&lt;/p&gt;
&lt;h4&gt;
  
  
  7. Use a CDN in front of your load balancer
&lt;/h4&gt;

&lt;p&gt;If you're using AWS, point your Route53 record to a Cloudfront Distribution that is in front of your Application Load Balancer (ALB). Cloudfront would then optimize traffic from your application to go through the nearest edge location in the AWS global network. Chances are, there is an edge location that is far closer to the user than the ALB. Your mileage may vary, but I saw an 80-100ms latency improvement for simply pointing our DNS to Cloudfront, which then points to the ALB. Take note that I did not yet implement any kind of caching whatsoever.&lt;/p&gt;

&lt;p&gt;Before you do this, make sure to customize Cloudfront's error page to point to your application's error page. You do not want your customers to see an ugly Cloudfront error. Additionally, you can also configure &lt;a href="https://aws.amazon.com/waf"&gt;AWS WAF&lt;/a&gt; to secure your app against malicious attacks.&lt;/p&gt;
&lt;h4&gt;
  
  
  8. Use HTTP caching
&lt;/h4&gt;

&lt;p&gt;Once you have configured having a CDN in front of your ALB, you can do an http cache through the CDN. This will be fast, since everything is cached at the edge. Be warned however, this is a bit complicated to set up and there are a lot of factors to consider. It can also be a source of headaches if you accidentally cache the wrong things. The rewards can be worth it, but proceed with caution.&lt;/p&gt;

&lt;p&gt;Read more about it here:&lt;br&gt;
&lt;a href="https://medium.com/rubyinside/https-medium-com-wintermeyer-caching-in-ruby-on-rails-5-2-d72e1ddf848c"&gt;https://medium.com/rubyinside/https-medium-com-wintermeyer-caching-in-ruby-on-rails-5-2-d72e1ddf848c&lt;/a&gt;&lt;br&gt;
&lt;a href="https://devcenter.heroku.com/articles/http-caching-ruby-rails#conditional-cache-headers"&gt;https://devcenter.heroku.com/articles/http-caching-ruby-rails#conditional-cache-headers&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  9. Use Redis for fragment caching
&lt;/h4&gt;

&lt;p&gt;By default, Rails caches data in the file store. This isn't the best solution if you are running a production site with multiple web servers with each having their own cache store. Thankfully beginning with version 5, Rails has built in support for caching via Redis.&lt;/p&gt;

&lt;p&gt;Redis stores the cache in memory and this does wonders in making any app load faster. By identifying parts of your app that do not change frequently, your site would be able to serve thousands of customers without purchasing additional CPU and memory for both your web servers and your database.&lt;/p&gt;

&lt;p&gt;Also consider using the &lt;a href="https://github.com/redis/hiredis"&gt;hiredis&lt;/a&gt; gem to make Redis load cached content even faster. Additionally, you can also use ElastiCache in production to reduce the burden in managing your Redis cluster.&lt;/p&gt;
&lt;h4&gt;
  
  
  10. Use soft deletes
&lt;/h4&gt;

&lt;p&gt;I would generally advise against doing hard deletes. A hard delete removes the row from the database completely and is final. There is no "undo" unless you keep backups. Just in general, it would be helpful to know when records are deleted. Check out the popular &lt;a href="https://github.com/jhawthorn/discard"&gt;discard&lt;/a&gt; gem that adds an elegant way to do soft deletes.&lt;/p&gt;
&lt;h4&gt;
  
  
  11. Switch to Pagy for pagination
&lt;/h4&gt;

&lt;p&gt;Other popular pagination gems, &lt;a href="https://github.com/kaminari/kaminari"&gt;Kaminari&lt;/a&gt; and &lt;a href="https://github.com/mislav/will_paginate"&gt;will_paginate&lt;/a&gt; consume more memory than Pagy. &lt;a href="https://github.com/ddnexus/pagy"&gt;Pagy&lt;/a&gt; is the new kid on the block and is generally more efficient.&lt;/p&gt;
&lt;h4&gt;
  
  
  12. Use jemmaloc or set MALLOC_ARENA_MAX=2
&lt;/h4&gt;

&lt;p&gt;Set &lt;code&gt;MALLOC_ARENA_MAX=2&lt;/code&gt; and check if the memory consumption of your app improves. I was personally surprised by how this relatively simple change could reduce our app's memory footprint by around 10%.&lt;/p&gt;

&lt;p&gt;You could also try running ruby using &lt;a href="https://github.com/jemalloc/jemalloc"&gt;jemmaloc&lt;/a&gt;, which has been shown to reduce memory consumption. I personally do not have experience in running ruby using jemmaloc, but there are several articles that recommend the switch.&lt;/p&gt;

&lt;p&gt;Read more about it here:&lt;br&gt;
&lt;a href="https://medium.com/rubyinside/how-we-halved-our-memory-consumption-in-rails-with-jemalloc-86afa4e54aa3"&gt;https://medium.com/rubyinside/how-we-halved-our-memory-consumption-in-rails-with-jemalloc-86afa4e54aa3&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.speedshop.co/2017/12/04/malloc-doubles-ruby-memory.html"&gt;https://www.speedshop.co/2017/12/04/malloc-doubles-ruby-memory.html&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  13. Use the database whenever possible
&lt;/h4&gt;

&lt;p&gt;Every good Rails developer should know their way around SQL. After all, ActiveRecord is just an abstraction on top of SQL. Do not use Ruby's &lt;code&gt;.map&lt;/code&gt;, &lt;code&gt;.select&lt;/code&gt; or &lt;code&gt;.group_by&lt;/code&gt; to select data when you can instead use ActiveRecord. SQL was built for querying and filtering data so it is also a lot faster and more efficient than Ruby. Always use the right tool for the job.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# bad&lt;/span&gt;
&lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;state&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"pending"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# good&lt;/span&gt;
&lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;state: &lt;/span&gt;&lt;span class="s2"&gt;"pending"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For complex queries, you may have no choice but to use raw SQL by using &lt;code&gt;ActiveRecord.connection.execute(sql_string)&lt;/code&gt;. While raw SQL will always be faster, you are still better off using ActiveRecord as much as possible since maintaining long lines of SQL is unbearable in the long run. Only use raw SQL when ActiveRecord is preventing you from getting the job done.&lt;/p&gt;

&lt;h4&gt;
  
  
  14. Be careful with Rails migrations
&lt;/h4&gt;

&lt;p&gt;In general, only generate migrations that are additive, and that are backwards compatible with your existing application. Also when adding indexes, always add a &lt;code&gt;disable_ddl_transaction!&lt;/code&gt; together with &lt;code&gt;algorithm: concurrently&lt;/code&gt; to prevent any unexpected downtimes when deploying to production. This is a long topic by itself so take a look at the guidelines of the &lt;a href="https://github.com/ankane/strong_migrations"&gt;strong_migrations&lt;/a&gt; gem. I don't usually put the gem in my app anymore but it would be helpful to read about the general rules to achieve zero downtime migrations.&lt;/p&gt;

&lt;h4&gt;
  
  
  15. Know your indexes
&lt;/h4&gt;

&lt;p&gt;A good rule of thumb is to always index the foreign keys of your tables. You can also use the &lt;a href="https://github.com/plentz/lol_dba"&gt;lol_dba&lt;/a&gt; gem to check if you have indexes missing. Aside from simple B-Tree indexes, be familiar with other types of indexes. My experience is mainly on using Postgres but I imagine it's relatively the same with other SQL engines. Here's a quick run through:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multi-column indexes

&lt;ul&gt;
&lt;li&gt;Can be significantly faster than a single index so this should be considered. However, Postgres can combine two separate indexes together so performance may vary.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;order of the index matters&lt;/strong&gt;. From the &lt;a href="https://www.postgresql.org/docs/13/indexes-bitmap-scans.html"&gt;postgres docs&lt;/a&gt;:&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;...if your workload includes a mix of queries that sometimes involve only column x, sometimes only column y, and sometimes both columns, you might choose to create two separate indexes on x and y, relying on index combination to process the queries that use both columns. You could also create a multicolumn index on (x, y). This index would typically be more efficient than index combination for queries involving both columns, but it would be almost useless for queries involving only y, so it should not be the only index. A combination of the multicolumn index and a separate index on y would serve reasonably well. For queries involving only x, the multicolumn index could be used, though it would be larger and hence slower than an index on x alone.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Partial Indexes

&lt;ul&gt;
&lt;li&gt;An index with a &lt;code&gt;where&lt;/code&gt; clause. &lt;/li&gt;
&lt;li&gt;Can be used to enforce a unique partial constraint.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Example:&lt;/em&gt; Just to illustrate, let's say you want to limit the number of active orders per user. If for some reason, you want to enforce one active order per user at a time, you can easily do this by utilizing a partial unique index.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Ensures one active order per user at a time.&lt;/span&gt;
&lt;span class="c1"&gt;# Adds index concurrently to prevent downtime.&lt;/span&gt;
&lt;span class="n"&gt;disable_ddl_transaction!&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change&lt;/span&gt;
    &lt;span class="n"&gt;add_index&lt;/span&gt; &lt;span class="ss"&gt;:orders&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="sx"&gt;%i[user_id, active]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="n"&gt;where&lt;/span&gt; &lt;span class="s2"&gt;"active = true"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="ss"&gt;unique: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="ss"&gt;algorithm: &lt;/span&gt;&lt;span class="n"&gt;concurrently&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This type of index is also useful when accessing a table with a particular condition that would make the resulting filtered records significantly smaller.

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Example:&lt;/em&gt; You have an orders table with states &lt;code&gt;pending&lt;/code&gt; and &lt;code&gt;complete&lt;/code&gt;. Over time, orders with a complete state would grow since orders are marked as complete everyday. However, our app's queries would naturally be more interested in finding pending orders today. Since there's a huge disparity between thousands of pending orders and potentially millions of complete orders, a partial index would be beneficial to reduce the size of the index and significantly improve performance.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Speeds up the pending orders query&lt;/span&gt;
&lt;span class="n"&gt;disable_ddl_transaction!&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change&lt;/span&gt;
    &lt;span class="n"&gt;add_index&lt;/span&gt; &lt;span class="ss"&gt;:orders&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="ss"&gt;:state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="ss"&gt;where: &lt;/span&gt;&lt;span class="s2"&gt;"state = pending"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="ss"&gt;algorithm: :concurrently&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;GIN Index

&lt;ul&gt;
&lt;li&gt;Commonly used to speed up &lt;code&gt;LIKE&lt;/code&gt; queries. While I would generally advise against using your database to do full text search types of queries, a GIN index can be very helpful when the situation arises.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;disable_ddl_transaction!&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change&lt;/span&gt;
    &lt;span class="n"&gt;enable_extension&lt;/span&gt; &lt;span class="s2"&gt;"btree_gin"&lt;/span&gt; &lt;span class="c1"&gt;# Needs to be enabled&lt;/span&gt;
    &lt;span class="n"&gt;add_index&lt;/span&gt; &lt;span class="ss"&gt;:orders&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
              &lt;span class="ss"&gt;:sample_column&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
              &lt;span class="ss"&gt;using: :gin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
              &lt;span class="ss"&gt;algorithm: :concurrently&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read more about it here:&lt;br&gt;
&lt;a href="https://towardsdatascience.com/how-gin-indices-can-make-your-postgres-queries-15x-faster-af7a195a3fc5"&gt;https://towardsdatascience.com/how-gin-indices-can-make-your-postgres-queries-15x-faster-af7a195a3fc5&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  16. Default values and unique/null constraints
&lt;/h4&gt;

&lt;p&gt;Always think about default values of your database columns. Should it be &lt;code&gt;nil&lt;/code&gt;? Or should it be 0? Being &lt;code&gt;nil&lt;/code&gt; should have a separate meaning from &lt;code&gt;0&lt;/code&gt; or an empty string &lt;code&gt;""&lt;/code&gt; value. If it doesn't matter, then it would probably be best to specify a default value and a &lt;code&gt;not null&lt;/code&gt; constraint. This would save you some headaches later on.&lt;/p&gt;

&lt;p&gt;For booleans, almost always specify a default value and a &lt;code&gt;not null&lt;/code&gt; constraint. Nobody would want three possible values for boolean columns: &lt;code&gt;true&lt;/code&gt;, &lt;code&gt;false&lt;/code&gt;, and &lt;code&gt;nil&lt;/code&gt; 😅&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change&lt;/span&gt;
    &lt;span class="n"&gt;add_column&lt;/span&gt; &lt;span class="ss"&gt;:order&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="ss"&gt;:active&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="ss"&gt;:boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="ss"&gt;null: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="ss"&gt;default: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  17. Use ElasticSearch for your searching needs
&lt;/h4&gt;

&lt;p&gt;With ElasticSearch, having a powerful production ready search engine is very doable nowadays. Use the &lt;a href="https://github.com/elastic/elasticsearch-rails"&gt;elasticsearch_rails&lt;/a&gt; gem in conjunction with &lt;a href="https://github.com/ankane/searchkick"&gt;searchkick&lt;/a&gt; to manage elasticsearch's syntax and quirks. Of course, this is another tool to manage so only consider this if you have complex search use cases. It is not terribly difficult to set up, but it brings another layer of complexity to your app. You may also opt to use a managed service like AWS ElasticSearch to reduce the burden of maintaining the ElasticSearch cluster.&lt;/p&gt;

&lt;p&gt;For something simpler, you can always use the popular &lt;a href="https://github.com/activerecord-hackery/ransack"&gt;ransack&lt;/a&gt; gem. The gem uses the database to do the heavy lifting so only use this for relatively simple searches. Be careful when you want to use pattern matching (&lt;code&gt;LIKE&lt;/code&gt; operator). There was one time we accidentally confused the &lt;code&gt;_eq&lt;/code&gt; ransack predicate from &lt;code&gt;_matches&lt;/code&gt;, making us wonder why our queries were running so slow. 😅&lt;/p&gt;

&lt;h4&gt;
  
  
  18. Consider using count estimates
&lt;/h4&gt;

&lt;p&gt;When counting thousands of records, consider using &lt;a href="https://wiki.postgresql.org/wiki/Count_estimate"&gt;Postgres count estimates&lt;/a&gt; when you do not need an exact count. The count estimate query would be extremely fast and is usually close enough.&lt;/p&gt;

&lt;h4&gt;
  
  
  19. Make sense of your queries by using EXPLAIN AND ANALYZE
&lt;/h4&gt;

&lt;p&gt;Understand how your query performs its actions by using &lt;code&gt;EXPLAIN AND ANALYZE&lt;/code&gt;. Unfortunately, ActiveRecord does not have native support but you can opt to use the &lt;a href="https://github.com/6/activerecord-explain-analyze"&gt;activerecord-explain-analyze&lt;/a&gt; gem. The &lt;code&gt;EXPLAIN AND ANALYZE&lt;/code&gt; result can be daunting to understand with complex queries. Use this nifty &lt;a href="https://explain.depesz.com"&gt;tool&lt;/a&gt; from depesz to make sense of your explain statements.&lt;/p&gt;

&lt;h4&gt;
  
  
  20. Get database insights by running a few queries
&lt;/h4&gt;

&lt;p&gt;There are SQL queries that you can run to have a much better understanding of the current state of your database. Take a look at the different SQL commands in &lt;a href="https://github.com/pawurb/rails-pg-extras"&gt;rails_pg_extras&lt;/a&gt; gem. Run a bunch of rake tasks and gain understanding of your index_usage, locks, outliers, and more.&lt;/p&gt;

&lt;h4&gt;
  
  
  21. Use pg_repack to reduce table/indexes bloat
&lt;/h4&gt;

&lt;p&gt;Do you have a database that has been running for years? Does it feel like queries are significantly getting slower and slower? Is it against a table that has rapid updates and deletes? Run &lt;code&gt;rake pg_extras:bloat&lt;/code&gt; from the &lt;code&gt;rails_pg_extras&lt;/code&gt; gem and check if your tables or indexes are suffering from bloat.&lt;/p&gt;

&lt;p&gt;Bloat grows when performing thousands of updates or deletes to our database. Internally, Postgres does not actually delete a row when we delete a record, it is only marked as unavailable to all future transactions. I am oversimplifying this but essentially, the row still exists on disk but is just no longer visible. You can imagine that this would grow over time, especially for large databases. This is bloat that slows down your queries.&lt;/p&gt;

&lt;p&gt;Running a &lt;code&gt;VACUUM full&lt;/code&gt; against a table will remove bloat completely. However, this command acquires an &lt;code&gt;EXCLUSIVE  LOCK&lt;/code&gt; to your table, which means no reads or writes during the duration of the command. Yikes. This would cause downtime to any production environment. Instead, install &lt;a href="https://github.com/reorg/pg_repack"&gt;pg_repack&lt;/a&gt;, a tool that would allow you to completely eliminate bloat without acquiring an exclusive lock.&lt;/p&gt;

&lt;p&gt;Read more about it here:&lt;br&gt;
&lt;a href="https://medium.com/compass-true-north/dealing-with-significant-postgres-database-bloat-what-are-your-options-a6c1814a03a5"&gt;https://medium.com/compass-true-north/dealing-with-significant-postgres-database-bloat-what-are-your-options-a6c1814a03a5&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;Thats it! That was quite a list, congratulations for reaching the end. I am sure there are still things that I did not cover, but then again there will never be a complete list. Let me know if you have any more tips in the comment section! Also, for anyone who is just starting their web development journey, don't feel overwhelmed! Take things in one at a time and you'll be done before you know it. ✨&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>performance</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
