<?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: Queenie Peng</title>
    <description>The latest articles on DEV Community by Queenie Peng (@queeniepeng).</description>
    <link>https://dev.to/queeniepeng</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%2F1111106%2F853624e4-2e5a-4e9b-8df0-e8b3b20e5dfe.jpeg</url>
      <title>DEV Community: Queenie Peng</title>
      <link>https://dev.to/queeniepeng</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/queeniepeng"/>
    <language>en</language>
    <item>
      <title>How to reduce memory usage for your Rails app - R14 - Memory Quota Exceeded in Ruby (MRI)</title>
      <dc:creator>Queenie Peng</dc:creator>
      <pubDate>Fri, 30 Jun 2023 16:56:07 +0000</pubDate>
      <link>https://dev.to/queeniepeng/how-to-reduce-memory-usage-for-your-rails-app-r14-memory-quota-exceeded-in-ruby-mri-14gg</link>
      <guid>https://dev.to/queeniepeng/how-to-reduce-memory-usage-for-your-rails-app-r14-memory-quota-exceeded-in-ruby-mri-14gg</guid>
      <description>&lt;p&gt;If you have just started building your Rails app and discovered that your memory quota is reaching nearly 1 GB - that's not normal. Fortunately, I have the exact solution for you. I've successfully brought my Rails app -&lt;a href="https://receipt-AI.com"&gt;https://receipt-AI.com&lt;/a&gt; 's memory usage from 1 GB to 268 MB.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Lower your worker count&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The default Puma worker pool size is 4, but you can reduce it if you are not using a lot of concurrent connections.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku config:set WEB_CONCURRENCY=1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2. Remove unused gems and set some to require: false&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;It could be that the gems you used are not required for boot time. To determine how much memory your gems consume during boot time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;group :development do
    gem 'derailed'
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then run bundle install and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bundle exec derailed bundle:mem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bear in mind that if any gems is set to &lt;code&gt;require: false&lt;/code&gt;, make sure to add require to the file where it is used.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;3. Use rack-mini-profiler to monitor your database queries.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Uncomment this gem in your Gemfile and run bundle install. This tool allows you to measure the speed of your HTML page. It's super straightforward.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
  # gem "rack-mini-profiler"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;4. Caching&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Rails has a built-in way to cache your view, as simple as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;%= render partial: 'hero', cached: true %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should only use caching on partials that are used frequently and whose output does not change frequently, such as your home page, which is also the most visited.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;5. Garbage collector turning&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;I have read multiple sources that state the default value of RUBY_GC_HEAP_GROWTH_FACTOR in Ruby is 1.8. Some sources mention that it may vary depending on the Ruby version. Nevertheless, you can configure the value by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku config:set RUBY_GC_HEAP_GROWTH_FACTOR=1.03
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this approach, your Rails app will be running super crisp and smooth without the need to upgrade the dyno.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;




&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Ruby memory use&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://devcenter.heroku.com/articles/ruby-memory-use"&gt;https://devcenter.heroku.com/articles/ruby-memory-use&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Garbage collection tuning&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://blog.appsignal.com/2021/11/17/practical-garbage-collection-tuning-in-ruby.html"&gt;https://blog.appsignal.com/2021/11/17/practical-garbage-collection-tuning-in-ruby.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>memory</category>
      <category>heroku</category>
    </item>
    <item>
      <title>How to switch hosting from Render to Heroku for your Ruby on Rails application</title>
      <dc:creator>Queenie Peng</dc:creator>
      <pubDate>Fri, 30 Jun 2023 16:54:00 +0000</pubDate>
      <link>https://dev.to/queeniepeng/how-to-switch-hosting-from-render-to-heroku-for-your-ruby-on-rails-application-2oof</link>
      <guid>https://dev.to/queeniepeng/how-to-switch-hosting-from-render-to-heroku-for-your-ruby-on-rails-application-2oof</guid>
      <description>&lt;p&gt;We were trying out the new hosting tool, Render, for &lt;a href="https://receipt-ai.com"&gt;Receipt-AI.com&lt;/a&gt;, and we ended up regretting it. Even though we were paying for their starter plan, we experienced numerous disconnection issues. After spending two days dealing with these problems, I decided to switch back to Heroku for my Rails app. Let me share with you the steps I took to make the transition.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 1. Download your Postgres Database from Render to your local machine&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The process of downloading and uploading a Postgres database is called 'pg_dump' and 'pg_restore'. Since we need to migrate from Render to Heroku, the first step is to connect to Render and download the database.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;A. Add your local machine's ip address on Render.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Make sure your VPN is turned off and add the correct IP address. This will enable you to connect to your database on Render from your local machine.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;B. Add &lt;code&gt;-Fc&lt;/code&gt; for correct compression format.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Run &lt;code&gt;pg_dump -Fc -d postgres://{YOUR_POSTGRESS_URL}&lt;/code&gt; on your local machine to download the database.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-Fc&lt;/code&gt; is the format for Heroku. If a backup is created with the incorrect format, &lt;code&gt;pg_restore&lt;/code&gt; will not be able to restore the dump file. You may see an error like this: &lt;code&gt;pg_restore: [archiver] did not find magic string in file header.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To run &lt;code&gt;pg_dump&lt;/code&gt; from your local machine, install 'pg_dump' using homebrew. Remember the version of &lt;code&gt;pg_dump&lt;/code&gt; has to be the same as Render's. Otherwise you will see this error &lt;code&gt;pg_dump: error: aborting because of server version mismatch.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Some additional details and instructions are available here. &lt;a href="https://devcenter.heroku.com/articles/heroku-postgres-import-export#create-dump-file"&gt;https://devcenter.heroku.com/articles/heroku-postgres-import-export#create-dump-file&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;C. Upload your database to a cloud bucket.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Heroku doesn't allow database uploads from a local machine, so you need to store the database in a publicly accessible bucket in the cloud. Both Google Cloud and AWS are suitable options. Remember to delete it after restoring the database.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 2. Upload your Postgre Database to Heroku from a cloud bucket.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Assuming you have already created an account on Heroku and completed all the necessary preparations to run your app, you can now proceed by running this command to restore your database.&lt;code&gt;heroku pg:backups:restore '&amp;lt;SIGNED URL&amp;gt;' DATABASE_URL --app example-app&lt;/code&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 3. SSL certificate&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If your app is already running on Basic or Professional dynos, enable &lt;code&gt;Automated Certificate Management&lt;/code&gt; with the following command:&lt;br&gt;
&lt;code&gt;heroku certs:auto:enable&lt;/code&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 4. Redis&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;A1: If you are using &lt;code&gt;heroku data for redis&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;It's a way easier setup. Starting plan costs $3/month.&lt;br&gt;
Source: &lt;a href="https://devcenter.heroku.com/articles/heroku-redis"&gt;https://devcenter.heroku.com/articles/heroku-redis&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;or&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;A2: If you want to use &lt;code&gt;Redis Enterprise Cloud&lt;/code&gt; to save $3/month:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Set the &lt;code&gt;REDIS_PROVIDER&lt;/code&gt; env var to the name of the env var containing the Redis server URL. (Example with RedisCloud: set REDIS_PROVIDER=REDISCLOUD_URL and Sidekiq will use the value of the REDISCLOUD_URL env var when connecting to Redis.)&lt;/p&gt;

&lt;p&gt;Run this command: &lt;code&gt;heroku config:set REDIS_PROVIDER=REDISCLOUD_URL&lt;/code&gt;&lt;br&gt;
Source: &lt;a href="https://github.com/sidekiq/sidekiq/wiki/Using-Redis"&gt;https://github.com/sidekiq/sidekiq/wiki/Using-Redis&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may see this error if you are running Sidekiq 7+&lt;br&gt;
&lt;code&gt;redis-client requires Redis 6+ with HELLO command available&lt;/code&gt; You have to downgrade Sidekiq to 6.5.8 version for it to work.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;B: Add these lines to your procfile&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;worker: bundle exec sidekiq -C config/sidekiq.yml
release: bin/rails db:migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You are almost there:&lt;/p&gt;

&lt;p&gt;Copy all your environment variables from Render to Heroku, ensure everything is up and running, and confirm that your Sidekiq jobs are executing. Once you have verified these aspects, you can safely suspend the usage of Render. You may also delete all the YAML files that were generated by Render from your repository.&lt;/p&gt;

&lt;p&gt;🎉 Congratulations! You have successfully migrated from Render to Heroku. Woo wee! I hope this guide proves helpful to you, and feel free to share it with anyone who may encounter similar challenges. Also, don't forget to check out &lt;a href="https://receipt-ai.com"&gt;https://receipt-ai.com&lt;/a&gt; —it's up and running.&lt;/p&gt;

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