<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Michael Lynch</title>
    <description>The latest articles on Forem by Michael Lynch (@mtlynch).</description>
    <link>https://forem.com/mtlynch</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F296803%2F1dec81b0-b4cb-4159-85bd-790aec3df63a.jpeg</url>
      <title>Forem: Michael Lynch</title>
      <link>https://forem.com/mtlynch</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mtlynch"/>
    <language>en</language>
    <item>
      <title>How Litestream Eliminated My Database Server for $0.03/month</title>
      <dc:creator>Michael Lynch</dc:creator>
      <pubDate>Fri, 28 May 2021 01:56:34 +0000</pubDate>
      <link>https://forem.com/mtlynch/how-litestream-eliminated-my-database-server-for-0-03-month-5hnp</link>
      <guid>https://forem.com/mtlynch/how-litestream-eliminated-my-database-server-for-0-03-month-5hnp</guid>
      <description>&lt;p&gt;Here's a riddle. My web app keeps all of its data in a SQL database. I can spontaneously tear it down, deploy the code to a different hosting platform, and the app will still serve all the same data. Running my app in production costs $0.03 per month.&lt;/p&gt;

&lt;p&gt;How is this possible?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;That's easy. You have a separate database server running somewhere that stores all of your app's state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;No, my app never talks to a remote database server.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Oh, then you're using a proprietary, managed datastore like &lt;a href="https://aws.amazon.com/dynamodb/"&gt;Amazon DynamoDB&lt;/a&gt; or &lt;a href="https://cloud.google.com/firestore"&gt;Google Cloud Firestore&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Nope, my entire stack is open-source and platform-agnostic.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Then what?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I combined &lt;a href="https://sqlite.org/index.html"&gt;SQLite&lt;/a&gt;, &lt;a href="https://litestream.io/"&gt;Litestream&lt;/a&gt;, and &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;My tool is called &lt;a href="https://logpaste.com"&gt;LogPaste&lt;/a&gt;. It allows users to generate shareable URLs for text files. I use it in my open-source &lt;a href="https://tinypilotkvm.com"&gt;KVM over IP device&lt;/a&gt; so that users can easily share diagnostic logs with me.&lt;/p&gt;

&lt;p&gt;Sharing text files isn't exactly revolutionary, but serverless data replication might be. Here's a demo of me migrating my LogPaste app server between two separate hosting platforms: &lt;a href="https://www.heroku.com/"&gt;Heroku&lt;/a&gt; and &lt;a href="https://fly.io"&gt;fly.io&lt;/a&gt;. There's no database server or data migration step, but all of my data persists between platforms:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://asciinema.org/a/I2HcYheYayeh7aHj23QSY9Vyf"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--44aH_TIC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/MTwEeyS.jpg" alt="Still of shell capture of LogPaste in action"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The best part is that I didn't need to modify my app's code at all. It just writes to a local SQLite database, and Litestream magically handles data replication in the background.&lt;/p&gt;

&lt;p&gt;In this post, I'll explain how I integrated Litestream into my app and how you can do the same to replace your expensive, complicated database servers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data persistence for people who hate database servers
&lt;/h2&gt;

&lt;p&gt;My shameful programmer secret is that I can't maintain a database server.&lt;/p&gt;

&lt;p&gt;I've been building my own software products and services for the past eight years, and I've never used a database server in production. I don't want to be responsible for backups or software upgrades, so anything that requires MySQL, Postgres, or Redis is a dealbreaker for me.&lt;/p&gt;

&lt;p&gt;Instead, I've always used Google-managed datastores like Cloud Datastore, Firebase, and Firestore. But every few years, Google builds a totally new datastore solution, deprecates its old one, and &lt;a href="https://medium.com/@steve.yegge/dear-google-cloud-your-deprecation-policy-is-killing-you-ee7525dc05dc"&gt;dumps all the migration work onto its customers&lt;/a&gt;. I didn't want to create another service on top of a tech stack that Google would probably kill off soon.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tza7obBm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mtlynch.io/litestream/gcp-deprecations.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tza7obBm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mtlynch.io/litestream/gcp-deprecations.png" alt="Screenshot of AppEngine library documentation featuring several deprecation notices"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Litestream: the serverless database server
&lt;/h2&gt;

