How to Make Serverless Containers Hosted on Railway Scale to Zero

Date: 2024-09-27 | create | tech | serverless-containers | hosting | cloud | railway |

I build a lot of projects but a majority of them get very little traffic. This means that keeping them online all the time is wasteful as they aren't needed - just burning a hole in my pocket.

In this post we'll explore how to make serverless containers hosted on Railway (referral) scale to zero so you don't pay for them when they're not being used.

Railway Scale-to-Zero

Railway calls it's scale-to-zero feature "App Sleeping". (official Railway Docs)

How it works:

  • If an app doesn't send outbound data for 10 minutes -> goes to sleep
  • If a sleeping app receives traffic -> wakes up

When an app is asleep you only pay for its storage (for the image, for any persistent stores) not its compute (cause it's not computing anything).

Caveats with App Sleeping:

  • Waking up takes awhile - expect a slow "cold boot" time as your app goes through its full startup process when waking up
  • Your app is deprioritized - In remote cases this will cause it to fail to wakeup (I've not experienced this in 3 months using Railway)

For my workloads this makes a lot of sense cause none of my apps are critical so slow wake up times / possibility of not waking up is okay for me.

How to setup Railway App Sleeping

To setup App Sleeping:

  • Go to your project's Architecture page
  • Select the App to change
  • Go to Settings
  • Scroll down to App Sleeping
  • Toggle Enable App Sleeping
  • Deploy new revision for the changes to take effect

Next

I've been using Railway for the past few months and been pretty happy with it - I'm currently hosting 5 apps there. It's pretty simple and not the cheapest but it makes a lot of my hosting scenarios much simpler so that's a win for me.

For hosting I prefer serverless containers because it allows me to build the same container locally in dev that I deploy in prod. I use CloudSeed (my F# project boilerplate) to give me a headstart - it includes a containerized fullstack F# app ready for deployment.

If you liked this post you might also like:

Want more like this?

The best / easiest way to support my work is by subscribing for future updates and sharing with your network.