Fly’s HTTP proxy can be used to make it so POST etc requests which write to SQLite go via the primary node, however any writes made by the application independent to a HTTP request are not so easily routed to the primary. For example, an application could have a background thread which periodically writes to the database.
Here in the docs it says that LiteFS will create a $LITE_FS_DIR/.primary file when the node is the primary. Is the way to approach this to check this for the existance of this file before writing, to ensure that it is the primary?
Within Fly is there a way to send a HTTP request to the node that is the primary? That could be handy for indirectly performing a write perhaps!
Yes, you can use that to check who the current primary is. We recently added an event stream endpoint to LiteFS (#401) which can also give you live updates about primary change status if you want to maintain that state within your application. The stream just returns newline-delimited JSON events.
You can send HTTP requests to other Fly nodes in your organization using the .internal addresses. By default, the .primary reports the hostname of the machine that is currently primary and this is also its machine ID. You can connect to that machine using:
Hello again! In my tests LiteFS 0.5 never creates the /litefs/.primary file, even though it has logged that it is the primary and is successfully replicating the database to LiteFS Cloud.
The .primary file only exists on replicas. We found that it was easy for users to get in a circular loop if they were primary and tried to redirect based on the .primary file contents to themselves. Maybe it’s worth us adding a .role file or something so the node can check if it is the primary or not.