<?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: Rudra Attri Pandey</title>
    <description>The latest articles on DEV Community by Rudra Attri Pandey (@rudraattripandey).</description>
    <link>https://dev.to/rudraattripandey</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%2F4269507%2F1f90ad6a-c02a-4e34-8787-6df8dfe0b921.png</url>
      <title>DEV Community: Rudra Attri Pandey</title>
      <link>https://dev.to/rudraattripandey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rudraattripandey"/>
    <language>en</language>
    <item>
      <title>I put SQLite in a serverless function. Here's what it actually costs you.</title>
      <dc:creator>Rudra Attri Pandey</dc:creator>
      <pubDate>Fri, 27 Nov 2026 18:56:40 +0000</pubDate>
      <link>https://dev.to/rudraattripandey/i-put-sqlite-in-a-serverless-function-heres-what-it-actually-costs-you-1a7h</link>
      <guid>https://dev.to/rudraattripandey/i-put-sqlite-in-a-serverless-function-heres-what-it-actually-costs-you-1a7h</guid>
      <description>&lt;p&gt;I run eleven small AI apps on a free Vercel plan. No managed Postgres, no Redis, no monthly bill. When you refuse to pay for a database, you end up asking a strange question: can SQLite just live inside the function?&lt;/p&gt;

&lt;p&gt;The answer is yes. It also comes with a footgun that took me a while to see clearly, and I think most "SQLite on serverless" posts skip it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;

&lt;p&gt;The library is &lt;code&gt;sql.js&lt;/code&gt; â€” SQLite compiled to WebAssembly. No native bindings, no build step, no binary that breaks on a different OS. It loads a &lt;code&gt;.wasm&lt;/code&gt; file and gives you a database object in memory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;initSqlJs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'sql.js'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;SQL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;initSqlJs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;locateFile&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'node_modules/sql.js/dist/sql-wasm.wasm'&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;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;SQL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Database&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="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'CREATE TABLE leads (id INTEGER PRIMARY KEY, email TEXT)'&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;From there it behaves like SQLite. You write SQL, you read rows, and when you want to persist, you export the whole database as a byte array and write it to disk:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dbPath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;export&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;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On Vercel the only writable path is &lt;code&gt;/tmp&lt;/code&gt;, so that's where the file goes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;isVercel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;VERCEL&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'1'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;dbDir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;isVercel&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s1"&gt;'/tmp'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the whole trick. It works on the first request, it works on the second, and for a demo it feels like cheating in a good way.&lt;/p&gt;

&lt;h2&gt;
  
  
  The part that bites
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;/tmp&lt;/code&gt; is not a database. It's scratch space attached to one running instance.&lt;/p&gt;

&lt;p&gt;Three consequences, and they compound:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Cold starts give you an empty disk.&lt;/strong&gt; Your function wakes up, finds no file, creates a fresh schema, and serves an empty table. No error. Just silently no data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Every instance has its own &lt;code&gt;/tmp&lt;/code&gt;.&lt;/strong&gt; Two concurrent invocations can be two different machines with two different copies of your database. Writes don't merge. Last one wins, or rather, each one only ever sees itself.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nothing survives a deploy.&lt;/strong&gt; New build, new instances, new empty files.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;None of this shows up in local development, because locally &lt;code&gt;dbPath&lt;/code&gt; is &lt;code&gt;__dirname&lt;/code&gt; and the file genuinely persists. The bug appears in production, under load, and it looks like "sometimes the data disappears," which is the worst kind of bug to debug.&lt;/p&gt;

&lt;p&gt;I hit this and then did what most people do: I added a fallback. Then another one.&lt;/p&gt;

&lt;h2&gt;
  
  
  The three layers I ended up with
&lt;/h2&gt;

&lt;p&gt;Layer one is &lt;code&gt;sql.js&lt;/code&gt; writing to &lt;code&gt;/tmp&lt;/code&gt;. Fast, real SQL, no network hop.&lt;/p&gt;

&lt;p&gt;Layer two is a JSON ledger. If &lt;code&gt;sql.js&lt;/code&gt; fails to load â€” a missing wasm file, a bundler that dropped it, an environment that can't write to &lt;code&gt;/tmp&lt;/code&gt; â€” the app keeps functioning against a JSON file instead of throwing. Slower and less capable, but the user never sees a 500.&lt;/p&gt;

&lt;p&gt;Layer three is an in-memory array. If even the file write fails, the app still answers requests for the life of that instance. Data is gone when the instance dies, but the demo still works.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;initSqlJs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'sql.js'&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="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'sql.js loading deferred'&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;That &lt;code&gt;try/catch&lt;/code&gt; around a &lt;code&gt;require&lt;/code&gt; looks paranoid until a deploy ships without the wasm file and every request returns a 500. Now the failure is a degradation instead of an outage.&lt;/p&gt;

&lt;p&gt;There's also a fourth mode: if a Supabase URL and key are present, the app switches to PostgREST and skips local storage entirely. Same interface, different backend. That's the honest answer for anything that must not lose rows â€” the free tier is generous and Postgres is Postgres.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two details worth stealing
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Migrate on boot, and expect it to fail.&lt;/strong&gt; Schema changes on an ephemeral database have to be idempotent, because the "migration" runs on every cold start:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ALTER TABLE leads ADD COLUMN phone TEXT DEFAULT '';"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;saveDB&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;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// column already exists&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No migration table, no version number. The &lt;code&gt;try/catch&lt;/code&gt; is the version control.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Write on every mutation, not on an interval.&lt;/strong&gt; There's no graceful shutdown on serverless. If you batch writes on a timer, the instance gets frozen between ticks and you lose the batch. Export and write synchronously after each change, and accept the cost.&lt;/p&gt;

&lt;h2&gt;
  
  
  When this is genuinely fine
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Read-mostly apps where the data is a seed, not a record.&lt;/li&gt;
&lt;li&gt;Single-user tools and demos. Nobody else is hitting the instance.&lt;/li&gt;
&lt;li&gt;Anything where losing state is annoying but not damaging â€” a scratchpad, a calculator, a converter.&lt;/li&gt;
&lt;li&gt;Cases where the alternative is not shipping at all. A tool that loses your history is still a tool.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Five of my eleven apps use this. The rest keep a JSON ledger, because JSON is even simpler and for those apps SQL was never buying anything.&lt;/p&gt;

&lt;h2&gt;
  
  
  When it will burn you
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Money, bookings, anything with a payment attached.&lt;/li&gt;
&lt;li&gt;Multi-user data where two people must see the same rows.&lt;/li&gt;
&lt;li&gt;Anything you'd have to apologise for losing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The failure mode isn't a crash. It's data that quietly isn't there, and a user who assumes it's their fault.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd change
&lt;/h2&gt;

&lt;p&gt;I'd decide the storage tier before writing the first route, not after. "Free" is the right default for a demo, but the question isn't &lt;em&gt;can I avoid a database&lt;/em&gt; â€” it's &lt;em&gt;what happens to this row if the instance dies right now.&lt;/em&gt; If the answer is "it disappears," that has to be a decision, not a discovery.&lt;/p&gt;

&lt;p&gt;Serverless SQLite is a great deal. It's just a deal with terms, and the terms are in the fine print about &lt;code&gt;/tmp&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>sqlite</category>
      <category>serverless</category>
      <category>javascript</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
