News for hackers. Stuff that matters.
Re:Anko SQLite, a library to simplify working with SQLite on... by le-mark on Thursday September 21, 2017 @10:20AM (#15303662)
SQLite is such a fantastic database. I've always wondered, is anyone using it at scale in a client server application? How do people handle syncing online/offline in mobile apps?
Re:Anko SQLite, a library to simplify working with SQLite on... by dhd415 on Thursday September 21, 2017 @10:37AM (#15303840)
I have but I'd say that SQLite is not intended for use in that scenario. It's an in-process library for persisting data to disk in a generally relational format with a SQL interface. It starts to degrade under highly concurrent read and write workloads that you can experience in client-server applications. At that point, a typical RDBMS with more robust concurrency support starts to be a better choice. I experienced that when using SQLite and we eventually moved the application to a full RDBMS which was more complex but also performed and scaled much better.

Note that this is very much in line with their recommendations from the docs (http://sqlite.org/whentouse.html):

>>SQLite is not directly comparable to client/server SQL database engines such as MySQL, Oracle, PostgreSQL, or SQL Server since SQLite is trying to solve a different problem.

Client/server SQL database engines strive to implement a shared repository of enterprise data. They emphasize scalability, concurrency, centralization, and control. SQLite strives to provide local data storage for individual applications and devices. SQLite emphasizes economy, efficiency, reliability, independence, and simplicity.

SQLite does not compete with client/server databases. SQLite competes with fopen().<<

It's worth noting that when considered as competition to fopen(), SQLite is quite good.

Re:Anko SQLite, a library to simplify working with SQLite on... by mwotton on Thursday September 21, 2017 @02:50PM (#15306323)
I used sqlite as a backend for scraping 160 million websites a day. It works extraordinarily well if you hold it right - in my case, that meant devoting a database file to each thread. (Arguably in this context, it's competing with flat files, but it does a really good job there.)
Re:Anko SQLite, a library to simplify working with SQLite on... by tiffanyh on Thursday September 21, 2017 @11:25AM (#15304369)
Fossil-SCM.

I've always wondered why the creator of SQLite (who's also the creator of Fossil SCM) will state that SQLite is not meant for client/server scenarios ... yet his Fossil SCM (which is client/server) uses SQLite.

https://www.fossil-scm.org

Re:Anko SQLite, a library to simplify working with SQLite on... by SQLite on Thursday September 21, 2017 @11:51AM (#15304675)
Fossil is the server in this case, not SQLite.

It is fine to use SQLite as the local storage in the implementation of some custom server like Fossil, as there is a separate server (Fossil) that sits in between the client and the data. In fact, SQLite excels at this and usually works better than traditional client/server databases. The scenario you want to avoid is where the client attempts to access SQLite data directly across the network, with no intermediary server. The proverb is: "Always send your query to the data, not the data to the query."

It will work to access an SQLite database across a network filesystem. Just remember that the amount of information that flows between the SQL engine and the storage medium is much greater than the amount of information that flows between the SQL engine and the application. So if the storage is separated by from the application by a relatively slow network, you want to position that network on the low-bandwidth link to obtain the best performance. That means that the SQL engine needs to be on the same side of the network as the storage - hence a client/server database. If you use SQLite to access a network file, the SQL engine will be on the application side of the network and a lot more content will need to traverse the relatively slow network link.

And so, while SQLite will work in a client/server situation, you might be disappointed by the resulting network load and/or performance of the system.

In the case of Fossil, Fossil itself is the "client" for SQLite and it is on the same machine as the data, which is exactly what you want with SQLite. The client of the Fossil server might well be across a network, but that does not matter to SQLite.

Re:Anko SQLite, a library to simplify working with SQLite on... by tiffanyh on Thursday September 21, 2017 @02:36PM (#15306235)
@SQLite

So how is what you described above architecturally different than a web application architecture?

Which is a use case I thought you did not recommend.

Re:Anko SQLite, a library to simplify working with SQLite on... by mritun on Thursday September 21, 2017 @11:36AM (#15304505)
SQLite does not have a client-server interface. That’s ok because the creator says it is supposed to be an embedded library that gives a better fopen interface. That is, you don’t need to invent file formats (think JSON, YAML, etc). SQLite is not just a database, it’s a persistence layer that happens to have SQL interface.

It places no restrictions whatsoever on the applications that uses it. That will be pointless. Fossil-SCM uses SQLite and is itself client server.

Re:Anko SQLite, a library to simplify working with SQLite on... by bensummers on Thursday September 21, 2017 @11:33AM (#15304469)
fossil on the server gets low numbers of writes, and not many reads. And being a distributed system, most of the SQLite action is on the client.
Re:Anko SQLite, a library to simplify working with SQLite on... by asdfjklasdfjkl on Thursday September 21, 2017 @11:39AM (#15304542)
I'm running into a weird case where even within an application, concurrency has become relevant because of parallel processing that the application wants to do over the data stored in the database.

But, our desire for both simplicity and obscenely low latency means that SQLite is still the better choice for us at the moment. In the future, though, I can see robust concurrency support being important even in single application use-cases.

Re:Anko SQLite, a library to simplify working with SQLite on... by eropple on Thursday September 21, 2017 @11:49AM (#15304639)
If you're in-process, though, you can just lock when you need exclusive access to the datastore.
Re:Anko SQLite, a library to simplify working with SQLite on... by asdfjklasdfjkl on Thursday September 21, 2017 @12:14PM (#15304939)
Ideally, I'd have several separate threads each with fine grained (table-level, maybe even row-level with deferred index updating) control on reading and writing. I admit that I haven't gone sufficiently down this path to know exactly what I'd want though, as it isn't the top priority right now. (We are "fast enough" without concurrency, ... for now.)
Re:Anko SQLite, a library to simplify working with SQLite on... by evanspa on Thursday September 21, 2017 @11:42AM (#15304570)
In my strength-tracking iOS app Riker, I use SQLite directly (not CoreData) as my local data store, and support full offline-mode. The backend is Postgres.

In the app, for each relation (e.g., a "workout set"), I have 2 tables: a master and a scratchpad. When a user saves a set, a row is written to the scratchpad table. When the user syncs it with the server, a row is written to the master table and deleted from the scratchpad table. When the user wants to edit the record, I first copy it down from the master table to the scratchpad table. All local editing impacts the scratchpad row. When the user wants to sync, only if a 200 response is returned will I copy-up the scratchpad row to the master row. If the set was edited on another device and the local copy is out-of-sync, the server would have responded with a 409 (http conflict code), and the body would contain the server copy, which is then written to the master table. The user can then figure how they want to merge the scratchpad row and the master row.

Anyway...trying to do all this with CoreData would have been a pain, so I use SQLite directly, and works great.

Or to summarize, I handle offline mode, syncing and conflict detection using "updated_at" timestamp columns along with logic in my REST API to returned appropriate HTTP status codes, interpret "if-unmodified-since" headers, etc.

https://itunes.apple.com/us/app/riker/id1196920730?mt=8

Riker on Android is currently in-progress...

Re:Anko SQLite, a library to simplify working with SQLite on... by lungureanu on Friday September 22, 2017 @02:18AM (#15309797)
I find it difficult to get full offline-mode + sync working correctly. Don't know if you have duplicates in the remote db but I think your approach has a problem: the app send the data (from scratchpad table) to server, the server saves it in DB but the connection drops on device (ex bad connection, user turn off WIFI/3G while syncing) so the app will never get a 200 response. In this case your new data was saved on server but it remains on scratchpad table. On next sync the data will be uploaded as being new: this will cause duplicates.
Re:Anko SQLite, a library to simplify working with SQLite on... by evanspa on Friday September 22, 2017 @05:12AM (#15310451)
You are right. The way I solved for this scenario is that when a record is first created on the device, a GUID is created for it (and stored using another column of course). When POSTing new records to the server for syncing, the server will check the GUID and see if the record already exists in its database, and if so, can ignore it (so the duplicate isn't written).

But yes, you're right overall - full offline mode w/syncing, etc is a big pain :)

Re:Anko SQLite, a library to simplify working with SQLite on... by russell_h on Thursday September 21, 2017 @10:38AM (#15303844)
OpenStack Swift (it's S3-like service) uses SQLite to store account and bucket metadata.
Re:Anko SQLite, a library to simplify working with SQLite on... by edwinyzh on Thursday September 21, 2017 @11:42AM (#15304573)
For question #1, an approach is to build a full-fledged Web/REST server wrapping around SQLite: https://github.com/synopse/mORMot

The performance is high!

Re:Anko SQLite, a library to simplify working with SQLite on... by alttab on Thursday September 21, 2017 @10:34AM (#15303815)
Spiceworks has a network management app that runs on a rails server on your network and uses sqlite to store everything and run the multi-tenant help desk. I was always surprised how well it worked.
Re:Anko SQLite, a library to simplify working with SQLite on... by bluedino on Thursday September 21, 2017 @10:23AM (#15303685)
One problem we've ran into with it, is tables with > 400 columns.
Re:Anko SQLite, a library to simplify working with SQLite on... by striking on Thursday September 21, 2017 @10:53AM (#15304013)
Sounds to me like you're using RDBMS wrong.
Re:Anko SQLite, a library to simplify working with SQLite on... by tyfon on Thursday September 21, 2017 @11:21AM (#15304332)
Not that it necessarily relates to SQLlite, but I've seen this _many many_ times in real world databases in the banking industry.

Personally I don't like it and I am luckily not in IT, but as an analyst I have seen many weird database structures. One can only imagine the business requirements that gave birth to some of them.

To quote a solutions architect to the head of marketing in a previous job: "You asked for a monster, you got a monster."

Re:Anko SQLite, a library to simplify working with SQLite on... by danso on Thursday September 21, 2017 @10:54AM (#15304022)
Is there something about SQLite's architecture that would make such a schema less problematic in MySQL/PgSQL/etc?
Re:Anko SQLite, a library to simplify working with SQLite on... by revelation on Thursday September 21, 2017 @10:27AM (#15303733)
Well, don't do that, normalize your scheme.
Re:Anko SQLite, a library to simplify working with SQLite on... by phn on Thursday September 21, 2017 @12:03PM (#15304814)
When your application grows, eventually you need to de-normalize data if you want to keep having reasonable performance for some operations.

You can reach a point where it's not feasible to have everything done by joins.

EDIT: I mean in relational DBs in general. In this case, 400+ columns probably mean SQLite may not be the engine to use, regardless of schema :)

Re:Anko SQLite, a library to simplify working with SQLite on... by Diederich on Thursday September 21, 2017 @10:26AM (#15303722)
Can you expand on that?
Re:Anko SQLite, a library to simplify working with SQLite on... by andrewguenther on Thursday September 21, 2017 @11:34AM (#15304472)
Isn't 400 columns expanded enough?
Re:Anko SQLite, a library to simplify working with SQLite on... by bluedino on Thursday September 21, 2017 @01:03PM (#15305411)
It's simply creating a local cache of a remote database that runs on Oracle.
Re:Anko SQLite, a library to simplify working with SQLite on... by Diederich on Friday September 22, 2017 @10:47AM (#15312641)
I mean...what kinds of problems did you run into? Thanks.
Re:Anko SQLite, a library to simplify working with SQLite on... by pier25 on Thursday September 21, 2017 @11:44AM (#15304603)
Check out Realm:

https://realm.io/

Re:Anko SQLite, a library to simplify working with SQLite on... by hasenj on Thursday September 21, 2017 @10:41AM (#15303873)
I really don't understand why majority of websites use mysql or even postgres for that matter. The vast majority of websites would run perfectly fine on sqlite.

Not only would you get rid of network latency, you would also vastly reduce your dependencies.

I have a side project to try to see if I can build a minimal micro-blogging type server application on sqlite and try to test how many requests it can handle per second (reads and writes).

I have a hunch it will be able to handle quite a lot. Specially if the server code is written in a fast language that compiles to native code (e.g. D) with some caching of the most commonly requested items in memory.

Re:Anko SQLite, a library to simplify working with SQLite on... by pmelendez on Thursday September 21, 2017 @10:48AM (#15303950)
Concurrent writes is a problem since the locking is at database level[1]. For a small to medium-low website might be OK depending on the writes volume.

[1] https://www.sqlite.org/lockingv3.html

Re:Anko SQLite, a library to simplify working with SQLite on... by cl0wnshoes on Thursday September 21, 2017 @11:38AM (#15304523)
I'd be willing to bet that 99% of the sites/apps on the web have low to medium writes and reads. Developers like to plan on needing Facebook and Uber scale for their new task list and subscription box services though. SQLite benchmarks are impressive, they beat out commercial RDMS in a lot of comparisons.
Re:Anko SQLite, a library to simplify working with SQLite on... by masklinn on Thursday September 21, 2017 @11:06AM (#15304155)
As the header of your page denotes, things got significantly improved years ago with the addition of a WAL as the default mode rather than an exclusive write lock.
Re:Anko SQLite, a library to simplify working with SQLite on... by k__ on Thursday September 21, 2017 @01:39PM (#15305743)
Also, I think most websites don't even need constant access to the DB, because the data doesn't change often.

Compile a static site from SQLite when someone enters new data, push it somewhere to be accessible and be done with it.

Re:Anko SQLite, a library to simplify working with SQLite on... by icebraining on Thursday September 21, 2017 @11:26AM (#15304394)
Network latency is only really a problem if you host your db on another machine. An Unix socket has something like 1ns. As for dependencies, the package manager takes care of that.
Re:Anko SQLite, a library to simplify working with SQLite on... by kps on Thursday September 21, 2017 @12:15PM (#15304949)

  > An Unix socket has something like 1ns. 
That seems implausible, given that you're lucky to get a context switch down to 1µs if the cache gods smile on you.
Re:Anko SQLite, a library to simplify working with SQLite on... by icebraining on Thursday September 21, 2017 @12:26PM (#15305058)
Sorry, I meant 1 microsecond.
Re:Anko SQLite, a library to simplify working with SQLite on... by hasenj on Thursday September 21, 2017 @08:32PM (#15308466)
With the complicated aws-based deployment schemes people are using these days, what's the chance that the db is on the same machine?
Re:Anko SQLite, a library to simplify working with SQLite on... by akvadrako on Thursday September 21, 2017 @10:45AM (#15303926)
SQLite does not work smoothly in many languages (like python) in regards to concurrent access. Without a controlling process (or thread) it's synchronisation possibilities are limited, so it can never scale too far.

Nonetheless I think it's a great idea to build your own single-threaded layer over SQLite and scale that, using a message bus or whatever.

But it's _not_ a drop-in replacement for MySQL based apps.

Re:Anko SQLite, a library to simplify working with SQLite on... by hasenj on Thursday September 21, 2017 @08:30PM (#15308460)
Yea my hunch/hypothesis does not apply to slow languages like python. That's why I said a fast language that's compiled to native binaries. For example, D, Swift, or Go.
Re:Anko SQLite, a library to simplify working with SQLite on... by ageofwant on Thursday September 21, 2017 @11:01AM (#15304104)
SQLAlchemy with transactions on SQLite should get you very close, probably close enough.
Re:Anko SQLite, a library to simplify working with SQLite on... by infecto on Thursday September 21, 2017 @11:23AM (#15304352)
Why get very close when I can just spin up a single micro RDS instance of MySQL or Postgres and not have to worry about it?
Re:Anko SQLite, a library to simplify working with SQLite on... by marktangotango on Thursday September 21, 2017 @11:55AM (#15304726)
Whoa! That's nearly $20 a month, if you're past the free tier, which you will be if you're not already. Edit that's t1.micro, t2 is $13.
Re:Anko SQLite, a library to simplify working with SQLite on... by infecto on Thursday September 21, 2017 @12:52PM (#15305313)
Not sure how other people do it but 1 micro RDS serves many projects for me. Well worth the money for me.
Re:Anko SQLite, a library to simplify working with SQLite on... by aianus on Thursday September 21, 2017 @11:14AM (#15304244)
It's only a few lines of terraform to spin up a mysql or pg RDS instance. It can't get any easier, so why would I jump through extra hoops to use sqlite?
Re:Anko SQLite, a library to simplify working with SQLite on... by cl0wnshoes on Thursday September 21, 2017 @11:33AM (#15304468)
Its free to host compared to other RDMS which aren't cheap on most IaaS, and spinning up an instance is as easy as deploying it alongside your code.
Re:Anko SQLite, a library to simplify working with SQLite on... by hasenj on Thursday September 21, 2017 @08:29PM (#15308455)
terraform itself is a monster I want to avoid ..

I want no dependencies .. and no dependency manager.

Re:Anko SQLite, a library to simplify working with SQLite on... by alvil on Thursday September 21, 2017 @10:53AM (#15304006)
Here are some comments from SQLite itself:

https://www.reddit.com/r/PHP/comments/59na74/sqlite_as_the_o...