34

Sorry for this question, I dont know if i've understood the concept, but SQLite is Serverless, this means the database in in a local machine, and it's stored in one file, this file is only accessible on one mode: if one client reads it, it's made only for reading mode for other clients, and if a client writes, then all clients have the write mode, so only in one mode at once! so imagine that i've made a django application, a blog for example; then how is this made using sqlite? since if a client enters to the blog he gots the reading mode to see the page and the blog entries, and if a registred client tries to add a comment then the file will be made as write mode, so how can sqlite handle this? so, does SQLite is here just like the BaseHTTPServer (the server shipped with django), for testing and learning purpose?

ItayB
  • 10,377
  • 9
  • 50
  • 77
Abdelouahab
  • 7,331
  • 11
  • 52
  • 82

7 Answers7

47

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.

spacediver
  • 1,483
  • 1
  • 11
  • 18
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • 1
    I use PostgreSQL and MySQL. I would prefer PostgreSQL for larger projects, mostly because of the transaction support, but MySQL is fine for smaller applications. – Rob Osborne Aug 03 '11 at 15:34
15

SQLite is not like BaseHTTPServer or anything basic like that. It's a fully featured embedded database. Quite fast too. Its SQL language might not have the most bells and whistles, but it's flexible enough. I haven't run into cases where I needed something it cannot do for the projects I was involved in (which aren't your typical web apps, truth be told).

Anyone that claims SQLite is good or bad for production without discussing the actual design is not telling you much. SQLite is pretty fast. In some cases, literally orders of magnitude faster than, say, Postgres, which comes up as a go-to alternative among Djangonauts. As someone pointed out, it also supports lots of concurrency. It's a matter of whether your app falls under the 'some cases' or not.

Now, there is one significant factor that has to be taken into account. SQLite is an in-process database. This is really important. If you are using something like gevent, you may run into edge cases where your app breaks. E.g., trying to do a transaction where you have a context switch in middle of it can possibly break the transaction in horrible ways. In other words, 'concurrency' really depends on your app, because SQLite is part of your app.

What you can't do with SQLite, though, in terms of scaling, is you can't make clusters of SQLite servers like you can with some of the other database engines, because it's in-process. Your app may or may not need to go to such lengths in terms of scaling, but my guess is that vast majority of apps out there don't anyway (wild guess).

On the other hand, being in-process means adding custom functions and aggregates to it is pretty trivial. I'm not sure if Django's ORM makes that any more difficult than it has to be, but you can come up with pretty good designs taking advantage of those features.

  • 1
    The concurrency claims are confused. Yes, theres a *degree* of concurrency, but its refering to multiple embedded app concurrency. This is not what we are talking about here. Django uses a *global lock* per page request, that means that it very quickly gets gummed up in lock contention once youve got more than a few simultaneous hits on a page. Theres a good reason the devs counsel strongly against using it in production. – Shayne Apr 26 '21 at 15:38
12

This issue in database theory is called concurrency and SQLite does support it in Windows versions > Win98 and elsewhere according to the FAQ:

http://www.sqlite.org/faq.html#q5

We are aware of no other embedded SQL database engine that supports as much concurrency as SQLite. SQLite allows multiple processes to have the database file open at once, and for multiple processes to read the database at once. When any process wants to write, it must lock the entire database file for the duration of its update. But that normally only takes a few milliseconds. Other processes just wait on the writer to finish then continue about their business. Other embedded SQL database engines typically only allow a single process to connect to the database at once.

Basically, do not worry about concurrency, any database worth its salt takes care of just fine. More information on as how SQLite3 manages this can be found here. You, as a developer, not a database designer, needn't care about it unless you are interested in the inner-workings.

Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
  • so it's only a few seconds problem? it's like time-sharing idea? – Abdelouahab Aug 02 '11 at 14:46
  • Those global locks pile up pretty quickly on a popular site. Trust me. Also the concurrenct thing is kind of beside the point. Thats a comparison with embedded databases. That type of concurrency isn't even in the ballpark of the sorts of concurency you find in an RDBMS like Postgres, MySQL , MSSQL or Oracle where you might have thousands of clients. We had a Postgres server ingesting terabytes of GIS data a day from thousands of clients at a govt job I worked in. Your not going to do that on an Sqlite db – Shayne Apr 26 '21 at 15:41
8

SQLite will only work effectively in production from some specific situations. It's quite easy to get MySQL or PostgreSQL up and running, even on Windows, and have a database that works in most situations.

The real problem is that SQLite3 isn't threaded in Django so only one PAGE view can happen at a time on your server, see this bug https://code.djangoproject.com/ticket/12118 Fixed

I don't use SQLite3 even in development.

EDIT: I keep getting downvoted here but the Django documentation itself recommended not using SQLite3 in Production at the time I wrote this answer. The documentation still contains the following caveat:

SQLite provides an excellent development alternative for applications that are predominantly read-only or require a smaller installation footprint.

If you do not have a small foot print/read-only Django instance, do NOT use SQLite3. Feel free to continue to downvote this answer.

