Preventing Read replica from trying to write litefs

Hi,

I made a mistake with my litefs setup and the noe GET requests were trying to be preformed by the replica instead of the primary. I believe that I needed to add the following to my litefs.yml file:

# This section defines settings for the option HTTP proxy.
# This proxy can handle primary forwarding & replica consistency
# for applications that use a single SQLite database.
proxy:
  addr: ":8080"
  target: "0.0.0.0:8081"
  db: "db.sqlite"
  passthrough:
    - "*.ico"
    - "*.png"

The problem im facing is that the load balancer is always bringing me to the primary region verus the replica region. Is there any way i can force this without taking the primary down?

The load balancer (also called the Fly Proxy) will route you to the closest region. Is your primary the closest to you?

Hey Ben, I am in Vancouver, so I believe the closest region would be sea (Seattle) which is my replica and my primary is in Chicago (ord). I would assume that it would connect me to Seattle but it does not. I was wondering if there was a way to force this just for testing purposes, I can’t submit the app again if it’s going to have the same load balancing errors. Thanks!

You can use the fly-prefer-region when making an HTTP request to your app to have it prefer a given region. Dynamic Request Routing · Fly Docs

Exactly what I needed, thank you Ben!

@benbjohnson I have been able to reproduce the error now by setting the prefer-region.

 sea [info] ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(SqliteFailure(Error { code: ReadOnly, extended_code: 1544 }, Some("attempt to write a readonly database"))), transient: false })}

This may be a dumb question but if the region is preferred, can requests can still go to a sever that is not preferred? In this case, I want to protect against writes being allowed to be performed at all on the server due to the obvious error. Is there a way that I can handle this?

@gilboBaggins You can’t force requests to go to a specific region from the client side, however, you could update your server so that it returns a fly-replay response header and that’ll force the region.

1 Like

I saw this written in WAL Mode in LiteFS on the Fly.io blog:

In the next release, we’ll be focused on making LiteFS easier to integrate into your application by adding support for write forwarding. That will let you write to your database from any node and have LiteFS automatically forward those writes to the primary instead of having your application redirect writes.

Is this possible yet?

It appears this is most definitely working!

I was about to add code to my application, because there are a few rare instances of a write happening during a GET request.

I was expecting the app to break, and got very confused that it was working.

In a replica region, it forwarded without issue, confirmed by doing a select query in litefs cloud on my sqlite data.

Wouldn’t let me edit, actually write forwarding isn’t working, I just messed up and was running in the primary region. noob mistake

From How To to Questions / Help