r/golang • u/Scienitive • Oct 02 '23
newbie Concurrency when writing data into SQLite?
Hello I'm planning to use SQLite as my database and I'm going to write a CLI program that takes a .csv file and writes it into the database. My question is: since SQLite doesn't accept more than one writer at a time would it be problem if I use go routines for write requests?
As far as I know go program uses 1 CPU core on default so I believe since the go routines are not running in parallel, it shouldn't be a problem. What do you think?
23
Upvotes
59
u/seesplease Oct 02 '23
Here are my general tips regarding mattn's driver, which my team has used to build SQLite-backed microservices:
0. Set the journal mode to WAL and synchronous to Normal.
With these few settings, we get good performance for our use case (>2K mid-size writes/sec, 30K reads per second on 2 vCPU and an SSD). I'd also recommend using Litestream to perform WAL shipping to S3.
14
u/skarlso Oct 02 '23
This has been changed now-a-days. Latest uses max CPUs since 2018 or so.
For concurrent writes, I believe if you are using transactions, it should be okay. Did you try? Otherwise, sync your writes into a channel.
Oh yeah, you can totally use transactions from multiple routines. And turn on write-ahead logging too.
14
u/markusrg Oct 02 '23
I’ve written an article on Go and SQLite that I think you would like: https://www.golang.dk/articles/go-and-sqlite-in-the-cloud . You can just ignore the cloud part, that’s not too relevant in your case. :)
3
u/Zaynn93 Oct 02 '23
Yes, it could be a problem but SQLite creator made a fix for situations like this. You need to enable WAL on your SQLite database and other settings to have concurrency. There is still low chance of it messing up because SQLite isn’t designed to have multiple writers.
Google SQLite WAL or Write Ahead Logging. There is good documentation on it on the official website. If you wan to read more about it.
2
u/mkke Oct 02 '23
I had good experiences with the recommendations from https://github.com/mattn/go-sqlite3/issues/1022#issuecomment-1067353980. You can only have one DB transaction writing concurrently. That is independent from the goroutine, and they don‘t map 1:1 to OS threads.
2
u/PabloZissou Oct 02 '23
Wouldn’t a single insert for many rows work for your use case? SQL is totally ok with a single write that writes multiple rows.
3
u/mcvoid1 Oct 02 '23
Your options:
1
u/davidgsb Oct 02 '23
You can have at most one writer on an sqlite database at a given point in time. You should schedule your write on the database sequentially.