<?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>DEV Community: David Backeus</title>
    <description>The latest articles on DEV Community by David Backeus (@dbackeus).</description>
    <link>https://dev.to/dbackeus</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%2F220282%2Fbfc8e6f5-f1a8-4c1c-a4c1-e2f384c657aa.jpeg</url>
      <title>DEV Community: David Backeus</title>
      <link>https://dev.to/dbackeus</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dbackeus"/>
    <language>en</language>
    <item>
      <title>Cut your Rails boot times on Heroku in half with a single command</title>
      <dc:creator>David Backeus</dc:creator>
      <pubDate>Wed, 23 Mar 2022 15:52:07 +0000</pubDate>
      <link>https://dev.to/dbackeus/cut-your-rails-boot-times-on-heroku-in-half-with-a-single-command-514d</link>
      <guid>https://dev.to/dbackeus/cut-your-rails-boot-times-on-heroku-in-half-with-a-single-command-514d</guid>
      <description>&lt;p&gt;TLDR;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;heroku labs:enable build-in-app-dir -a &amp;lt;APP_NAME&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now enjoy your Rails app booting ~twice as fast after your next deploy 🙌&lt;/p&gt;

&lt;h2&gt;
  
  
  But why?
&lt;/h2&gt;

&lt;p&gt;One of the tradeoffs with majestic monoliths is that the larger they grow, the more code has to be loaded and interpreted at boot time. In massive code-bases, such as Shopify's and Github's, booting can take more than a minute (see eg. &lt;a href="https://github.blog/2020-08-25-upgrading-github-to-ruby-2-7/"&gt;Upgrading Github to Ruby 2.7&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Particularly requiring files (eg. &lt;code&gt;require "foo"&lt;/code&gt;) tends to become a bottleneck due to Ruby having to iterate through all possible load paths to lookup matching files. This gets time consuming when it’s applied to thousands of files.&lt;/p&gt;

&lt;p&gt;To alleviate this problem, the engineers at Shopify created &lt;a href="https://github.com/Shopify/bootsnap"&gt;bootsnap&lt;/a&gt;, a gem which automatically detects and caches exact load paths to make those require calls fast (see &lt;a href="https://shopify.engineering/bootsnap-optimizing-ruby-app-boot-time"&gt;Bootsnap: Optimizing Ruby App Boot Time&lt;/a&gt; for details).&lt;/p&gt;

&lt;p&gt;The bootsnap gem has been a default Rails gem since version 5.2 which was released back in 2018 and has been improving boot times in most environments ever since. But what about on Heroku?&lt;/p&gt;

&lt;h2&gt;
  
  
  Bootsnap on Heroku
&lt;/h2&gt;

&lt;p&gt;As it turns out due to a quirk in how Heroku's Ruby buildpack initially builds the application in a &lt;code&gt;/tmp&lt;/code&gt; folder before moving the results over to &lt;code&gt;/app&lt;/code&gt;, the cache generated by bootsnap gets invalidated and can't be used at all. But after enabling the &lt;code&gt;build-in-app-dir&lt;/code&gt; labs feature the entire build process takes place inside of &lt;code&gt;/app&lt;/code&gt; which allows bootsnap to work as intended.&lt;/p&gt;

&lt;p&gt;The feature is enabled via the &lt;a href="https://devcenter.heroku.com/articles/heroku-cli"&gt;heroku CLI&lt;/a&gt; tool:&lt;br&gt;
&lt;code&gt;heroku labs:enable build-in-app-dir -a &amp;lt;APP_NAME&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At &lt;a href="https://mynewsdesk.com"&gt;Mynewsdesk&lt;/a&gt; this reduced the boot times of our 17 year old Rails codebase from ~16 seconds to ~8 seconds. The improvement is particularly noticeable when booting one off dynos via eg. &lt;code&gt;heroku console&lt;/code&gt; or &lt;code&gt;heroku run rails db:migrate&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There is a &lt;a href="https://github.com/heroku/heroku-buildpack-ruby/issues/979"&gt;Github issue&lt;/a&gt; on this topic in the &lt;code&gt;heroku-ruby-buildpack&lt;/code&gt; repo with some success stories in the comment section and a hint that &lt;code&gt;build-in-app-dir&lt;/code&gt; may become the default behaviour in the future.&lt;/p&gt;

&lt;p&gt;So far there have been no reports of issues after enabling this feature but you might want to try this on your staging environments first before enabling it in production to be sure. If any issues do turn up, don't forget to share your experience in the Github issue.&lt;/p&gt;

&lt;p&gt;This is the first post in a planned series of tips and tricks for using Heroku efficiently. Feel free to follow me here on dev.to or &lt;a href="https://twitter.com/dbackeus"&gt;@dbackeus on Twitter&lt;/a&gt; to get notified when new posts are published.&lt;/p&gt;

</description>
      <category>heroku</category>
      <category>rails</category>
      <category>ruby</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
