<?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>DEV Community: LORQAI</title>
    <description>The latest articles on DEV Community by LORQAI (@lorqai).</description>
    <link>https://dev.to/lorqai</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4264780%2Fb100100a-6f87-407e-9bed-7831e7c15312.png</url>
      <title>DEV Community: LORQAI</title>
      <link>https://dev.to/lorqai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lorqai"/>
    <language>en</language>
    <item>
      <title>What an Intermittent SQLite Lock Taught Us About Building Local-First Software</title>
      <dc:creator>LORQAI</dc:creator>
      <pubDate>Wed, 25 Nov 2026 20:59:08 +0000</pubDate>
      <link>https://dev.to/lorqai/what-an-intermittent-sqlite-lock-taught-us-about-building-local-first-software-k3l</link>
      <guid>https://dev.to/lorqai/what-an-intermittent-sqlite-lock-taught-us-about-building-local-first-software-k3l</guid>
      <description>&lt;p&gt;Building a local-first application for a single user sounds simple enough. No distributed infrastructure, no thousands of concurrent users, no complicated database servers.&lt;/p&gt;

&lt;p&gt;Yet even an ordinary editorial workflow can reveal an unexpected concurrency problem.&lt;/p&gt;

&lt;p&gt;While developing LORQAI Studio 3, a creative content management application built with Go, SQLite, React, and TypeScript, we encountered an intermittent error:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;database is locked&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It appeared when saving an article after generating a website preview.&lt;/p&gt;

&lt;p&gt;That small error led us to reconsider not only database concurrency, but also how we create and verify backups.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reproducing the Problem
&lt;/h2&gt;

&lt;p&gt;The application generates previews from existing content while allowing users to continue editing.&lt;/p&gt;

&lt;p&gt;We suspected that an active read transaction might overlap with a subsequent save operation.&lt;/p&gt;

&lt;p&gt;Instead of relying on additional retries, we isolated the suspected mechanism in a disposable test database.&lt;/p&gt;

&lt;p&gt;The test followed a simple sequence:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create an article revision.&lt;/li&gt;
&lt;li&gt;Open a read-only transaction and read the revision.&lt;/li&gt;
&lt;li&gt;Keep the read transaction open.&lt;/li&gt;
&lt;li&gt;Attempt to save an updated revision through a separate writable connection.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With rollback-journal mode, the writer reached the configured three-second busy timeout and returned &lt;code&gt;database is locked&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Even though each connection pool was limited to one open connection, the separate reader and writer could still compete for access to the same database.&lt;/p&gt;

&lt;p&gt;We had reproduced a plausible failure mechanism, although the precise sequence behind the original intermittent UI error remained unconfirmed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Moving to WAL
&lt;/h2&gt;

&lt;p&gt;The architectural adjustment was to enable SQLite's Write-Ahead Logging mode for writable database connections.&lt;/p&gt;

&lt;p&gt;The relevant setting is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="no"&gt;PRAGMA&lt;/span&gt; &lt;span class="n"&gt;journal_mode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;WAL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike rollback-journal mode, WAL allows readers to retain their existing snapshots while a writer commits new changes.&lt;/p&gt;

&lt;p&gt;The journal mode also persists across database connections and restarts.&lt;/p&gt;

&lt;p&gt;Our configuration uses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go driver: mattn/go-sqlite3&lt;/li&gt;
&lt;li&gt;Busy timeout: 3000 milliseconds&lt;/li&gt;
&lt;li&gt;Synchronous mode: FULL&lt;/li&gt;
&lt;li&gt;Foreign keys: enabled&lt;/li&gt;
&lt;li&gt;Maximum open connections: one per connection pool&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After the change, the writer successfully committed a new article revision while the existing read transaction continued to see the original title.&lt;/p&gt;

&lt;p&gt;That was the behavior we needed.&lt;/p&gt;

&lt;p&gt;However, WAL does not turn SQLite into a multi-writer database. Write transactions remain serialized, and long-running readers can interfere with checkpoint progress.&lt;/p&gt;

&lt;p&gt;WAL addresses an important concurrency problem, but it does not eliminate every possible source of contention.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Backup Question
&lt;/h2&gt;

&lt;p&gt;Changing the journaling mode introduced another consideration: backup integrity.&lt;/p&gt;

&lt;p&gt;When WAL is active, committed changes may still reside in the associated WAL file. Simply copying the main database file is therefore not a reliable general-purpose backup strategy.&lt;/p&gt;

&lt;p&gt;Our implementation uses SQLite's Online Backup API to create a separate, consistent database snapshot.&lt;/p&gt;

&lt;p&gt;The backup workflow then:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creates an isolated database copy.&lt;/li&gt;
&lt;li&gt;Converts the destination database to DELETE journal mode.&lt;/li&gt;
&lt;li&gt;Confirms that the journal mode has changed.&lt;/li&gt;
&lt;li&gt;Closes the database connections before cleaning up the destination's remaining sidecar files.&lt;/li&gt;
&lt;li&gt;Verifies the resulting backup.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The live application continues using WAL.&lt;/p&gt;

&lt;p&gt;Backup verification includes SQLite's &lt;code&gt;PRAGMA integrity_check&lt;/code&gt;, SHA-256 payload verification, schema consistency checks, and validation of restored content and media.&lt;/p&gt;

&lt;p&gt;Creating a backup is one thing. Demonstrating that it can be restored successfully is another.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the Tests Confirmed
&lt;/h2&gt;

&lt;p&gt;The recorded development results included successful targeted concurrency, backup, recovery, and restart tests.&lt;/p&gt;

&lt;p&gt;The subsequent end-to-end editorial workflow also completed successfully without requiring a save retry.&lt;/p&gt;

&lt;p&gt;These were controlled tests using disposable data, not a long-term production benchmark.&lt;/p&gt;

&lt;p&gt;The results demonstrate that the reproduced reader/writer conflict was addressed under the tested conditions. They do not establish that all possible database contention has been eliminated.&lt;/p&gt;

&lt;h2&gt;
  
  
  What We Learned
&lt;/h2&gt;

&lt;p&gt;Local-first does not mean concurrency-free.&lt;/p&gt;

&lt;p&gt;A single-user application can still have background jobs, preview generation, database writes, and backup operations competing for shared resources.&lt;/p&gt;

&lt;p&gt;Our experience reinforced four practical engineering principles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reproduce a failure before attempting to fix it.&lt;/li&gt;
&lt;li&gt;Understand the database's concurrency model.&lt;/li&gt;
&lt;li&gt;Consider the consequences of configuration changes beyond the immediate problem.&lt;/li&gt;
&lt;li&gt;Verify recovery and the actual user workflow, not just individual components.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The most valuable outcome was not enabling WAL itself.&lt;/p&gt;

&lt;p&gt;It was recognizing how a seemingly small database configuration change could affect the reliability of the wider application.&lt;/p&gt;

&lt;p&gt;Sometimes the smallest error messages lead to the most useful architectural lessons.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.sqlite.org/wal.html" rel="noopener noreferrer"&gt;SQLite Write-Ahead Logging&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.sqlite.org/isolation.html" rel="noopener noreferrer"&gt;SQLite Isolation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.sqlite.org/backup.html" rel="noopener noreferrer"&gt;SQLite Online Backup API&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Based on documented LORQAI Studio 3 development experience and recorded regression tests. AI-generated drafting and editorial review were used in preparing this article.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>sqlite</category>
      <category>go</category>
      <category>architecture</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
