Logo

dev-resources.site

for different kinds of informations.

PostgreSQL vs. SQLite: read & write in multithreaded environment

Published at
10/15/2024
Categories
postgres
sqlite
Author
teminian
Categories
2 categories in total
postgres
open
sqlite
open
Author
8 person written this
teminian
open
PostgreSQL vs. SQLite: read & write in multithreaded environment

This post is from my old blog, written at Mar. 7th 2024.
https://codenested.blogspot.com/2024/03/postgresql-vs-sqlite-read-write-in.html


multithreading meme

The start was humble. I needed to cache some data, and I thought just push them to database table and give index, and the rest will be database's job. There were only 2 TEXT fields, and I needed to refer to only one field to search for specific row - which is some kind of key-value store -, so I thought whatever database engine should be fine.

And yes. It was a BIG mistake.

First I tried SQLite, and I found out that, in multithreaded environment some records are evaporated when trying to write to the table simultaneously, even with -DSQLITE_THREADSAFE=2 compile time option. I pushed the same data in same condition, and sometimes I have only 20 records, other times 40, and yet 26 for some others....... What drove me crazier was that the SQLite itself worked fine without any I/O problems. A good moment to shout "WHAT THE HELL?!" in real time.

So I changed the engine to PostgreSQL. Our trustworthy elephant friend saved all the records without any loss. I was satisfied with that, but...... Though I applied b-tree index to necessary field of the table, it took 100 milliseconds for just running SELECT field2 WHERE field1='something'. No, the table was small enough. There were only 680 records and data length was at most 30 characters for field 1 and only 4 characters for field 2. I configured the engine with some optimization, so it worked fine for bigger tables so I felt assured for its performance, but I didn't expect something like this, even in my dreams.

Elephant is tough, but as a side effect it's too slow.......

So, one last chance: I ran pg_dump to move data from PostgreSQL to SQLite, and with same condition(same index, same table structure, ......), I turned on at .timer SQLite shell and it took less than 0.001 second. Hooray!

After some more experiments, SQLite can't fully resist from data loss by itself even with multithread support option enabled, and you need more external support like std::mutex. I guess that it's fread() call doesn't support full serialization in multithread environment, but I have neither time nor abilities to do the proper inspection. :P

Anyway, now I use the combination of SQLite + WAL mode + more SQLite internal cache + std::mutex. Still the write performance looks good, but if needed, I think I could use more files with load balancing via non-cryptographic hash.

sqlite Article's
30 articles in total
Favicon
Android SQLite Crud Tutorial
Favicon
๐Ÿš€ Building a User Management API with FastAPI and SQLite
Favicon
How to Use SQLite in Vue 3: Complete Guide to Offline-First Web Apps
Favicon
Building a Simple SQLite Library Manager in Python
Favicon
MySQL vs SQLite ุฃูŠู‡ู…ุง ุฃูุถู„ ุŸ
Favicon
How to setup Ghost in a VPS using Docker, Mailgun and SQLite
Favicon
Java JDBC + IntelliJ + SQLite - A Beginner's Walkthrough
Favicon
Cloudflare D1 and Prisma: Not a Good Combination (For Now)
Favicon
How to Query CSV Files with SQLite
Favicon
Deploy FastAPI application with SQLite on Fly.io
Favicon
How to import excel into sqlite only 1 step
Favicon
PostgreSQL vs. SQLite: read & write in multithreaded environment
Favicon
PostgreSQL vs. SQLite: ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ์˜ ์ฝ๊ธฐ-์“ฐ๊ธฐ
Favicon
Sometimes it's the little things
Favicon
Tauri 2.0 - Sqlite DB - React
Favicon
SQLite Database Recovery
Favicon
Streamlining Your Rails 8 App: Migrating from Postgres to SQLite
Favicon
I still prefer SQLite for little things you know.
Favicon
How to Build Lightweight GraphRAG with SQLite
Favicon
Can You Create a Product That Makes Money with Wasm?
Favicon
Building a cache in Python
Favicon
Building a RESTful API with Laravel 11, A Completeย Guide
Favicon
In-Memory Database with SQLite
Favicon
Build your own SQLite, Part 2: Scanning large tables
Favicon
Fundamentos para Web APIs com .NET: Uma Introduรงรฃo ao Essencial com Entity Framework
Favicon
Multitenant Database Schemas
Favicon
Use SQLite as a Celery broker in Django
Favicon
Build your own SQLite, Part 1: Listing tables
Favicon
Hosting a simple Laravel application using Turso on Laravel Forge
Favicon
Introducing vectorlite: A Fast and Tunable Vector Search Extension for SQLite

Featured ones: