Cheaply running ruby on rails apps (for side projects)

alexspark

Alex Park

Posted on January 2, 2023

Cheaply running ruby on rails apps (for side projects)

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.

Here's the gist of it:

  • Rails.cache uses :file_store
  • Use SQLite for a database
  • Use redis for sidekiq and actioncable
  • Run Sidekiq in embedded mode
  • Use disk_storage for ActiveStorage
  • Run ActionCable's websocket server in embedded mode
  • Use hatchbox to deploy all of my apps on a single VPS on linode/vultr/aws/digital ocean
  • Use Cloudflare as a CDN

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

cost = ($10 per server + VPS cost) / # of apps
Enter fullscreen mode Exit fullscreen mode

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.

Caveats

This stack does make some assumptions about the app ideas I have:

  • a lot of reads and very few writes
  • very small chance of going viral
  • low to medium traffic
  • wary of every gem you use to manage memory usage

And this stack does nudge coding your app a certain way to squeeze as many apps as possible in a single server:

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

Possible further optimizations

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 here. 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.

If anyone is interested in a tutorial, let me know.
Thanks for reading!

I am partially running this stack (sqlite and file_store caching) in production today at reipricetracker. If you are concerned about performance, you can see the top left speed badge be adding ?rails=1 to any URL.

💖 💪 🙅 🚩
alexspark
Alex Park

Posted on January 2, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Local snapshots
code Local snapshots

September 22, 2023

ActiveRecord adapter improvements
code ActiveRecord adapter improvements

September 22, 2023

Array columns in SQLite
code Array columns in SQLite

September 12, 2023

Optimizing SQLite compilation
code Optimizing SQLite compilation

September 10, 2023

Setting up Litestream
code Setting up Litestream

September 9, 2023