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

21

u/tom-dixon Sep 10 '24

DBs handle concurrency and this is their main feature and their main problem. SQLite does not handle it and does not care.

That's false.

https://www.sqlite.org/draft/faq.html#q5

Q: Can multiple applications or multiple instances of the same application access a single database file at the same time?

A: Multiple processes can have the same database open at the same time. Multiple processes can be doing a SELECT at the same time. But only one process can be making changes to the database at any moment in time, however.

SQLite uses reader/writer locks to control access to the database.

...

However, client/server database engines (such as PostgreSQL, MySQL, or Oracle) usually support a higher level of concurrency and allow multiple processes to be writing to the same database at the same time. This is possible in a client/server database because there is always a single well-controlled server process available to coordinate access. If your application has a need for a lot of concurrency, then you should consider using a client/server database. But experience suggests that most applications need much less concurrency than their designers imagine.

They also have WAL-mode, which allows really fast operation when there's many concurrent readers and writers. Orders of magnitude faster than fopen() with manual locking.

https://www.sqlite.org/wal.html

2

u/keepthepace Sep 10 '24

OK, I remembered concurrent writes were not possible, but looks like it handles that more gracefully than I thought. Still I don't recall it was possible to have different applications write concurrently on the same file? Is it handled correctly? (And by that I just mean that a locking is happening automatically and that transactions wont leave the DB in a corrupt state?)

13

u/tom-dixon Sep 11 '24

Yes, the database file is locked during writes by default on every OS that supports it. It's borderline impossible to leave an SQLite database in a corrupt state, no matter what you do.

Their test suite includes tests for IO errors, power loss, file system damage, out-of-memory situations, hard drive errors, etc.

https://www.sqlite.org/testing.html

SQLite is used in the flight control system of the Airbus A350, and NASA uses it in on several space crafts. It's extremely robust and reliable.

6

u/casualops Sep 11 '24

Yes absolutely SQLite handles locking across process boundaries. So multiple processes can write to the same SQLite file and SQLite will take care of synchronization.