News for hackers. Stuff that matters.
Re:SQLite Is Serverless by geokon on Sunday January 26, 2020 @05:38AM (#22151436)
a bit tangential, but when do you move form using in-application data structures (maps, trees, vector/arrays) to using a database? Is it basically when the data doesn't fit in memory? I've been programming for almost a decade and I've never come across needing a database... (for context, it's ten years without anything web related) I'm interested in them and I'd love to learn SQL but I can't even think of a use case outside of managing user in some web based application
Re:SQLite Is Serverless by closeparen on Sunday January 26, 2020 @06:51AM (#22151637)
SQL queries tend to be much smaller than their equivalent data-structure traversal procedures. This can be beneficial even when you still want the data in your process's address space, hence embedded database engines like sqlite. Libraries like Linq can also provide the same expressive power over a programming language's native objects and collections.

As to why you'd want a separate DB server process: long lived mutable structures tend to drift into unexpected states, which is why the standard computer troubleshooting procedure since forever is to restart. Database management systems specialize in not having that problem. You deploy one that's widely used and hardened over many years, change it infrequently, and operate it with great care. Or pay a cloud provider to do that.

Then everything else gets to outsource the burden of persistence to it, and the vast majority of the workloads you develop and operate are stateless. These are drastically more convenient and more resilient to mistakes. They're effectively "restarted" with each API request, and you can stand them up, tear them down, migrate them to new machines, etc. as much as you want without fear of data loss.

Re:SQLite Is Serverless by FpUser on Sunday January 26, 2020 @11:32AM (#22152900)
"long lived mutable structures tend to drift into unexpected states"

Why are those "tend to drift" ? As an example I wrote sort of like game server for one of my applications. Internally it has those exact forever lived mutable structures. I've never observed it to drift into any unpredictable state. Works like a charm and running for many month. I only reboot it when I need to update it to a new version.

The only catch here is that all data fit into RAM. With the amount of RAM modern computers can be stuffed with I do not really see if my server would ever run out of it.

Of course it backed up by database but it merely serves as a persistence layer for this particular application.

Re:SQLite Is Serverless by dragontamer on Sunday January 26, 2020 @07:51PM (#22156000)
Consider why 3rd normal form exists to begin with.

In the real world, let's say you have a shipping address and a billing address for a customer, and they are usually (but not always) the same.

Eventually, a customer moves, changing both of their addresses. But the user forgets to change their billing address with their delivery address.

A proper database would have a 'billing address is same as delivery address' logic, following the principle of DRY (don't repeat yourself).

----------

There are lots of examples here of what can go wrong when you repeat yourself in a database application. The user may have an error when repeating themselves over the dataset (delivery address is correct, but zip code on billing address has a typo).

Dealing with these issues at scale, with hundreds of thousands of customers, is certainly a problem. Normal forms can formalize these issues and help the business owner avoid the problems.

Where do you verify the existence of zip codes and cities? Where do you check for typos? How do you prevent contradictions on the submitted information?

Your human customers will make many mistakes. Your logic must hold up even in the presence of faulty data.

Re:SQLite Is Serverless by FpUser on Monday January 27, 2020 @02:12PM (#22162693)
"A proper database would have a 'billing address is same as delivery address logic"

The database does not have logic like this. It has to be implemented by stored procedure. When I have application server all such logic (if applicable) is handled by code in much more performant way. No data goes to a database directly. Everything passes through the app server along with the validation data transformation etc, etc. As already said the database in this particular case is nothing more but persistence layer.

Again we can put all kind of theoretical speculations but as I already said, my particular server does not have data drifting to some faulty states.

Re:SQLite Is Serverless by dragontamer on Monday January 27, 2020 @05:54PM (#22164722)
>> "A proper database would have a 'billing address is same as delivery address logic"

> The database does not have logic like this.

Of course it wouldn't have 'logic', databases are just stores of data.

You'd have one 'address' table, with probably a int-primary/surrogate key. Then the 'delivery' and 'billing' address would be an int, pointing to the address table.

Furthermore, the billing and delivery address would be foreign keys, so the internal database logic would keep the tables in sync with no application code required.

With the data organized in this manner, the application code becomes logic free and braindead easy to write. Or at least, corner cases become easier to handle and more explicit. (Say two customers share the same address, do you allow repeats in the address table? Or do you allow customers to tie address information together? Either way, your decision rests on how you define the primary key)

> When I have application server all such logic (if applicable) is handled by code in much more performant way.

The most performant way is no logic at all. A proper database removes a lot of checks, the storage format itself naturally creates logic free code.

Some application logic is necessary of course. But you can minimize the logic needed by thinking about data layout.

Re:SQLite Is Serverless by geokon on Sunday January 26, 2020 @10:20AM (#22152430)
hmm, it seems to map to my workflow with Clojure lately. But there I'm massaging and generating new data sets (even though it's statless) but I can see that if you have a very settled data format then this would work in a language agnostic data-interface kind of way. I could see like measurement/time series data being stored in SQL and accessed that way would be much cleaner than opening and closing CSV files for all sorts of datasets
Re:SQLite Is Serverless by wpietri on Sunday January 26, 2020 @11:40AM (#22152960)
I'm sure people will give you the orthodox answer, so let me give you mine: when other things become more important than performance, ease of development, and code clarity.

Many years ago I happened to meet the creator of Prevayler, an open-source persistence framework that provided ACID guarantees and was thousands of times faster than a database as long as your data fit in RAM. I tried it out for a project and we loved it. Our hundreds and hundreds of unit tests ran in a few seconds. Our pages rendered in ~5 milliseconds. It was structured around a log of actions, so if you wanted to know how the data ended up like it did, every change was logged.

What people who haven't worked this way often miss is that a database doesn't make things simpler, it just makes certain things easier. Once you add a traditional database to your project, you're importing a million lines of mysterious code into your project, and demanding to pay a serialization/deserialization penalty any time your code wants to look at data. When it works, it can be swell, but when you have a problem, suddenly things can get hairy. Database performance optimization is a murky art in a way that just isn't true if all your data is right there in RAM.

Prevayler of course didn't catch on. It was just too weird for most people, who had grown up on databases and for whom data structures were something that they hadn't really thought about since their last CS exam. But I sometimes dream of the world where it did catch on. At the time, fitting in RAM was a big limitation. But now I can get an off-the-shelf desktop with 768 GB of RAM, and Amazon's servers go up to 24 TB of RAM. If you're going past working sets at those levels, traditional databases have anyhow fallen out of favor of big data tools. But it would have been a much better fit for today's world of microservices and distributed systems.

Re:SQLite Is Serverless by notduncansmith on Sunday January 26, 2020 @12:58PM (#22153502)
This is exactly the style in which I build applications these days. Most of the time I just describe it as “hella caching” but it is a different paradigm from treating the database as the primary state engine. The speed and simplicity of working on in-memory structures are great. When you outgrow a single server’s RAM capacity, you can use Kafka or another durable message queue as your application’s WAL and shard your data across multiple servers.
Re:SQLite Is Serverless by wpietri on Sunday January 26, 2020 @07:00PM (#22155778)
Have you written up the systems you've built? I'd love to read more about the practical details. Feel free to email me or DM me on Twitter if that's better.
Re:SQLite Is Serverless by notduncansmith on Monday January 27, 2020 @01:50AM (#22157314)
I haven’t written up any of the production work I’ve done in this vein, but here’s a demo application I built as a hiring challenge (apologies for the broken demo link, it was hosted by the now-defunct Hyper.sh): https://github.com/notduncansmith/agree/blob/master/README.m...

Given no firm deadline, I timeboxed to 12 hours so it’s not fully fleshed-out but I like to think it illustrates the concept well.

Re:SQLite Is Serverless by wpietri on Monday January 27, 2020 @02:39PM (#22162951)
Ah, that's cool! I like your writeup; it makes the advantages of your approach clear. I hope you got the job!
Re:SQLite Is Serverless by notduncansmith on Monday January 27, 2020 @07:15PM (#22165200)
Thanks! I did, though now working at another co and spending my free time generalizing this into a library that makes this paradigm easier to adopt (basically Redux-ifying your backend but with end-to-end encryption on the stored event logs). The initial version should be ready to publish in the next few weeks :)
Re:SQLite Is Serverless by wpietri on Tuesday January 28, 2020 @01:37PM (#22172580)
If you remember, please email me or contact me on Twitter when it comes out! I'd love to check it out. And I'll definitely pass it along to the Prevayler community, who I'm sure will be tickled.
Re:SQLite Is Serverless by adamcharnock on Sunday January 26, 2020 @06:42PM (#22155690)
Prevayler sounds a lot like Redis, unless I’m missing something.

I’m a big fan of Redis, but I was bitten many years ago when I tried to use it as a replacement for an RDBMS. There were two reasons for this: 1) lack of development libraries and operational tools, and 2) lack of data integrity checks.

1 has changed these days, but 2 is still very much the case (and rightly so, IMHO). Perhaps Prevayler had this?

But yep, it was fast at a time when our competition’s software was slow and clunky. It made a difference.

Re:SQLite Is Serverless by wpietri on Sunday January 26, 2020 @06:59PM (#22155771)
Not quite. Redis is still an external database. Prevayler was much simpler, just a library.

With Prevayler, all data is kept in RAM, reachable from one root object, which Prevayler holds. All changes to the data model must be expressed as command objects. Each object is handed to Prevayler, which serializes it to a log and then executes it. Once in a while, you can snapshot the data out and start a new log. If there's a crash, you just load the latest snapshot and replay the log.

You got exactly as much data integrity as you wrote into your objects and your commands. Which without much work could be quite a bit, because you get a lot of data integrity by not writing things. E.g., if a kind of object should never be deleted, you just don't write any deletion code. If, say, account balances should never be changed directly, but only as part of properly structured credits and debits, then you just write your code like that.

Most databases, Redis I think included, are made for arbitrary operations on data. Developers add integrity and security later, hopefully. And that is often duplicative of the code base, so that one ends up having integrity checks both in the code and in the database.

I should say that made a lot of sense for the era databases came out of. Databases were a huge step forward in the 1970s and 1980s. My dad was a developer in that era and it was a big relief not to have to get a bunch of programmers to all follow the same conventions for exactly which record was stored in exactly which spot on their precious and expensive disk drives. Not having to know the minutia of the hardware let a lot of people just get in there and build business reports. But if we were starting fresh today, I don't think we would do anything like a SQL database. Redis was definitely a step away from that era, and I look forward to many more.

Re:SQLite Is Serverless by solarkraft on Monday January 27, 2020 @06:42PM (#22165037)
If I didn't need the log, would I need to use Prevayler at all? It sounds to me like a plain old "keep objects in RAM", which is what most programming languages already natively do.
Re:SQLite Is Serverless by wpietri on Monday January 27, 2020 @07:18PM (#22165216)
What Prevayler got you was the ACID guarantees: https://en.wikipedia.org/wiki/ACID

So if you didn't need persistence, you certainly didn't need it. Ditto if data integrity wasn't important.

It would also have made distribution much easier. Since each mutation was already serialized to disk before execution, you could also send it over the wire to read-only replicas and hot spares for the master.

Re:SQLite Is Serverless by bob1029 on Sunday January 26, 2020 @05:55AM (#22151495)
I have found that you can go for a disturbingly-long period of time using primitive schemes like LINQ<->Objects<->JSON to persist your business data before things start to get hairy. Personally, I would say 10 megs of persisted data is about the upper limit before I am going to start reaching for SQLite. If you start to get clever with schemes like one file per serialized entity, you could potentially avoid using a 3rd party database indefinitely. I have found that the technical cost of using SQLite from the start is so low that I just start out using it by default even if the file will never exceed 1MB.
Re:SQLite Is Serverless by solarkraft on Monday January 27, 2020 @06:44PM (#22165053)
> If you start to get clever with schemes like one file per serialized entity, you could potentially avoid using a 3rd party database indefinitely

Most file systems will start to get slow at some point with too many files in a folder.

Re:SQLite Is Serverless by geokon on Sunday January 26, 2020 @06:10AM (#22151545)
will you store large objects like images or audio file directly in SQL?
Re:SQLite Is Serverless by bob1029 on Sunday January 26, 2020 @08:32AM (#22151977)
In most cases I will. Pulling a blob out of a row is a lot faster than opening an additional file handle. There aren't really any downsides to this either unless you are running table scans. SQLite does support indexes (even full-text) and they do work miracles so do use them when necessary.

I would go so far as to argue that SQLite could be used to store all of the assets for any large piece of software (I.e. a AAA game). This would probably wind up faster and more reliable than most alternative solutions out there today.

Re:SQLite Is Serverless by bsenftner on Sunday January 26, 2020 @10:30AM (#22152488)
Can confirm SQlite is used in AAA game engines as well as VFX production pipelines. I know because I put it there.
Re:SQLite Is Serverless by solarkraft on Monday January 27, 2020 @06:46PM (#22165056)
So - is SQlite a good file system? Should I just use it for everything?
Re:SQLite Is Serverless by bob1029 on Monday January 27, 2020 @07:36PM (#22165320)
I think hard drive manufacturers should just embed SQLite directly into their devices and optimize the SQLite VFS implementation to be flash aware at the controller-level. Who needs a file system when your disks speak SQL?
Re:SQLite Is Serverless by nomadluap on Monday January 27, 2020 @09:20AM (#22159425)
SQLite has a page measuring performance of internal blobs vs external files: https://www.sqlite.org/intern-v-extern-blob.html
Re:SQLite Is Serverless by d0mine on Sunday January 26, 2020 @05:43AM (#22151461)
Anytime you want persistency: sqlite is a replacement for fopen() https://www.sqlite.org/whentouse.html
Re:SQLite Is Serverless by dragontamer on Sunday January 26, 2020 @05:41AM (#22151447)
> when do you move form using in-application data structures (maps, trees, vector/arrays) to using a database?

The killer feature of databases, including no-sql or older ones like BerkeleyDB, is multiprocess synchronization.

If two programs (or two copies of the same program) need to coordinate their data, a database is often far easier to use than writing your own transaction layer through files, mutexes, and other primitives.

Web applications, such as forums or discussion boards, have many users and processes adding data simultaneously. That's why databases are so popular in web backends.

Re:SQLite Is Serverless by manigandham on Sunday January 26, 2020 @05:40AM (#22151442)
Persistence, portability, and durability. SQLite is an actual file on disk, can be easily read by multiple programs/languages, and has write-ahead logging to keep your data safe from crashes.
Re:SQLite Is Serverless by threatripper on Sunday January 26, 2020 @06:04AM (#22151517)
Is all your data on disk read-only and the data in RAM can be forgotten when your application finishes/closes? Then you don't need any of this.

If your state consists of a single data structure you could write it to disk into a new file and move-replace the old file. This works as long as you have a single instance of your application.

For anything more complex SQLite is a good way to keep your data in a consistent state. If you store it on a local drive, the performance is stellar.

Thinking about going multi-process or multi-user? SQLite can still give you an easy head-start because it performs OK in most situations with a database stored on a network drive.

Re:SQLite Is Serverless by typon on Sunday January 26, 2020 @05:43AM (#22151457)
Any medium size or greater application either uses SQL or recreates a shitty version of it. You just can't write complex code without relations.
Re:SQLite Is Serverless by shakna on Sunday January 26, 2020 @05:45AM (#22151464)
Structured transactions between applications is one place.

You can get by with files, but they're slower. A DB is the right choice.

Structured data anytime you're hitting multiuser - web, networked games, collab programs, live maps, etc.

Anything that's massively stateful. A DB hands you a lot of guarantees. The filesystem has some of them, but is much slower.

Re:SQLite Is Serverless by JyB on Sunday January 26, 2020 @11:58AM (#22153102)
That's a weird question, I don't really understand.

When you want to persist data beyond the lifespan of your process?

Re:SQLite Is Serverless by jtms on Sunday January 26, 2020 @12:06PM (#22153149)
How do you persist state across a restart of the app or device? Do you dump state to a file? Do you not need to persist state ever in 10 years of software development?
Re:SQLite Is Serverless by bernawil on Sunday January 26, 2020 @10:52AM (#22152626)
> when do you move from using in-application data structures?

as soon as you need any of these:

* a relational model -because you need to model your data that way or you are required to because someone wants to consume it with tableau-.

* concurrently read/read data between 1+ instances.

* a standard way of doing backups.

moving from that to a database would probably equal a rewrite.