Rob Osborne
  • 4,897
  • 4
  • 32
  • 43
  • so SQLite is only here for test purpose? – Abdelouahab Aug 02 '11 at 22:45
  • 4
    Sqlite is also good for teaching database concepts (since it's easy to set up) and for research (since it's source-code is small and simple). For each of these things, SQLite really shines. For heavy transaction loads, there are much better options. – SingleNegationElimination Aug 03 '11 at 01:16
  • 2
    Yes, test/development. Once you start building a more complex site you'll need a real database AND you'll probably need some raw SQL (I've built 3 Django sites and all three have needed some raw SQL for a couple of queries) – Rob Osborne Aug 03 '11 at 15:34
  • 3
    The statement "SQLite isn't really meant for production" is just false. Please refer to SQLite documentation. – Marcin Aug 14 '18 at 21:10
  • 1
    According to sqlite, it's good for their website which handles 400k hits a day, with some dynamic content and 200 queries per hit. So my understanding is that saying sqlite shouldn't be used for Django is mostly FUD and it's fine unless you handle a lot of traffic. If you're asking the question then sqlite is probably ok and potentially easier to work with and backup than a server based database. Mostly I trust sqlite docs more than django because most of their claims are backed up by real world tests. Does django provide numbers to support the claim? https://www.sqlite.org/whentouse.html – Josh Apr 03 '22 at 17:20
  • @Josh I wrote this comment more than a decade ago. As of 4.0, Django documentation is still saying "SQLite provides an excellent development alternative for applications that are predominantly read-only or require a smaller installation footprint." Please go argue with the Django project if you disagree. – Rob Osborne Apr 04 '22 at 18:21
  • 1
    @RobOsborne That's fair, though it's still relevant and people still ask this a lot. We can agree to disagree that sqlite isn't meant for production - Django's docs (to me) imply that it's fine unless you expect a lot of traffic. I would suggest also removing the threading bug, that was fixed in the tracker a while back: https://code.djangoproject.com/ticket/12118 – Josh Apr 08 '22 at 09:43
  • @Josh Updated with your suggestions. – Rob Osborne Apr 08 '22 at 17:16
4

It is not impossible to use Django with Sqlite as database in production, primarily depending on your website/webapp traffic and how hard you hit your db (alongside what kind of operations you perform on it i.e. reads/writes/etc). In fact, approaching end of 2019, I have used it in several low volume applications with less than 5k daily interactions (these are more common than you might think).

Simply put for the current state of tech , at the moment Sqlite-3 supports unlimited concurrent reads (or as far as your machine / workers can handle), BUT only a single process can write to it at any point in time. Bear in mind, a well designed query/ops to the db will last only miliseconds!

Coming from experience in using sqlite as the only db for simple non-routine (by non-routine i mean that a typical user would not be using this app on a daily basis year-round) production web app for overseas job matching that deal with ~5000 registered students (stats show consistently less than 2k requests per day that involves hitting the database during peak season - 40% write 60% read), I've had no problems whatsoever with timeouts/performance issues.

It really boils down to being pragmatic about the development and the URS (client spec). If it becomes the next unicorn , one can always migrate the SQLITE to another RDBMS. For instance, see David d C e Freitas's take on migration in Quick easy way to migrate SQLite3 to MySQL?

Additionally the SQLITE website uses sqlite db at its backend .. see below...

The SQLite website (https://www.sqlite.org/) uses SQLite itself, of course, and as of this writing (2015) it handles about 400K to 500K HTTP requests per day, about 15-20% of which are dynamic pages touching the database. Dynamic content uses about 200 SQL statements per webpage. This setup runs on a single VM that shares a physical server with 23 others and yet still keeps the load average below 0.1 most of the time.

Bear in mind that the above quote is of course mainly referring to read operations, so the values may not be a applicable for write-heavy sites.

The example I gave above on the job matching application I built using sqlite as db is quite write heavy if you've noticed the numbers ... on average, 40% are short lived write operations (i.e. form submissions, etc etc) but bear in mind my volume hitting the db is only 2k per day during peak season.

Then again, if you realize that your sqlite.db is causing alot of timeout and bad user experience (408 !!! on form submission...), especially with Django throwing the OperationalError: database is locked error. (and then they have to key in the whole thing again)...You can always increase the timeout in your settings.py as per django docs as a temporary solution while you prepare for migrating the db.

'OPTIONS': {
# ...
'timeout': 20,
# ...
}

Again, it all boils down to pragmatic development and facing reality that the site may not attract as much activity as hoped , and is prone to over-engineering from the get-go.

There are many times that going for a simple solution enables faster time to market , essentially, to quickly test waters , and of course, be prepared If the piranhas do come in swarms and then its time to upgrade to another RDBMS.

With Django's ORM, for most cases you dont need to touch your models.py during migration to other supported sql db. Be VERY mindfull though that Sqlite does not support some more advanced functions or even fields that its bigger cousins MYSQL and POSTGRES do.

aaronlhe
  • 1,062
  • 10
  • 18
1

Late to the party, but the question is still relavant as of mid 2018.

"Client" of a blog site is a different term that a "database client". SQLite documentation refers to a client as a process opening a database file. Such process, say a django app, may handle many web app clients ("users") simultaneously and it still is going to be just one client from the standpoint of SQLiite.

The important consideration for choosing SQLite over proper RDBMS is whether your architecture is comprised of more than one software component connecting to a database. In such case, using SQLite may be a major performance bottleneck due to the fact that each app needs to access the same DB file, possibly over a network.

If multiple apps(database clients) is not the case, SQLite is a great production choice in 99% of cases. The remaining 1% is apps using specific DB features, apps under enormous load, etc.

Know your architecture.

Marcin
  • 4,080
  • 1
  • 27
  • 54
1

The anwer to this question depends on the application that you want to deploy in production:

According to the how to use from the SQLite website, SQLite works great in production as the database engine for most website having low to medium traffic (which is to say, most websites).

They argue that the amount of web traffic that SQLite can handle depends on how heavily you use the database of your website. It is known that any site that gets fewer than 100K hits/day should work fine with SQLite. However, this 100K hits/day figure is a conservative estimate, not a hard upper bound.

In summary, SQLite might be a great choice for applications with fewer users and databases uses. Thus, use SQLite for website with fewer or medium interactions with the database and MySQL or PostgreSQL for website with higher interactions with the database.

Reference: sqlite.org

liedji
  • 739
  • 10
  • 11