r/programming • • Sep 10 '24

SQLite is not a toy database

https://antonz.org/sqlite-is-not-a-toy-database/
811 Upvotes

309 comments sorted by

View all comments

607

u/bastardoperator Sep 10 '24

I keep trying to push SQLite on my customers and they just don't understand, they think they always need something gigantic and networked. Even when I show them the performance, zero latency, and how everything is structured in the same way, they demand complexity. Keeps me employed, but god damn these people and their lack of understanding. The worst part is these are 2 and 3 table databases with the likelihood of it growing to maybe 100K records over the course of 5-10 years.

235

u/account22222221 Sep 10 '24

Can you convince me that I should choose Sqllite over Postgres, who performs great at small scale, but will also very painless scale to a cluster of if I need it to?

What does it have that other dbs don’t?

253

u/SanityInAnarchy Sep 10 '24

Depends what you're doing.

SQLite's main benefit is simplicity, and its main target isn't replacing Postgres, it's replacing fopen. So it's basically zero overhead, zero work to setup and maintain, backup can be as simple as cp...

I don't know if I agree with the OP that it's a good choice for small websites, but if you're building anything that ships to a user's device, then you should be asking: Should your app work offline? (Is there any reason it shouldn't, for that matter?) If so, probably better to use a library to manage a single file, rather than asking your users to set up some docker nonsense.

71

u/yoniyuri Sep 11 '24

Just so people are aware, you CAN NOT just cp a sqlite database while it is open. There are ways to do it, and I would suggest looking it up before doing it.

6

u/wickedsilber Sep 12 '24

Sounds like you learned this from experience!

28

u/MaleficentFig7578 Sep 11 '24

You shouldn't backup sqlite with cp while the application is running because you can get a torn backup that is corrupted. You can do it while you are holding a database lock (BEGIN IMMEDIATE), or you can stop the application or you can use the sqlite backup API. This advice applies for non-WAL databases and I can't tell you how WAL changes it.

Of course it is safe to take a backup while the application is only reading from the database.

→ More replies (1)

40

u/gimpwiz Sep 11 '24

Yep, it seems to me that if I'm going to have a webserver, I may as well also have a postgres/mysql server, slap em together and do whatever I want, it's gonna work great and be free and be easy to configure and keep going. But if I want to ship something that needs to have a database locally, sqlite is a great option if the data isn't enormous and not super complex. Which 99.8% isn't.

→ More replies (3)

26

u/[deleted] Sep 11 '24

backup can be as simple as cp

not if you have separate WAL file. Which you should do.

→ More replies (5)
→ More replies (5)

54

u/nnomae Sep 10 '24

The best explanation I have seen is that SQLite doesn't replace a database, it replaces file access. So it's amazing when you want to store some structured data without the hassle of setting up a database, i.e. in those situations where the alternative would be to come up with your own file format. If your workload is a more standard single central repository problem then you'll almost always be better off going with one of the other databases.

So smallish amounts of client side structured data storage it's the king. Server side data storage, go with a normal database.

→ More replies (11)

20

u/Mognakor Sep 10 '24

SQLite is great for scenarios that have one of those characteristics

  • clientside data

  • portable files, e.g. read-only data exports, like an advanced form of json, you may want the ability to do joins or fast access for entries within a dataset that can scale up to gigabytes

  • scenarios where updates are not time critical, you can guarantee a single writer, e.g. through a scheduler

  • potentially other scenarios, read https://www.sqlite.org/whentouse.html

→ More replies (3)

54

u/bastardoperator Sep 10 '24

I look at from this perspective, I'm happy to bill my customers more, if complexity makes them feel better, and makes me more money, I'm not going to beat a dead horse. Most of my stuff uses an ORM so switching out the DB and doing a migration is something that can be done fairly easily too. I look at SQLite as the most deployed database in the world and something that everyone uses even if they don't realize it.

29

u/account22222221 Sep 10 '24

Most of my stuff uses an ORM so switching out the DB and doing a migration is something that can be done fairly easily too.

This does not jive with my personal experiences. Both times I worked on a project migrating databases it was hell. Both times the database was a few TBs so it hade more to do with the practical considerations of moving that much data in a time frame that didn’t cause outages. Which is why, IMO, selecting an option that will scale from the start, if there is any chance of scale, is a good choice

→ More replies (2)
→ More replies (2)

40

u/thuiop1 Sep 10 '24

Postgres demands more setup.

37

u/the-berik Sep 10 '24

T.b.h. if you control the server PostGres is also relatively easy to setup.

My go to for development is sqlite but I like postgres in production, also for dashboarding etc.

30

u/thuiop1 Sep 10 '24

Sure, it is not nightmarish, but then again SQLite requires basically no setup.

→ More replies (7)

18

u/fiah84 Sep 10 '24

that's kind of a moot point if you're already in an environment where you can just pick a docker image to spin up

→ More replies (15)
→ More replies (9)

5

u/lux44 Sep 11 '24 edited Sep 11 '24

SQLite is not for concurrent users, the whole database is locked on write. There's 'WAL-mode' to overcome that somewhat, but at that point you really should use more capable database instead.

If you think about clusters, SQLite is not on the table (heh ;))

4

u/mycall Sep 11 '24

SQLite is great for embedding inside mobile apps so you can keep the 1:1 (client/server) tables CRUD DTOs intact while having all the sophisticated SQL queries inside the mobile app. This can greatly simplify things, especially when internet connections are sporadic.

→ More replies (15)

31

u/zapporian Sep 10 '24

All macos + ios apps are / were literally built on it. ie. https://en.wikipedia.org/wiki/Core_Data

It's obviously a pretty good / great solution for native frontend applications, when wrapped with an ORM, formal versioned schema and fully automated migrations.

And obviously on backends that don't need crazy scaling or whatever.

15

u/bastardoperator Sep 10 '24

Not sure how many folks use it on the android platform, but it's there too. I heard the contacts app used it, but not sure if that's true anymore, if so that's practically every phone.

https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase

20

u/Wodanaz_Odinn Sep 10 '24

Android still uses SQLite but it's usually behind an abstraction layer like: https://developer.android.com/training/data-storage/room

0

u/[deleted] Sep 11 '24

[deleted]

→ More replies (2)

5

u/ApatheistHeretic Sep 11 '24

I'm not against the idea, how do I use sqlite from multiple applications hosts? NFS/SMB feel like a failure waiting to happen.

11

u/MaleficentFig7578 Sep 11 '24

You don't and you should use a different database when you need that. Do not run sqlite over NFS/SMB.

→ More replies (1)
→ More replies (16)