&lt;p&gt;A few months ago, I saw that &lt;a href="https://twitter.com/benbjohnson"&gt;Ben Johnson&lt;/a&gt;, author of the popular &lt;a href="https://github.com/boltdb/bolt"&gt;Bolt database&lt;/a&gt;, had taken on a new project: &lt;a href="http://litestream.io"&gt;Litestream&lt;/a&gt;. It's a simple, open-source tool that replicates a SQLite database to Amazon's S3 cloud storage.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IHRKwaiM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mtlynch.io/litestream/litestream.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IHRKwaiM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mtlynch.io/litestream/litestream.png" alt="Screenshot of Litestream homepage"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It seemed neat, but I wasn't particularly excited about it. I never use SQLite, so what did I care?&lt;/p&gt;

&lt;p&gt;I didn't have anything against SQLite, but the design seemed impractical. Unlike other databases that send data to an external server over the network, SQLite writes everything to a local file. I always worried, "What happens if I lose that file?"&lt;/p&gt;

&lt;p&gt;Thinking about it more, I realized I'd dismissed Litestream because I didn't use SQLite. But Litestream solved the very obstacle keeping me from adopting SQLite... Maybe this was worth a try.&lt;/p&gt;

&lt;p&gt;Even better, Litestream could be my ticket out of Google Cloud Platform. SQLite runs anywhere, so I'd have freedom in choosing server hosting platforms. Litestream provides vendor flexibility on the storage side, as it supports any S3-compatible service, including &lt;a href="https://www.backblaze.com/b2/cloud-storage.html"&gt;BackBlaze B2&lt;/a&gt;, &lt;a href="https://wasabi.com/"&gt;Wasabi&lt;/a&gt;, and &lt;a href="https://min.io/"&gt;Minio&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Litestream sounded rosy in theory, but you can't judge a technology until you test it in production. I needed a log upload service, and it seemed like the perfect project to test Litestream.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the basic functionality
&lt;/h2&gt;

&lt;p&gt;LogPaste needed to accept HTTP PUT requests from the command-line, so I wrote &lt;a href="https://github.com/mtlynch/logpaste/blob/add9e363bd0ea0116d60e759778114ddbc979024/handlers/paste.go#L45L78"&gt;this simple HTTP handler&lt;/a&gt; in Go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="n"&gt;defaultServer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;pastePut&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandlerFunc&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// Read the full HTTP PUT request body as a string.&lt;/span&gt;
    &lt;span class="n"&gt;bodyRaw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;ioutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"can't read request body"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusBadRequest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bodyRaw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// Generate a random entry ID.&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;generateEntryId&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c"&gt;// Store the PUT body in the SQLite database.&lt;/span&gt;
    &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InsertEntry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"can't save entry"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusInternalServerError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// Send a JSON response with the ID we generated.&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;PastePutResponse&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewEncoder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;a href="https://github.com/mtlynch/logpaste/blob/master/store/sqlite/sqlite.go#L56L75"&gt;&lt;code&gt;InsertEntry&lt;/code&gt; implementation&lt;/a&gt; looks how you'd expect. It's a basic SQLite row insertion:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;InsertEntry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`
  INSERT INTO entries(
    id,
    creation_time,
    contents)
  values(?,?,?)`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RFC3339&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows LogPaste to accept HTTP requests from the command line like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; PUT &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s2"&gt;"Hello, world!"&lt;/span&gt; http://localhost:3001
&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;:&lt;span class="s2"&gt;"fFnL9cU6"&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;curl http://localhost:3001/fFnL9cU6
Hello, world!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That works, but it's writing the SQLite database to the local filesystem. I needed to integrate Litestream to enable cloud storage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Layering in Litestream for cloud data syncing
&lt;/h2&gt;

&lt;p&gt;One of Litestream's biggest strengths is that it's completely independent of the application it serves. My LogPaste code never calls into a Litestream API, nor does it require any special configuration to allow syncing. Litestream quietly does its job in the background.&lt;/p&gt;

&lt;p&gt;I created &lt;a href="https://hub.docker.com/r/mtlynch/logpaste/"&gt;a custom Docker image&lt;/a&gt; to combine Litestream and LogPaste. Generally, Docker images should hold Just One Service, but I sometimes bend this rule to facilitate deployment. It's orders of magnitude easier to deploy a single, independent Docker container than two containers that need to coordinate with each other.&lt;/p&gt;

&lt;p&gt;LogPaste's &lt;a href="https://github.com/mtlynch/logpaste/blob/a9d9b39e4b78401c68cd54ed3d2fd40838dd7b8b/Dockerfile"&gt;Dockerfile&lt;/a&gt; starts by building the LogPaste binary from source, and then it pulls down the Linux executable for Litestream.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;# Build LogPaste from source&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;go build &lt;span class="se"&gt;\
&lt;/span&gt;  &lt;span class="nt"&gt;-mod&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;readonly&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;  &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;  &lt;span class="nt"&gt;-o&lt;/span&gt; /app/server &lt;span class="se"&gt;\
&lt;/span&gt;  ./main.go

&lt;span class="c"&gt;# Download Litestream executable&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;wget &lt;span class="s2"&gt;"https://github.com/benbjohnson/litestream/releases/download/v&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;litestream_version&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;litestream_deb_filename&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, Docker copies a custom &lt;code&gt;litestream.yml&lt;/code&gt; file into the image. This is Litestream's &lt;a href="https://litestream.io/reference/config/"&gt;configuration file&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;access-key-id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${LITESTREAM_ACCESS_KEY_ID}&lt;/span&gt;
&lt;span class="na"&gt;secret-access-key&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${LITESTREAM_SECRET_ACCESS_KEY}&lt;/span&gt;
&lt;span class="na"&gt;dbs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DB_PATH}&lt;/span&gt;
    &lt;span class="na"&gt;replicas&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DB_REPLICA_URL}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;replicas.url&lt;/code&gt; field contains the cloud storage location for my database. &lt;code&gt;access-key-id&lt;/code&gt; and &lt;code&gt;secret-access-key&lt;/code&gt; are the IAM-style credentials Litestream needs to access the cloud storage bucket.&lt;/p&gt;

&lt;p&gt;You can hardcode these values into the configuration file, but Litestream supports environment variables and interpolates them at runtime. That's a convenient feature, as it allows you to keep your &lt;code&gt;litestream.yml&lt;/code&gt; file under source control without storing any sensitive credentials. It also makes the Docker image portable — anyone can create their own LogPaste server by reusing &lt;a href="https://hub.docker.com/r/mtlynch/logpaste/"&gt;my image&lt;/a&gt; and setting environment variables for their cloud storage bucket.&lt;/p&gt;

&lt;p&gt;The next bit of Litestream logic is in LogPaste's &lt;a href="https://github.com/mtlynch/logpaste/blob/a9d9b39e4b78401c68cd54ed3d2fd40838dd7b8b/docker_entrypoint"&gt;&lt;code&gt;docker_entrypoint&lt;/code&gt; script&lt;/a&gt;, which runs when the Docker container launches. It starts by pulling down the app's latest database snapshot from cloud storage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Restore database from S3.&lt;/span&gt;
litestream restore &lt;span class="nt"&gt;-if-replica-exists&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;DB_PATH&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-if-replica-exists&lt;/code&gt; flag tells Litestream that it's okay if no snapshots exist on cloud storage yet. Otherwise, you'd have a chicken-and-egg problem. Your app could never launch because there's no cloud database to restore, but Litestream can't replicate the database to cloud storage because the app has never run.&lt;/p&gt;

&lt;p&gt;Next, the entrypoint script spawns a Litestream process, which watches LogPaste's SQLite database and continuously streams any changes to cloud storage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Begin replication to S3 in the background.&lt;/span&gt;
litestream replicate &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;DB_PATH&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;DB_REPLICA_URL&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &amp;amp;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The minor hack is in that trailing &lt;code&gt;&amp;amp;&lt;/code&gt;. It tells the script to run the Litestream process in the background, which is how I can execute two long-running processes in the same Docker container. Ben Johnson has published &lt;a href="https://github.com/benbjohnson/litestream-s6-example"&gt;a cleaner solution&lt;/a&gt;, but I'm using the hacky version for ease of demonstration.&lt;/p&gt;

&lt;p&gt;The entrypoint script ends by launching the Logpaste app, which is a simple HTTP server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Start LogPaste server.&lt;/span&gt;
/app/server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To run the Docker container with all the environment variables populated, I use &lt;a href="https://github.com/mtlynch/logpaste#from-docker--cloud-data-replication"&gt;this command&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;LITESTREAM_ACCESS_KEY_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;MY-ACCESS-ID
&lt;span class="nv"&gt;LITESTREAM_SECRET_ACCESS_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;MY-SECRET-ACCESS-KEY
&lt;span class="nv"&gt;DB_REPLICA_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;s3://my-bucket-name/db

docker run &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"PORT=3001"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"LITESTREAM_ACCESS_KEY_ID=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;LITESTREAM_ACCESS_KEY_ID&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"LITESTREAM_SECRET_ACCESS_KEY=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;LITESTREAM_SECRET_ACCESS_KEY&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"DB_REPLICA_URL=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;DB_REPLICA_URL&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-p&lt;/span&gt; 3001:3001/tcp &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--name&lt;/span&gt; logpaste &lt;span class="se"&gt;\&lt;/span&gt;
  mtlynch/logpaste
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's how it all fits together in production:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BXBybWBM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mtlynch.io/litestream/diagram.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BXBybWBM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mtlynch.io/litestream/diagram.jpg" alt="Diagram of how LogPaste, Litestream, Docker, and S3 all fit together"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using LogPaste in production
&lt;/h2&gt;

&lt;p&gt;I'm using LogPaste in production for &lt;a href="https://tinypilotkvm.com"&gt;TinyPilot&lt;/a&gt;, my open-source KVM over IP device. Because users run my software on devices they own, I can't see any diagnostic information when they report issues. LogPaste provides a convenient way for users to share their logs with me.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/HZUdtrz4SB4"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;LogPaste has handled all of TinyPilot's debug logs for the past few months, and it's worked well. The cost for data replication truly is just $0.03 per month:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BL8WU7CC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mtlynch.io/litestream/aws-bill.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BL8WU7CC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mtlynch.io/litestream/aws-bill.png" alt="Screenshot of AWS bill showing $0.03 in S3 charges and $0.00 in data transfer fees"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My use case is, admittedly, fairly gentle. Only a handful of users upload their logs each day, so there may be pain points with this setup under heavier workloads.&lt;/p&gt;

&lt;p&gt;It's also important to note that Litestream can't resolve conflicts between multiple database writes, so each database can have only one application server with write access.&lt;/p&gt;

&lt;p&gt;Still, I've been incredibly impressed with Litestream, and I'm eager to use it in more scenarios.&lt;/p&gt;

&lt;h2&gt;
  
  
  Self-hosting LogPaste
&lt;/h2&gt;

&lt;p&gt;If you want to host your own instance of my LogPaste app, it's easy to deploy. You can even customize the text on the homepage so that it says your product's name instead of "LogPaste."&lt;/p&gt;

&lt;p&gt;For example, here's TinyPilot's version:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xy9Ic1Sl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mtlynch.io/litestream/tinypilot-branding.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xy9Ic1Sl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mtlynch.io/litestream/tinypilot-branding.png" alt="Screenshot of TinyPilot's LogPaste instance"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I've written deployment instructions for a few different platforms:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Platform&lt;/th&gt;
&lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/mtlynch/logpaste/blob/master/docs/deployment/fly.io.md"&gt;fly.io&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Free tier allows up to three always-on instances and includes SSL certificates&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/mtlynch/logpaste/blob/master/docs/deployment/lightsail.md"&gt;Amazon LightSail&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;$7/month per instance, includes SSL certificates&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/mtlynch/logpaste/blob/master/docs/deployment/heroku.md"&gt;Heroku&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Free tier allows unlimited on-demand instances, $7/month for SSL certificates on custom domains&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://litestream.io/"&gt;Litestream&lt;/a&gt;: Litestream's official documentation.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/mtlynch/logpaste"&gt;mtlynch/logpaste&lt;/a&gt;: MIT-licensed source code and documentation for LogPaste.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/benbjohnson/litestream-s6-example"&gt;litestream-s6-example&lt;/a&gt;: A more advanced and robust method for running Litestream alongside your app in a Docker container. It uses &lt;a href="https://github.com/just-containers/s6-overlay"&gt;s6-overlay&lt;/a&gt; to restart the Litestream instance on failure.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Architecture diagram by &lt;a href="https://www.lolo-ology.com/"&gt;Loraine Yow&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks to &lt;a href="https://twitter.com/benbjohnson"&gt;Ben Johnson&lt;/a&gt; for his work on Litestream and his early review of this article. Thanks to the members of the &lt;a href="https://bloggingfordevs.com"&gt;Blogging for Devs Community&lt;/a&gt; for providing feedback on this post.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>litestream</category>
      <category>sqlite</category>
      <category>go</category>
      <category>pastebin</category>
    </item>
  </channel>
</rss>
