├── .dockerignore ├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── cmd └── litefs-example │ ├── index.tmpl │ ├── main.go │ └── schema.sql ├── docker-compose.yml ├── docker-config ├── README.md ├── etc │ └── litefs.static-lease.yml └── nginx │ ├── Dockerfile │ └── nginx.conf ├── fly-io-config ├── README.md └── etc │ └── litefs.yml ├── go.mod └── go.sum /.dockerignore: -------------------------------------------------------------------------------- 1 | # flyctl launch added from .gitignore 2 | **/fly.toml 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | fly.toml 2 | *~ 3 | *# 4 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Build our application using a Go builder. 2 | FROM golang:1.20 AS builder 3 | 4 | WORKDIR /src/litefs-example 5 | COPY . . 6 | RUN go build -buildvcs=false -ldflags "-s -w -extldflags '-static'" -tags osusergo,netgo -o /usr/local/bin/litefs-example ./cmd/litefs-example 7 | 8 | 9 | # Our final Docker image stage starts here. 10 | FROM alpine 11 | ARG LITEFS_CONFIG=litefs.yml 12 | 13 | # Copy binaries from the previous build stages. 14 | COPY --from=flyio/litefs:0.5 /usr/local/bin/litefs /usr/local/bin/litefs 15 | COPY --from=builder /usr/local/bin/litefs-example /usr/local/bin/litefs-example 16 | 17 | # Copy the possible LiteFS configurations. 18 | ADD fly-io-config/etc/litefs.yml /tmp/litefs.yml 19 | ADD docker-config/etc/litefs.static-lease.yml /tmp/litefs.static-lease.yml 20 | 21 | # Move the appropriate LiteFS config file to /etc/ (this one will be 22 | # used by LiteFS). By default this is the config file used on Fly.io, 23 | # but it's set appropriately to other files for the docker setup in 24 | # docker-compose.yml 25 | RUN cp /tmp/$LITEFS_CONFIG /etc/litefs.yml 26 | 27 | # Setup our environment to include FUSE & SQLite. We install ca-certificates 28 | # so we can communicate with the Consul server over HTTPS. cURL is added so 29 | # we can call our HTTP endpoints for debugging. 30 | RUN apk add bash fuse3 sqlite ca-certificates curl 31 | 32 | # Run LiteFS as the entrypoint. After it has connected and sync'd with the 33 | # cluster, it will run the commands listed in the "exec" field of the config. 34 | ENTRYPOINT litefs mount 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | LiteFS Example Application 2 | ========================== 3 | 4 | This repository is an example of a toy application running on LiteFS. You can 5 | test it out by deploying to Fly.io, or locally with a docker-compose setup. 6 | 7 | * [Fly.io instructions](./fly-io-config) 8 | * [Docker-compose instructions](./docker-config) 9 | 10 | **Note: commands should be run from the top-level directory for both Fly.io and 11 | local docker-compose (not from the location of the README files above).** 12 | -------------------------------------------------------------------------------- /cmd/litefs-example/index.tmpl: -------------------------------------------------------------------------------- 1 | 2 |
3 |ID | 20 |Name | 21 |Company | 22 |Phone | 23 |
---|---|---|---|
{{ .ID }} | 28 |{{ .Name }} | 29 |{{ .Company }} | 30 |{{ .Phone }} | 31 |
36 | No data yet, try generating a new row. 37 |
38 | {{ end }} 39 | 40 |42 | Region: {{ .Region }} 43 |
44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /cmd/litefs-example/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "database/sql" 6 | _ "embed" 7 | "flag" 8 | "fmt" 9 | "html/template" 10 | "log" 11 | "math/rand" 12 | "net/http" 13 | "os" 14 | "time" 15 | 16 | "github.com/brianvoe/gofakeit/v6" 17 | _ "github.com/mattn/go-sqlite3" 18 | ) 19 | 20 | // Command line flags. 21 | var ( 22 | dsn = flag.String("dsn", "", "datasource name") 23 | addr = flag.String("addr", ":8080", "bind address") 24 | ) 25 | 26 | var db *sql.DB 27 | 28 | //go:embed schema.sql 29 | var schemaSQL string 30 | 31 | func main() { 32 | log.SetFlags(0) 33 | rand.Seed(time.Now().UnixNano()) 34 | 35 | if err := run(context.Background()); err != nil { 36 | fmt.Fprintln(os.Stderr, err) 37 | os.Exit(1) 38 | } 39 | } 40 | 41 | func run(ctx context.Context) (err error) { 42 | flag.Parse() 43 | 44 | if *dsn == "" { 45 | return fmt.Errorf("dsn required") 46 | } else if *addr == "" { 47 | return fmt.Errorf("bind address required") 48 | } 49 | 50 | // Connect to SQLite database. 51 | db, err = sql.Open("sqlite3", *dsn) 52 | if err != nil { 53 | return fmt.Errorf("open db: %w", err) 54 | } 55 | defer db.Close() 56 | 57 | log.Printf("database opened at %s", *dsn) 58 | 59 | // Run migration. 60 | if _, err := db.Exec(schemaSQL); err != nil { 61 | return fmt.Errorf("cannot migrate schema: %w", err) 62 | } 63 | 64 | // Start HTTP server. 65 | http.HandleFunc("/", handleIndex) 66 | http.HandleFunc("/generate", handleGenerate) 67 | 68 | log.Printf("http server listening on %s", *addr) 69 | return http.ListenAndServe(*addr, nil) 70 | } 71 | 72 | //go:embed index.tmpl 73 | var indexTmplContent string 74 | var indexTmpl = template.Must(template.New("index").Parse(indexTmplContent)) 75 | 76 | func handleIndex(w http.ResponseWriter, r *http.Request) { 77 | // If a different region is specified, redirect to that region. 78 | if region := r.URL.Query().Get("region"); region != "" && region != os.Getenv("FLY_REGION") { 79 | log.Printf("redirecting from %q to %q", os.Getenv("FLY_REGION"), region) 80 | w.Header().Set("fly-replay", "region="+region) 81 | return 82 | } 83 | 84 | // Query for the most recently added people. 85 | rows, err := db.Query(` 86 | SELECT id, name, phone, company 87 | FROM persons 88 | ORDER BY id DESC 89 | LIMIT 10 90 | `) 91 | if err != nil { 92 | http.Error(w, err.Error(), http.StatusInternalServerError) 93 | return 94 | } 95 | defer rows.Close() 96 | 97 | // Collect rows into a slice. 98 | var persons []*Person 99 | for rows.Next() { 100 | var person Person 101 | if err := rows.Scan(&person.ID, &person.Name, &person.Phone, &person.Company); err != nil { 102 | http.Error(w, err.Error(), http.StatusInternalServerError) 103 | return 104 | } 105 | persons = append(persons, &person) 106 | } 107 | if err := rows.Close(); err != nil { 108 | http.Error(w, err.Error(), http.StatusInternalServerError) 109 | return 110 | } 111 | 112 | // Render the list to either text or HTML. 113 | tmplData := TemplateData{ 114 | Region: os.Getenv("FLY_REGION"), 115 | Persons: persons, 116 | } 117 | 118 | switch r.Header.Get("accept") { 119 | case "text/plain": 120 | fmt.Fprintf(w, "REGION: %s\n\n", tmplData.Region) 121 | for _, person := range tmplData.Persons { 122 | fmt.Fprintf(w, "- %s @ %s (%s)\n", person.Name, person.Company, person.Phone) 123 | } 124 | 125 | default: 126 | if err := indexTmpl.ExecuteTemplate(w, "index", tmplData); err != nil { 127 | http.Error(w, err.Error(), http.StatusInternalServerError) 128 | return 129 | } 130 | } 131 | } 132 | 133 | func handleGenerate(w http.ResponseWriter, r *http.Request) { 134 | // Only allow POST methods. 135 | if r.Method != "POST" { 136 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) 137 | return 138 | } 139 | 140 | /* 141 | // If this node is not primary, look up and redirect to the current primary. 142 | primaryFilename := filepath.Join(filepath.Dir(*dsn), ".primary") 143 | primary, err := os.ReadFile(primaryFilename) 144 | if err != nil && !os.IsNotExist(err) { 145 | http.Error(w, err.Error(), http.StatusInternalServerError) 146 | return 147 | } 148 | if string(primary) != "" { 149 | log.Printf("redirecting to primary instance: %q", string(primary)) 150 | w.Header().Set("fly-replay", "instance="+string(primary)) 151 | return 152 | } 153 | */ 154 | 155 | // If this is the primary, attempt to write a record to the database. 156 | person := Person{ 157 | Name: gofakeit.Name(), 158 | Phone: gofakeit.Phone(), 159 | Company: gofakeit.Company(), 160 | } 161 | if _, err := db.ExecContext(r.Context(), `INSERT INTO persons (name, phone, company) VALUES (?, ?, ?)`, person.Name, person.Phone, person.Company); err != nil { 162 | http.Error(w, "Method not alllowed", http.StatusMethodNotAllowed) 163 | return 164 | } 165 | 166 | // Redirect back to the index page to view the new result. 167 | http.Redirect(w, r, r.Referer(), http.StatusFound) 168 | } 169 | 170 | type TemplateData struct { 171 | Region string 172 | Persons []*Person 173 | } 174 | 175 | type Person struct { 176 | ID int 177 | Name string 178 | Phone string 179 | Company string 180 | } 181 | -------------------------------------------------------------------------------- /cmd/litefs-example/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS persons ( 2 | id INTEGER PRIMARY KEY AUTOINCREMENT, 3 | name TEXT NOT NULL, 4 | phone TEXT NOT NULL, 5 | company TEXT NOT NULL 6 | ); 7 | 8 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | nginx: 4 | build: 5 | context: ./docker-config/nginx 6 | ports: 7 | - "8080:80" 8 | primary: 9 | build: 10 | context: . 11 | args: 12 | - "LITEFS_CONFIG=litefs.static-lease.yml" 13 | ports: 14 | - "8081:8081" 15 | privileged: true 16 | environment: 17 | FLY_REGION: primary 18 | IS_PRIMARY: "true" 19 | replica1: 20 | build: 21 | context: . 22 | args: 23 | - "LITEFS_CONFIG=litefs.static-lease.yml" 24 | ports: 25 | - "8082:8081" 26 | privileged: true 27 | environment: 28 | FLY_REGION: replica1 29 | IS_PRIMARY: "false" 30 | -------------------------------------------------------------------------------- /docker-config/README.md: -------------------------------------------------------------------------------- 1 | LiteFS Example Application with Docker 2 | ====================================== 3 | 4 | ## Prerequisites 5 | 6 | You need to [install Docker][] (and docker-compose, which is included in all 7 | recent releases of Docker). 8 | 9 | [install Docker]: https://docs.docker.com/engine/install/ 10 | 11 | ## Usage 12 | 13 | ### Running the app 14 | 15 | You can run the application with this command (from the top-level directory 16 | of the repo): 17 | 18 | ```bash 19 | docker-compose up 20 | ``` 21 | 22 | ### Using the app locally 23 | 24 | The app is configured with the following containers: 25 | 26 | * `primary` - this is the application with LiteFS running as primary 27 | (all writes happen on this node) 28 | * `replica1` - this is the application running with LiteFS as a replica 29 | * `nginx` - nginx configured as a load balancer, and also configured to 30 | route all `POST` requests to the primary node 31 | 32 | Each of these is available to test out locally: 33 | 34 | * http://localhost:8080 - the load balancer 35 | * http://localhost:8081 - the primary 36 | * http://localhost:8082 - the replica 37 | 38 | You can test generating records on each node (it will fail if attempted on 39 | the replica). -------------------------------------------------------------------------------- /docker-config/etc/litefs.static-lease.yml: -------------------------------------------------------------------------------- 1 | # note: there should be one template for this, which you can update as necessary. 2 | # note: had to leave the getting started guide and go to the lease management section bc not using fly 3 | # note: lease.hostname and lease.advertise-url are not clear 4 | # note: we should have an example docker-compose setup with everything working both with consul 5 | # and static leases 6 | # This directory is where your application will access the database. 7 | fuse: 8 | dir: "/litefs" 9 | 10 | # This directory is where LiteFS will store internal data. 11 | # You must place this directory on a persistent volume. 12 | data: 13 | dir: "/var/lib/litefs" 14 | 15 | # The lease section defines how LiteFS creates a cluster and 16 | # implements leader election. For dynamic clusters, use the 17 | # "consul". This allows the primary to change automatically when 18 | # the current primary goes down. For a simpler setup, use 19 | # "static" which assigns a single node to be the primary and does 20 | # not failover. 21 | lease: 22 | # Required. Must be either "consul" or "static". 23 | type: "static" 24 | 25 | # Required. The URL for this node's LiteFS API. 26 | # Should match HTTP port. 27 | advertise-url: "http://primary:20202" 28 | 29 | # Specifies whether the node can become the primary. If using 30 | # "static" leasing, this should be set to true on the primary 31 | # and false on the replicas. 32 | candidate: $IS_PRIMARY 33 | 34 | exec: 35 | - cmd: "litefs-example -addr :8081 -dsn /litefs/db" 36 | -------------------------------------------------------------------------------- /docker-config/nginx/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nginx:alpine 2 | 3 | COPY ./nginx.conf /etc/nginx/nginx.conf 4 | EXPOSE 80 5 | CMD ["nginx", "-g", "daemon off;"] -------------------------------------------------------------------------------- /docker-config/nginx/nginx.conf: -------------------------------------------------------------------------------- 1 | http { 2 | upstream primary { 3 | server primary:8081; 4 | } 5 | upstream all { 6 | server primary:8081; 7 | server replica1:8081; 8 | } 9 | server { 10 | listen 80; 11 | location / { 12 | if ($request_method ~ "(PUT|POST|PATCH|DELETE)") { 13 | proxy_pass http://primary; 14 | } 15 | proxy_pass http://all; 16 | } 17 | } 18 | } 19 | 20 | events { } -------------------------------------------------------------------------------- /fly-io-config/README.md: -------------------------------------------------------------------------------- 1 | LiteFS Example Application on Fly.io 2 | ==================================== 3 | 4 | ## Prerequisites 5 | 6 | First, you'll need to install [`flyctl`][] and then [sign up for a free account][signup]. 7 | 8 | [`flyctl`]: https://fly.io/docs/hands-on/install-flyctl/ 9 | [signup]: https://fly.io/docs/hands-on/sign-up/ 10 | 11 | 12 | ## Usage 13 | 14 | ### Creating the application 15 | 16 | First, we'll need to create our application on [Fly.io](https://fly.io). This 17 | will prompt you for a name or you can autogenerate one for this example. 18 | Remember this name as we'll need it later. 19 | 20 | ```sh 21 | fly launch --region ord --no-deploy 22 | ``` 23 | 24 | The launch command will create a `fly.toml` file and set the primary region to 25 | Chicago (`ord`) but will not launch the app. 26 | 27 | 28 | ### Creating a volume 29 | 30 | Next, we need to set up a persistent volume in our primary region so that our 31 | data is not lost between restarts. 32 | 33 | ```sh 34 | fly volumes create -r ord --size 1 litefs 35 | ``` 36 | 37 | And add a mount to this volume in your `fly.toml` file: 38 | 39 | ```toml 40 | [mounts] 41 | source = "litefs" 42 | destination = "/var/lib/litefs" 43 | ``` 44 | 45 | ### Setting up Consul 46 | 47 | LiteFS uses [Consul](https://consul.io) for its distributed lease. You can find 48 | instructions for using Fly.io's free multi-tenant Consul in the 49 | [Lease Management][] section of the Getting Started guide. 50 | 51 | [Lease Management]: https://fly.io/docs/litefs/getting-started/#lease-configuration 52 | 53 | 54 | ### Launching your app 55 | 56 | The next step is to launch & deploy your app with the following command: 57 | 58 | ```sh 59 | fly deploy 60 | ``` 61 | 62 | The application should build and deploy and you should see it up in running 63 | after a minute or so. You can go to `https://$APPNAME.fly.dev/` and see your 64 | application running live. 65 | 66 | The application is a simple interface for generating fake records. It's just 67 | for illustrating how LiteFS can easily replicate your data between nodes. 68 | 69 | When you click the _"Generate Record"_ button, it will create that row in a 70 | local SQLite database that is running on a LiteFS file system. Any other node 71 | running LiteFS will automatically get those updates and apply them to their 72 | local copy of the database. That lets every node keep an exact copy of the same 73 | database. 74 | 75 | 76 | ### Launching more regions 77 | 78 | This example application is configured to run as a primary only in the 79 | `PRIMARY_REGION` (which is Chicago). It's best practice to run two or more 80 | instances in the primary region and then you can add instances in additional 81 | regions to reduce latency for your users. 82 | 83 | You can clone the configuration of the machine to other regions by using the 84 | `fly m clone` command. The `--select` flag lets you choose from a list of 85 | existing machines to clone. 86 | 87 | ```sh 88 | # Make a second instance in your primary region. 89 | fly m clone --select --region ord 90 | 91 | # Make additional instances in regions around the world (London, Sydney, etc). 92 | fly m clone --select --region lhr 93 | fly m clone --select --region syd 94 | ``` 95 | -------------------------------------------------------------------------------- /fly-io-config/etc/litefs.yml: -------------------------------------------------------------------------------- 1 | # The fuse section describes settings for the FUSE file system. This file system 2 | # is used as a thin layer between the SQLite client in your application and the 3 | # storage on disk. It intercepts disk writes to determine transaction boundaries 4 | # so that those transactions can be saved and shipped to replicas. 5 | fuse: 6 | dir: "/litefs" 7 | 8 | # The data section describes settings for the internal LiteFS storage. We'll 9 | # mount a volume to the data directory so it can be persisted across restarts. 10 | # However, this data should not be accessed directly by the user application. 11 | data: 12 | dir: "/var/lib/litefs" 13 | 14 | # This flag ensure that LiteFS continues to run if there is an issue on starup. 15 | # It makes it easy to ssh in and debug any issues you might be having rather 16 | # than continually restarting on initialization failure. 17 | exit-on-error: false 18 | 19 | # This section defines settings for the option HTTP proxy. 20 | # This proxy can handle primary forwarding & replica consistency 21 | # for applications that use a single SQLite database. 22 | proxy: 23 | addr: ":8080" 24 | target: "localhost:8081" 25 | db: "db" 26 | passthrough: 27 | - "*.ico" 28 | - "*.png" 29 | 30 | # This section defines a list of commands to run after LiteFS has connected 31 | # and sync'd with the cluster. You can run multiple commands but LiteFS expects 32 | # the last command to be long-running (e.g. an application server). When the 33 | # last command exits, LiteFS is shut down. 34 | exec: 35 | - cmd: "litefs-example -addr :8081 -dsn /litefs/db" 36 | 37 | # The lease section specifies how the cluster will be managed. We're using the 38 | # "consul" lease type so that our application can dynamically change the primary. 39 | # 40 | # These environment variables will be available in your Fly.io application. 41 | lease: 42 | type: "consul" 43 | advertise-url: "http://${HOSTNAME}.vm.${FLY_APP_NAME}.internal:20202" 44 | candidate: ${FLY_REGION == PRIMARY_REGION} 45 | promote: true 46 | 47 | consul: 48 | url: "${FLY_CONSUL_URL}" 49 | key: "litefs/${FLY_APP_NAME}" 50 | 51 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/superfly/litefs-example 2 | 3 | go 1.19 4 | 5 | require github.com/brianvoe/gofakeit/v6 v6.18.0 6 | 7 | require github.com/mattn/go-sqlite3 v1.14.15 // indirect 8 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/brianvoe/gofakeit/v6 v6.18.0 h1:tDQ4zJVFQHaJKvY9xYSqGN4S7noZU/doFn15/aNbhCU= 2 | github.com/brianvoe/gofakeit/v6 v6.18.0/go.mod h1:Ow6qC71xtwm79anlwKRlWZW6zVq9D2XHE4QSSMP/rU8= 3 | github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= 4 | github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= 5 | --------------------------------------------------------------------------------