Search code examples
djangosqlite

SQLite read/write concurrency in Django


SQLite is Server-less, the database is in a local machine in one file: if one client reads it's made reading mode for other clients, and if a client writes then all clients have the write mode, so only in one mode at once!

If I made a Django blog then how does this work using SQLite? If a client enters the blog he got the reading mode to see the blog entries, and a registered client tries to add a comment then the file will be made as write mode. How does SQLite handle this?

Is SQLite like BaseHTTPServer (the server shipped with Django) for testing and learning purposes only?


Solution

  • Different databases manage concurrency in different ways, but in sqlite, the method used is a global database-level lock. Only one thread or process can make changes to a sqlite database at a time; all other, concurrent processes will be forced to wait until the currently running process has finished.

    As your number of users grows; sqlite's simple locking strategy will lead to increasingly great lock contention, and you will need to migrate your data to another database, such as MySQL (Which can do row level locking, at least with InnoDB engine) or PostgreSQL (Which uses Multiversion Concurrency Control). If you anticipate that you will get a substantial number of users (on the level of say, more than 1 request per second for a good part of the day), you should migrate off of sqlite; and the sooner you do so, the easier it will be.