News for hackers. Stuff that matters.
Re:Ask HN: Have you used SQLite as a primary database? by mekster on Monday April 25, 2022 @09:44AM (#31154575)
What is the point of using SQLite under a web service?

I thought people complained how MySQL sucks and PostgreSQL rocks for being right and SQLite was nowhere near being right or performant. (Things seem to be getting better with strict column types these days.)

I've recently migrated a smallish service from MySQL to PostgreSQL and figured it's quite a work if you're not careful writing by the SQL standard which means if the service had gotten bigger, your chance of moving away from SQLite kind of walks away.

So, why not use a safer choice to begin with? Nothing is complicated running MySQL/PostgreSQL unless you've sold yourself to AWS to care for the cost and don't know how to run a DB instance yourself.

Re:Ask HN: Have you used SQLite as a primary database? by oppositelock on Monday April 25, 2022 @03:07PM (#31159281)
SQLite is far faster than Postgres or MySQL, however, the price you pay for this is having a single writer thread, and it's a library incorporated into your process, not a shared DB. It's faster because those other features of a server have a cost as well, particularly the cost of write arbitration.

SQLite is fine when all your load can be served by a single backend process on a single machine. The moment you need multiple backends to handle more load, or the moment you need high availability, you can't do it with SQLite. SQLite has very limited DDL operations, so you also can't evolve your schema over time without downtime. Now, for streaming backups - how do you come back from node failure? You're going to incur downtime downloading your DB.

I run many SQL backed production services, and my sweet spot has become a big honking Postgres instance in RDS in AWS, with WAL streaming based replicas for instant failover in case of outage, and read replicas, also via WAL. This system has been up for four years at this point, with no downtime.

I love SQLite, and use it regularly, just not for production web services.

Re:Ask HN: Have you used SQLite as a primary database? by js2 on Monday April 25, 2022 @07:41PM (#31162292)
> my sweet spot has become a big honking Postgres instance in RDS

Why do you prefer PostgreSQL RDS over Aurora RDS? Aurora seems better in every way but price[1]. (I know it also had some growing pains at launch.)

[1]: Amazon RDS for PostgreSQL is ideal when you have a small-to-medium intense workload. It works best when you have limited concurrent connections to your database. If you’re moving from commercial database engines such as Oracle or Microsoft SQL Server, Aurora PostgreSQL is a better choice because it provides matching performance with a lower price.

https://aws.amazon.com/blogs/database/is-amazon-rds-for-post...

Re:Ask HN: Have you used SQLite as a primary database? by oppositelock on Monday April 25, 2022 @11:49PM (#31163744)
Price is the main reason. Write performance is a second one.
Re:Ask HN: Have you used SQLite as a primary database? by js2 on Tuesday April 26, 2022 @11:02AM (#31168329)
The article I linked implies that Aurora offers higher write performance:

> If your database workload reaches the Amazon RDS max limit of 80,000 IOPS, and it requires additional IOPS, Aurora PostgreSQL is the preferred database solution. If database workload requires less than 80,000 IOPS, you can choose either Amazon RDS for PostgreSQL or Aurora PostgreSQL based on supported features.

Re:Ask HN: Have you used SQLite as a primary database? by dclusin on Monday April 25, 2022 @05:32PM (#31160941)
SQLite has write ahead logging as well

https://sqlite.org/wal.html

Re:Ask HN: Have you used SQLite as a primary database? by oppositelock on Monday April 25, 2022 @11:50PM (#31163755)
It doesn't let you run an active replica from WAL.
Re:Ask HN: Have you used SQLite as a primary database? by lm28469 on Monday April 25, 2022 @10:34AM (#31155403)
> So, why not use a safer choice to begin with?

I know several people who build projects like that. It take them months to get a working product, just to discover it doesn't interest people or doesn't work like they expected. If for every piece of tooling you go for the "safe" and most performant one you gain bloat and complexity real quick.

People underestimate "simple" tech performance, in 99% of projects by the time your bottleneck is your DB system I can assure you that it'll be the least of your concerns

Re:Ask HN: Have you used SQLite as a primary database? by mekster on Monday April 25, 2022 @10:55AM (#31155741)
Which part of running MySQL instead SQLite is over engineering?
Re:Ask HN: Have you used SQLite as a primary database? by post-it on Monday April 25, 2022 @11:01AM (#31155840)
- Setting up a MySQL server on both your dev machine and server, and making sure they're the same version (extra fun if they're on different OS versions)

- Setting up an out-of-repo config file on the server with your MySQL credentials

- Setting up a backup script for your server data

It's only about an hour of work total, but it's an hour of work that I hate doing.

Re:Ask HN: Have you used SQLite as a primary database? by marcosdumay on Monday April 25, 2022 @01:00PM (#31157649)
Hum... MySQL is one of those labor-generating technologies that a Luddite would fall in love for. Those are best avoided. But for Postgres...

- You make sure the production version is larger or equal than the development, or you make sure to not use new features before they reach production, what is quite easy. There is no problem with different OSes (except for Windows itself not being very reliable, but I imagine you are not using Windows on production, as it's another one of those labor-generating techs).

- Trusting a local user is the same level of security you get with SQLite, no credentials required.

- And setting a backup script... Wait, you don't do that for SQLite? There's something missing here.

Yes, there are a lot of small tasks that add up when setting some new software. It's a pain. But it's a pain you suffer once, and it's over. It's worth optimizing, but not at any ongoing cost.

Re:Ask HN: Have you used SQLite as a primary database? by jamesrr39 on Monday April 25, 2022 @11:32AM (#31156299)
docker-compose is the way to go for keeping the dev versions synced with the production version. And for the backup script scheduled mysqldump and copy to storage should see you through quite far, so not really any more effort than copying an SQLite database.
Re:Ask HN: Have you used SQLite as a primary database? by orthecreedence on Monday April 25, 2022 @12:54PM (#31157571)
This person is talking about reducing complexity, so I don't think adding more moving pieces to the machine is the way to go.
Re:Ask HN: Have you used SQLite as a primary database? by b20000 on Monday April 25, 2022 @12:45PM (#31157427)
and now you need to learn docker and 10 other things.
Re:Ask HN: Have you used SQLite as a primary database? by mekster on Monday April 25, 2022 @11:15AM (#31156041)
So you trade for some risk for an hour.
Re:Ask HN: Have you used SQLite as a primary database? by TheRealPomax on Monday April 25, 2022 @11:32AM (#31156303)
Please do explain what risk you're thinking of, as anyone smart enough to write their own SaaS would not put resources in the web server's file system tree? You stick your db file in an normal secure location outside the server's root, chmodded appropriately so that it suffers the exact same risks as any other file on the OS. It's no more or less risky than /etc/shadow, while being considerably easier to work with (and less failure-prone for light db work) than an independently running database service.
Re:Ask HN: Have you used SQLite as a primary database? by mekster on Monday April 25, 2022 @03:39PM (#31159579)
The risk is as I had written previously that it takes some effort to move away from a db to another when the need arises when I see no benefit in choosing SQLite in the beginning.

I'm not a professional db engineer but one point is that there doesn't seem to be a way to create functions in SQLite which would mean creating triggers on various tables can cause excessive amount of duplicate code.

If I rely on PostgreSQL, I feel covered for my use case for web apps but once you hit some little gotchas in SQLite, you may regret about saving 10 minutes (install db and set up a password) for nothing.

Re:Ask HN: Have you used SQLite as a primary database? by post-it on Monday April 25, 2022 @08:25PM (#31162573)
If a project of mine ever gets enough traffic to force me away from SQLite, I expect I'll be rolling in dough and willing to put in the effort.
Re:Ask HN: Have you used SQLite as a primary database? by TheRealPomax on Monday April 25, 2022 @04:00PM (#31159781)
That's not a risk, that's just an inefficiency further down the line (migrating data from sqlite to a "real" database can indeed be quite a chore, but far less so if you formalized your schema and constraints beforehand, so that a migration mostly involves exporting to SQL, rewriting where needed, and then importing into whatever dbms you end up using later on in the lifetime of your project).

When we're talking about risks, think security exploits: how is sqlite3 more likely to get your data leaked, or flat out copied in its entirety, compared to using a mysql/postgres/etc.

Re:Ask HN: Have you used SQLite as a primary database? by mekster on Monday April 25, 2022 @05:04PM (#31160578)
Inefficiency or not, if you start down the path of SQLite, you need to invest good amount of time refactoring into another DB if you feel like migrating away.

When I realized SQLite would store any type of data in any kind of column type, it was obvious SQLite is different from others. They only added strict types about a year ago but scared me enough not to use it.

And how is SQLite any less secure? You can flat out copy its entirety using pg_dumpall. I'm not talking about security.

Re:Ask HN: Have you used SQLite as a primary database? by TheRealPomax on Thursday April 28, 2022 @12:49PM (#31194854)
While that's an unfortunate discovery, I do need to point out that that's a problem of your own making, not SQLite's fault. SQLite is very clear on what you can expect, with dynamic typing being one of the main differences between it and "regular SQL" databases.

Yes, plenty of folks just go for SQLite blindly, but the consequences of that aren't SQLite's doing: if you take the time to read through https://www.sqlite.org/features.html because you want to know what it can actually do, you'll almost certainly click through to https://www.sqlite.org/omitted.html because you'll want to know what it doesn't do, and then you'll see the "See also the Quirks, Caveats, and Gotchas of SQLite." link and you're going to follow it an read through https://www.sqlite.org/quirks.html because those sound pretty important to know about before you commit to using something that is going to be your application/service data store for the foreseeable future.

Re:Ask HN: Have you used SQLite as a primary database? by deckard1 on Monday April 25, 2022 @01:26PM (#31158024)
your app has full access to the SQLite file. MySQL/PostgreSQL have users and permissions. Security is about layers, and SQLite is removing one layer of security. You can, for example, put DELETEs or access to certain tables on a separate user that your web app has no access to. With SQLite, if your app gets hacked then they can do anything with the whole DB they want to. In addition, with a separate DB process you get audit logs. If someone hacks your SQLite app they may have access for months before you realize it, if you ever do. Especially if they are doing something subtle like UPDATEs on specific tables/fields that may go unnoticed but provide the hacker some benefit. This is why you can't simply rely on the idea of using a backup. That's only going to help if the hacker totally trashes your DB.

With a separate DB you may have a hope of detecting when someone hacked your app. But without that firewall, the question becomes: how much of the data in my SQLite can now be trusted? If you don't know what backup is safe to restore, then you can't trust any of it.

Again, this is about layers. Not saying MySQL/Postgres will save you. But they can increase the odds.

Re:Ask HN: Have you used SQLite as a primary database? by TheRealPomax on Monday April 25, 2022 @02:46PM (#31159013)
If your server or API can be exploited, it doesn't matter whether there's an auth layer in between. Your SQL server runs as a service to connect to, your sqlite3 file is a file that you need access to. They're the same kind of layer: you need to break through the server's security to ever get to them directly, and if your app gets hacked such that the hackers gain file system access, then:

1. You're fucked. The end. It doesn't matter whether you were using mysql, postgres, or sqlite3, or S3, or Redis, or any other server your app was connecting to: they can just look at your environment vars.

That's not going to happen "because you're using Sqlite3", that's going to happen because you used some obscure server software, or worse, rolled your own.

People really do seem to put too much faith into "it has a username and password, it's more secure". It's not: if someone has access to your actual server, they have access to everything your server has access to. Sqlite3 is no more or less secure than a dbms daemon (or remote) in that sense.

Re:Ask HN: Have you used SQLite as a primary database? by ipaddr on Monday April 25, 2022 @07:05PM (#31161993)
With sqlite your server, api or application can be hacked. The most common and likely hack would be somewhere in your application. It really doesn't make sense to use sqlite here.

Setup a separate database server and use it for all of your projects. That one hour pays off each and every project.

Re:Ask HN: Have you used SQLite as a primary database? by kyawzazaw on Monday April 25, 2022 @11:25AM (#31156195)
Pieter Levels has been using SQLite for nomadlist and I think it's been going well for him.
Re:Ask HN: Have you used SQLite as a primary database? by post-it on Monday April 25, 2022 @11:56AM (#31156675)
Some risk of what?
Re:Ask HN: Have you used SQLite as a primary database? by dgb23 on Monday April 25, 2022 @01:11PM (#31157823)
Running MySQL/Postgres over SQLite:

- needs to be provisioned and configured

- needs additional tooling and operational overhead

- comes with a _large_ performance overhead that is only won back if you have quite a significant load - especially writes, which means the vast majority of web projects are slower and require more resources than they should.

- it makes the whole system more complex by definition

It is a cost-benefit thing that tilts towards RDBMS as soon as you need to sustain very high transactional loads and want a managed, individually accessible server that you can query and interact with while it's running in production.

But if it is just "a website that needs durability" then you haven't yet shown how that tradeoff is worth it.

Re:Ask HN: Have you used SQLite as a primary database? by rapind on Monday April 25, 2022 @02:31PM (#31158848)
Just wanted to add another’s scenario where postgresql has been useful to me. Functions. There are cases where you have expensive operation(s) that reference a lot of persistent data. Even without massive traffic these operations can be prohibitively expensive in the middleware. Leveraging database functions can be a massive performance improvement (100x + for me), especially if your middleware is slow (e.g. rails).

I’ve used SQLite in production once and it worked great. But that was a very simple app. For more complex (but not always higher traffic) I’m leaning more and more on postgresql and less on my middleware, like moving business logic to the database when it makes sense.

Re:Ask HN: Have you used SQLite as a primary database? by dgb23 on Monday April 25, 2022 @02:59PM (#31159185)
If you need the expressiveness and power of Postgres then sure, it has also way better JSON support for example, there is generally better tooling for it as well and so on. But in this case, your database becomes it's own _thing_, has much more value outside of being just durability for an application. Like for example Supabase is doing things. That's a very fundamental design decision IMO. I explored this and it is very attractive and robust, but serves different use-cases.
Re:Ask HN: Have you used SQLite as a primary database? by dspillett on Monday April 25, 2022 @10:45AM (#31155597)
> What is the point of using SQLite under a web service? … people complained how MySQL sucks and PostgreSQL rocks for being right and SQLite was nowhere near being right or performant.

My understanding is that for read performance SQLite is pretty damn good, outperforming MySQL and Postgres in both single and concurrent tests. The key performance issue is the single global write lock. If your data access pattern is massively read biased then SQLite is a good choice performance wise, if you see a lot of write activity then it really isn't.

With regard to being correct*, it offers proper ACID transactions and so on. Typing is a big issue for some but far from all. It is significantly more correct than mysql used to be back before InnoDB became the default table type in ~2010, at least as correct as it now (aside from the data types matter depending on which side of that you sit on).

Re:Ask HN: Have you used SQLite as a primary database? by Glench on Monday April 25, 2022 @09:56AM (#31154783)
> What is the point of using SQLite under a web service?

Take a look at the Consider SQLite post I linked. They address your performance questions too.

For me, SQLite was a nice way to simplify launching and running my SaaS business and has had no downsides.

Re:Ask HN: Have you used SQLite as a primary database? by iampims on Monday April 25, 2022 @11:40AM (#31156420)
Any tips you can share on how to deploy your saas app without downtime when using sqlite?
Re:Ask HN: Have you used SQLite as a primary database? by srcreigh on Monday April 25, 2022 @02:48PM (#31159051)
For my personal project I'm planning to use a VM with an SSD. Manually I'll use caddy to switch over to a new running backend service with readiness check.

As for scaling if I need it I can increase disk space for the app server or scale out horizontally/vertically. Don't need that yet so I'm waiting for more details in the future to decide how to handle that.

Re:Ask HN: Have you used SQLite as a primary database? by iampims on Monday April 25, 2022 @03:07PM (#31159271)
Which backend “owns” the sqlite database?

That’s what I haven’t seen mentioned anywhere, if the database is part of the application, how do you switch from one version to another without downtime.

Re:Ask HN: Have you used SQLite as a primary database? by srcreigh on Monday April 25, 2022 @08:11PM (#31162504)
The VM owns the database. Multiple backend service versions run on that same VM. Usually just one but 2 while migrating traffic to a new version.

I haven't figured out details regarding having 2 processes both writing and reading to the SQLite DB at once. It might just be fine. With a 1 minute request timeout I can just shut down the previous version after 2 minutes (should receive no new requests after 1 minute) and caddy will have sent requests to the new version for a while.

Not sure which types of errors I'll be seeing but the client may need to retry requests in some cases.

This is all just what I've planned but hopefully kinda makes sense.

Re:Ask HN: Have you used SQLite as a primary database? by simonw on Monday April 25, 2022 @01:14PM (#31157858)
I would expect the performance of SQLite for queries against an index to outperform MySQL and PostgreSQL in all cases - because SQLite eliminates the need for network overhead by essentially executing those queries directly as a C function call.

So no matter how optimized MySQL and PostgreSQL are, SQLite will run rings around them for basic SELECT queries.

Re:Ask HN: Have you used SQLite as a primary database? by mekster on Monday April 25, 2022 @03:45PM (#31159632)
Running against a unix socket doesn't seem to make that a selling point for SQLite.
Re:Ask HN: Have you used SQLite as a primary database? by simonw on Monday April 25, 2022 @04:39PM (#31160291)
There's still serialization and deserialization overhead there. I would expect SQLite to win in benchmarks against MySQL or PostgreSQL on a Unix socket for basic "select * from table where id = 5" calls, but I've not done the work to prove it myself.
Re:Ask HN: Have you used SQLite as a primary database? by robertlagrant on Monday April 25, 2022 @12:36PM (#31157270)
> I thought people complained how MySQL sucks and PostgreSQL rocks for being right and SQLite was nowhere near being right or performant

While "people" complaining is basically meaningless, I don't know why they'd be doing that about SQLite. It's used in most phones, all copies of Windows 10+, and countless other places.

Re:Ask HN: Have you used SQLite as a primary database? by djbusby on Monday April 25, 2022 @12:58PM (#31157618)
Chrome browser has loads of them in your profile directory.