News for hackers. Stuff that matters.
Re:Go and SQLite in the Cloud by markusw on Wednesday December 07, 2022 @11:22AM (#33896223)
Hey! I'm the author of the article. Saw a sudden influx of traffic from HN and found the post here.

Happy to answer any questions. Although SQLite is so dead-simple-but-awesome that you probably don't have any. :D

Anyway, hope you enjoy it and learn a thing or two.

Markus

Re:Go and SQLite in the Cloud by nileshtrivedi on Wednesday December 07, 2022 @01:00PM (#33897716)
I'd like to avoid writing boilerplate CRUD code. Is there an equivalent of PostgREST but for SQLite? Essentially, a standard binary would read the schema metadata, and generate standard CRUD APIs: https://postgrest.org/en/stable/api.html

The APIs can even support authentication and authorization with the help of JWT tokens. SQLite may not have row-level security, but even a convention (eg: if a row has user_id column, the JWT must have the same user_id value to get access to a row) would go a long way.

Re:Go and SQLite in the Cloud by iccananea on Wednesday December 07, 2022 @04:44PM (#33900725)
Not exactly what you want, but I've been using https://sqlc.dev to generate hydration code from SQL queries and loving it!
Re:Go and SQLite in the Cloud by LVB on Wednesday December 07, 2022 @02:20PM (#33898894)
I've not use this myself, but Ben Johnson's https://github.com/benbjohnson/postlite in front of SQlite might allow you to use PostgREST? I recall him saying on a podcast that his goal was to be able to point the large ecosystem of PG tools at SQlite.
Re:Go and SQLite in the Cloud by 0cf8612b2e1e on Wednesday December 07, 2022 @01:54PM (#33898504)
A long way off from PostgREST, but you might be able to hack together something sort of similar with Datasette [0] and the new JSON api. Not out of the box at all, but the potential is there to get a similar product.

[0]: https://datasette.io/

Re:Go and SQLite in the Cloud by nileshtrivedi on Wednesday December 07, 2022 @03:01PM (#33899435)
AFAIK, Datasette only offers read-only APIs.
Re:Go and SQLite in the Cloud by 0cf8612b2e1e on Wednesday December 07, 2022 @03:09PM (#33899553)
Re:Go and SQLite in the Cloud by mjay23 on Wednesday December 07, 2022 @07:29PM (#33902446)
Re:Go and SQLite in the Cloud by Cwizard on Wednesday December 07, 2022 @03:19PM (#33899679)
Have you tried pocketbase?
Re:Go and SQLite in the Cloud by yandrypozo on Wednesday December 07, 2022 @12:26PM (#33897233)
Nice article, what is your opinion on pocketbase.io which match your title really well, it would be great a follow-up article for LiteFS and pocketbase :)
Re:Go and SQLite in the Cloud by markusw on Wednesday December 07, 2022 @03:38PM (#33899896)
I don’t know pocketbase, will have a look.
Re:Go and SQLite in the Cloud by maxpert on Wednesday December 07, 2022 @05:11PM (#33901052)
Hey, I am author of Marmot (https://github.com/maxpert/marmot) would love to get in touch and explore a few things with you. I’ve been using Marmot in production in a while scaling couple of services, and since you mentioned LiteFS I would love to pick your brain, and explore couple of ideas with you. Multiple ways to get in touch including Github discussions or discord channel. Hope to see you around!
Re:Go and SQLite in the Cloud by markusw on Thursday December 08, 2022 @03:11AM (#33905546)
Feel free to reach out, [email protected]
Re:Go and SQLite in the Cloud by marktangotango on Wednesday December 07, 2022 @01:25PM (#33898056)
I suggest you run some simple load tests that do inserts, you'll find WAL is not sufficient for concurrency. Multiple reader, single writers handle this well though.
Re:Go and SQLite in the Cloud by sharps_xp on Wednesday December 07, 2022 @01:44PM (#33898346)
You can put all your writes in a queue to be processed by a finite set of writers. I think most CRUD applications can afford the delay and if not you can pair it with a write through cache.
Re:Go and SQLite in the Cloud by belmont_sup on Wednesday December 07, 2022 @03:56PM (#33900108)
It sounds simpler to setup postgres and never have to set up a queue (unless you’re using an in memory queue, and that has its drawbacks).

Although I definitely would like to use SQLite just for the cost savings for something. Litefs/litestream looks great.

Re:Go and SQLite in the Cloud by infamia on Wednesday December 07, 2022 @04:52PM (#33900816)
I was surprised at the amount of performance SQLite provided. I get about 700 inserts/sec with the WAL enabled sitting behind a Django app.

https://imgur.com/a/a3U41Zo

Without the WAL enabled, I get a around 400req/sec.

edit: clarity

Re:Go and SQLite in the Cloud by thomascgalvin on Wednesday December 07, 2022 @02:26PM (#33898983)
Concurrent writes are the bane of SQLite's existence, but for a one-person blog, you shouldn't run into any issues.

You would have trouble scaling up to fives of authors, though, which would be a deal breaker for any serious production app.

Re:Go and SQLite in the Cloud by markusw on Wednesday December 07, 2022 @02:35PM (#33899095)
@levelsio famously runs a dozen websites all backed by SQLite, with 200M reqs / month: https://twitter.com/levelsio/status/1520356430800617472

Expensify got 4 million request _per second_ out of a custom SQLite-based setup (on a huge machine, but still): https://blog.expensify.com/2018/01/08/scaling-sqlite-to-4m-q...

I would call those serious production apps.

Re:Go and SQLite in the Cloud by thomascgalvin on Wednesday December 07, 2022 @08:37PM (#33902976)
I specifically cited concurrent writes. SQLite is fantastic for read-only data. Those websites are using SQLite for what it's designed for. But, if they were trying to use SQLite to do even dozens of writes per second, it would crash and burn.
Re:Go and SQLite in the Cloud by markusw on Thursday December 08, 2022 @04:07AM (#33905877)
Well, that claim is fairly easy to test. So I did: https://gist.github.com/markuswustenberg/0030360d7d4679f2656...

Nothing fancy, just a small table that holds some blog posts with an ID, a title, some content, and a creation timestamp.

I ran the benchmark with and without WAL enabled on my Macbook Air (2020, M1) with some SSD drive inside. Results:

  $ make benchmark 
  go test -bench=.
  goos: darwin
  goarch: arm64
  pkg: sqlite
  BenchmarkWriteBlogPost/write_blog_post_without_WAL-8              6441     191735 ns/op
  BenchmarkWriteBlogPost/write_blog_post_with_WAL-8               102559      11205 ns/op
  PASS
  ok   sqlite 3.725s
That's around 89k writes per second in parallel on all available cores with WAL enabled. I know this is a trivial setup, but adjust to your liking. You'll find that SQLite probably doesn't crash and burn with dozens of writes.
Re:Go and SQLite in the Cloud by thomascgalvin on Thursday December 08, 2022 @08:04AM (#33907436)
This is single-threaded, which a production app would not be. That's when things get scary with SQLite.

Yes, I was incorrect when I said SQLite couldn't handle lots of writes quickly; what I should have said is that it can't handle lots of writes from multiple threads quickly.

Re:Go and SQLite in the Cloud by markusw on Thursday December 08, 2022 @01:46PM (#33911491)
Okay, fair enough. I thought my previous test was parallel as well, but I was mistaken.

Here's one for 64 parallel writes: https://gist.github.com/markuswustenberg/f35ab7e191137dca5f7...

  $ make benchmark
  go test -bench=.
  goos: darwin
  goarch: arm64
  pkg: sqlite
  BenchmarkWriteBlogPost/write_blog_post_without_WAL-8               100   16317415 ns/op
  BenchmarkWriteBlogPost/write_blog_post_with_WAL-8                 1196    1198679 ns/op
  BenchmarkWriteBlogPost/write_blog_post_with_WAL_and_Go_mutex-8             58455      17461 ns/op
  PASS
  ok   sqlite 4.557s
1e9/1198679 = 834 writes per second is still far from crash and burn territory when using just WAL mode.

Of course, it gets more interesting when there are also concurrent readers, as people are trying out elsewhere in the discussion. But the point still stands: it can handle way, waaaay better than "fives of authors" in a not-at-all scary way.

Re:Go and SQLite in the Cloud by marktangotango on Friday December 09, 2022 @11:03AM (#33922793)
You should duplicate this test against postgres inserts. I did the same thing for sqlite; in go using a mutex on inserts, and sqlite still out performs postgres on inserts (on the same machine).
Re:Go and SQLite in the Cloud by cy_hauser on Thursday December 08, 2022 @01:23PM (#33911222)
I'm not understanding this. Wouldn't the lower bound on performance be the single thread? Any worse performance and you queue the write requests to match the single thread performance.
Re:Go and SQLite in the Cloud by thomascgalvin on Thursday December 08, 2022 @04:02PM (#33913357)
No, because of SQLite's locking mechanism. It uses filesytem-level constructs to support concurrency, and this gets dicey, quickly.
Re:Go and SQLite in the Cloud by markusw on Thursday December 08, 2022 @04:49PM (#33914057)
As my test above shows, use a mutex for writes in Go directly and whatever locking performance problems SQLite exhibits disappear. I suspect because of the polling used in SQLite as described elsewhere on this page.
Re:Go and SQLite in the Cloud by mathewvp on Thursday December 08, 2022 @03:42AM (#33905744)
I have a very basic question regarding SQLite. How does it work in different languages? Is the SQLite server code completely implemented in Go when we use go and completely implemented in php when we use php? Because there is no actual server and it is just a file, I'm wondering who does the ACID compliancy and query executions and all the other things needed for a db server and how it is done.
Re:Go and SQLite in the Cloud by markusw on Thursday December 08, 2022 @04:11AM (#33905899)
The implementation used in this article is actually in C (which is the official, extremely well-tested one), with bindings to the Go language through something called CGo. There's no "server", it's all just code run in your application directly, so think of it more as a library that can access a special database file on your disk.

I don't know how it's done in PHP, I would think it's similar.

Re:Go and SQLite in the Cloud by longcommonname on Wednesday December 07, 2022 @11:58AM (#33896819)
Would you provide a summary of how you noticed the increased traffic and how you found this post?
Re:Go and SQLite in the Cloud by markusw on Wednesday December 07, 2022 @12:25PM (#33897219)
Saw the request spike, searched HN for "sqlite".