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

Show parent comments

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.

7

u/[deleted] Sep 11 '24

cp works on multiple files though? :P

25

u/Romeo3t Sep 11 '24

sqlite cli also has a backup command thats dead easy to use.

4

u/[deleted] Sep 11 '24

it doesn't atomically open both sources at the same time

→ More replies (1)
→ More replies (1)
→ 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

6

u/campbellm Sep 11 '24

Indeed. I'm close to 60, and have heard this "use ORM in case we need to change DB's" argument more times than I can remember.

In my 30+ years of doing this,

  • The project has decided to change a db once. And this was from a pretty standard RDBMS to another.

  • The ORM didn't help much. Not "it didn't help at all", but it didn't help much. We still had to change enough stuff that caused us to have to at least INSPECT VERY CAREFULLY the things that we didn't.

Not saying ORMs or other db-layer code is bad, it isn't, but if you're thinking "this will save me time if we change", then you're considering a solution for a problem you not only don't, but will VERY unlikely EVER actually have.

→ More replies (1)
→ 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.

11

u/izpo Sep 10 '24

AFAIK, SQLite can't work as a TCP server.

It really depends what you are building. If (and most of the time when) I need a server that is on TCP, I go with PostgreSQL.

→ More replies (6)

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

11

u/FujiKeynote Sep 10 '24

True, but I can't help but feel dirty by submitting to the implicit complexity of containers, daemons, servers, etc. Maybe I've just never experienced the need for any of that and I'll be eating my words when I have to truly scale, but all other things being equal, nothing beats the simplicity of a simple ASGI app and an embedded database. There's so much fewer things in the stack that might crap out.

I mean I'm also the kind of person who salivates over stuff like chess engines that fit into a bootsector so there's that

→ More replies (1)

8

u/thuiop1 Sep 10 '24

Sure, I am not saying one should not use postgres. Only saying that SQLite typically requires nothing to set up, while postgres does need to take a bit of time or use some kind of container (which tend to be an heavy setup too !)

→ More replies (12)
→ 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)