<?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: Alex Park</title>
    <description>The latest articles on DEV Community by Alex Park (@alexspark).</description>
    <link>https://dev.to/alexspark</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%2F1000590%2Fbd4eb31e-742f-490a-8361-0521e6402124.png</url>
      <title>DEV Community: Alex Park</title>
      <link>https://dev.to/alexspark</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alexspark"/>
    <language>en</language>
    <item>
      <title>Cheaply running ruby on rails apps (for side projects)</title>
      <dc:creator>Alex Park</dc:creator>
      <pubDate>Mon, 02 Jan 2023 22:08:13 +0000</pubDate>
      <link>https://dev.to/alexspark/cheaply-running-ruby-on-rails-apps-for-side-projects-4na6</link>
      <guid>https://dev.to/alexspark/cheaply-running-ruby-on-rails-apps-for-side-projects-4na6</guid>
      <description>&lt;p&gt;With Heroku's free tier gone, this is my work-in-progress configuration for a new ruby on rails apps that I want to run as cheaply as possible.&lt;/p&gt;

&lt;p&gt;Here's the gist of it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rails.cache uses &lt;code&gt;:file_store&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Use SQLite for a database&lt;/li&gt;
&lt;li&gt;Use redis for sidekiq and actioncable&lt;/li&gt;
&lt;li&gt;Run Sidekiq in embedded mode&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;disk_storage&lt;/code&gt; for ActiveStorage&lt;/li&gt;
&lt;li&gt;Run ActionCable's websocket server in embedded mode&lt;/li&gt;
&lt;li&gt;Use &lt;a href="https://hatchbox.io" rel="noopener noreferrer"&gt;hatchbox&lt;/a&gt; to deploy all of my apps on a single VPS on linode/vultr/aws/digital ocean&lt;/li&gt;
&lt;li&gt;Use Cloudflare as a CDN&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The main theme here is taking advantage of how cheap disk storage is on VPS providers (~$0.10 per &lt;strong&gt;GB&lt;/strong&gt;) as well as Hatchbox allowing unlimited apps to be deployed on a single machine. The cost per months follows this formula:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cost = ($10 per server + VPS cost) / # of apps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So if I had 10 apps running a single $5 Linode server and each app would cost me $1.50 per month. 100 apps on a $40 server would cost my $50 / 100 apps = $0.50 per app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Caveats
&lt;/h2&gt;

&lt;p&gt;This stack does make some assumptions about the app ideas I have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a lot of reads and very few writes&lt;/li&gt;
&lt;li&gt;very small chance of going viral&lt;/li&gt;
&lt;li&gt;low to medium traffic&lt;/li&gt;
&lt;li&gt;wary of every gem you use to manage memory usage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And this stack does nudge coding your app a certain way to squeeze as many apps as possible in a single server:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;heavily use russian doll caching&lt;/li&gt;
&lt;li&gt;SQLite should be running in WAL mode&lt;/li&gt;
&lt;li&gt;Sidekiq workers should not do anything that requires a lot of compute (adding things together, reading files, etc).&lt;/li&gt;
&lt;li&gt;Remove cookies from page responses so that they can be cached in a CDN and use lazy turbo frames to fetch user specific content.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Possible further optimizations
&lt;/h2&gt;

&lt;p&gt;This same configuration could be done on fly.io instances in addition to scaling down to 0 after hitting idle timeout. I learned about this &lt;a href="https://benjohnson.ca/2022/11/20/scale-to-zero.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Although I don't have a lot of experience optimizing rails boot times in order to respond to a waking request fast enough. Given what has happened to heroku, I'd bet that a simple VPS provider would outlast a niche platform-as-a-service.&lt;/p&gt;

&lt;p&gt;If anyone is interested in a tutorial, let me know.&lt;br&gt;
Thanks for reading!&lt;/p&gt;

&lt;p&gt;I am partially running this stack (sqlite and file_store caching) in production today at &lt;a href="https://reipricetracker.com" rel="noopener noreferrer"&gt;reipricetracker&lt;/a&gt;. If you are concerned about performance, you can see the top left speed badge be adding &lt;code&gt;?rails=1&lt;/code&gt; to any URL.&lt;/p&gt;

</description>
      <category>devmeme</category>
    </item>
  </channel>
</rss>
