How can I further improve the speed of the app in other region?

Hi, I have been experimenting on deploying my Rails app running on Sqlite3 (app database and background job with solid queue) + redis (cache and action cable). The goal is when my app starts to grow, I can easily deploy to a new region closest to my users so that they experience the same snappy app as the one near to my primary region. Fly seems like a good bet to achieve this.

So far, I have managed to deploy my Rails app on Fly with litefs. The primary region is in hkg (I am based in Malaysia)

Here are the page speed of the response when hitting the #new endpoint of one of my controller:

  1. Sending a request from Malaysia (without VPN): 81ms

  2. Sending a request with VPN connected to Seattle with only primary region in hkg: 395ms

  3. Sending a request with VPN connected to Seattle with additional region in sea: 241ms

So there’s about 150ms improvement with an additional region in sea, but I am wondering is it possible of it to also achieve sub 100ms response?

Appreciate any suggestions. Thank you

Hi… In a typical LiteFS setup, mutations are redirected all the way back to the primary,
which is the only node that can write.

You should see more improvement with pages that are read-only.

Hope this helps!

Thanks for the reply, the endpoint I am testing doesn’t perform any write so I was wondering if I missed anything that might improve the speed more.

I am a little confuse with how to setup the proxy and having issue where PUT request when performed in non-primary region failed as it wasn’t redirected back to the primary. My understanding of the doc is that the proxy should handle it already, so maybe I am setting it wrong.

  # Bind address for the proxy to listen on.
  addr: ":8080"

  # Hostport of your application.
  target: "localhost:8081"

  # Filename of the SQLite database you want to use for TXID tracking.
  db: "my.db"

You’ll also need to ensure that your application is running on the target port, which is 8081 in this example. Additionally, make sure your internal port is configured to the proxy’s bind port in your fly.toml:

  internal_port = 8080
  protocol = "tcp"

Here’s my setup:

I am assuming now the 3000 is the hostport of my application

# fly.toml
  internal_port = 3000
  force_https = true
  auto_stop_machines = false
  auto_start_machines = true
  min_machines_running = 0
  processes = ['app']

Then in my litefs.yml, I set the target to localhost:3000.

  # Specifies the bind address of the proxy server.
  addr: ":8080"

  # The hostport of the target application.
  target: "localhost:3000"

  # The name of the database used for TXID tracking.
  db: "production.sqlite3"

My question will be, where should I set the bind address of the proxy port 8080 in my fly.toml? Do I need to add the [[services]] as per the doc? Or I need to change my [https_service]'s port?

Thank you.

Set that as internal_port, otherwise the LiteFS proxy is being bypassed completely.

The chain should be Fly edge proxy → LiteFS proxy → Rails. The fly.toml settings forge the first of those links, and litefs.yml does the second.

[http_service] is a simplified way of doing [[services]]; it basically saves you the inconvenience of typing port = 80, etc. The internal_port in the documentation that you quoted is the same knob, just with different syntax overall.

  internal_port = 8080  # LiteFS proxy will forward to Rails
  force_https = true
  auto_stop_machines  = false
  auto_start_machines = false

The terminology of LiteFS takes a little getting used to initially, but it does make sense in the end…

Thank you so much for the reply. Yeah, I am trying to get my ahead around litefs and your reply is definitely helping me.

So I tried setting the internal_port to 8080 but I am seeing these warnings and the site won’t load. Do you know what actually went wrong, cause from the doc I thought I should set it to 8080 too.

Setting the internal_port to 3000 works, but I also need to update my app to rescue from error when trying to PUT or POST to other region and forward the request to the primary using fly-replay.

Hm… It looks like the LiteFS proxy may not actually be running…

Try checking fly logs; mine have a line reading:

proxy server listening on: http://localhost:8080

Ahh you are right! So I have some error with the exec cmd and the proxy server closed due to that.

Once that’s fixed, everything works correctly, I no longer need to have a custom code to rescue and redirect to primary region for PUT and POST.

Once again, thank you so much and wishing you a great day!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.