News for hackers. Stuff that matters.
Re:Why I love databases by marknadal on Sunday November 09, 2014 @04:54PM (#8581028)
I'm glad he loves databases, databases have been the bane of my existence.

However, the torment they have given me has also lead to a similar fascination - and now I'm writing my own database! So I've become very familiar with the topics he writes on, and they are very good points for anybody interested in the subject.

Why would I write my own database? Because databases are hard, and I am determined to make them easy (even if that means me sacrificing years of my life into doing all this crazy research). Check out http://github.com/amark/gun !

- CAP Theorem, he is correct, P cannot be sacrificed. GUN is AP with eventual consistency. The beauty of this, though, is that you can always build strong consistency out of eventual consistency (it just requires knowing X amount of peers in advance, and doing a trivial lock until you've heard back from all of them - in fact, I do this in one of the example apps) but you can never go from strong consistency down to an eventually consistent system.

- Distributed Systems, this is incredibly incredibly important. I cannot repeat this enough times, there should be no "master" or "single source of truth" in any database. If there is, you're going to have a nightmare of a time being woken up at 3am to fix it when it crashes (my personal experience with other databases). Why? Because single points of failures will fail, centralized systems suck. Solution: Distribute and Decentralize! We make this easy for you.

- Correctness vs Efficiency, as he says, Paxos is difficult - all of them, Raft, Quorum, leader election, etc. DO NOT USE THEM unless you are Google, Amazon, Walmart, or what not. Even then, do not use them. Instead, I've solved this challenging problem by developing a new Conflict Resolution system that (very poorly) can be summarized as Vector Clocks + Timestamps, you get the advantages of both without either of their weaknesses. What his means is that data integrity is guaranteed because every machine is using a deterministic algorithm, without any extra gossip between machines. Let me repeat, you'll get the same result on every machine, eventually consistent, without any multi-machine coordination. This means every peer is a master, and that is awesome, even if you are running it on an ephemeral server/cloud - completely resilient to failures, terminations, restarts, and reboots.

- Empowering the App. Yes. Databases should serve you, not the other way around. Answers to his questions about abstractions are at http://github.com/amark/gun .

- Operational Challenges. This is where I diverge from him. If something seems wrong, like things suddenly becoming slow, you can easily just restart it without any damage/harm/failure occurring. And then you can look through your logs, taking your time, to see what went wrong.

- Basic Building Blocks. Because GUN is a graph database, you get key-value like access, as well as documents and relational styles. That is because mathematically graphs are the superset of relational algebra and hierarchy trees.

Happy to answer any questions!

Re:Why I love databases by mjb on Sunday November 09, 2014 @08:07PM (#8581528)
> you can never go from strong consistency down to an eventually consistent system

Sure you can. In fact, almost every eventually consistent database is built on a collection of strongly consistent single systems. Eventually consistent systems are even frequently built on top of consistent distributed systems, and some of the largest infrastructure on the planet works just like that.

Re:Why I love databases by marknadal on Sunday November 09, 2014 @11:13PM (#8581975)
I'm not quite sure where you get that. A strongly consistent system requires other database peers to lock on writes, which isn't needed for eventual consistency.

See http://aphyr.com/posts/313-strong-consistency-models for some great descriptions of how it works, he is quite the expert at testing these things.

Re:Why I love databases by lucian1900 on Monday November 10, 2014 @05:03AM (#8582754)
> Paxos is difficult - all of them, Raft, Quorum, leader election, etc. DO NOT USE THEM unless you are Google, Amazon, Walmart, or what not. Even then, do not use them. Instead, I've solved this challenging problem by developing a new Conflict Resolution system

Just in case anyone else is considering this: it's a bad idea. Developing a correct consensus algorithm is a lot of hard, unusual work. It's also useless until you can prove it correct.

Paxos, Raft and ZAB (Zookeeper's protocol) have already been proven correct. Pick a library for one and use it. Raft is probably easiest to understand and use.

Re:Why I love databases by marknadal on Tuesday November 11, 2014 @12:28AM (#8588468)
CRDTs are probably the best thing out there, and the most understandable. They focus on data that does not require complicated leader election algorithms - and this is coming from Facebook and the Cassandra team.

Why? Because these systems are very weak and prone toward failure, just check out some of http://aphyr.com/ 's stuff, he tears a lot of popular databases apart. Even if the theories are "proven right" the implementations are often wrong.

Re:Why I love databases by lucian1900 on Tuesday November 11, 2014 @04:29AM (#8589037)
None of the systems that failed aphyr's tests used a proven consensus algorithm correctly. If you look the Zookeeper test, it performed perfectly, as expected. Even etcd/Consul would pass if they chose to, the defaults are merely bad by choice.

CRDTs are useful for salvaging consistency out of an inconsistent database, but they're not the only choice possible.

Re:Why I love databases by marknadal on Tuesday November 11, 2014 @09:17PM (#8593631)
Good point. BUT that is a tautology - of course systems that implement it correctly are correct. Unfortunately, there are very few of those things out there... and Zookeeper etcd/Consul are all relatively new, too.

This second point goes back to my statement regarding "strong consistency can be built from eventually consistent systems, but you can't go from a strong system to an eventually consistent system." And this is where and why gun makes sense. Start with an eventually consistent system, because it is far far easier and covers 98% of the use cases out there. And only then, when you need consensus on edge-case data, build it out but make sure you understand what you are doing.

Re:Why I love databases by zzzcpan on Sunday November 09, 2014 @09:49PM (#8581815)
It's always fun the first time. Good luck.

Some notes from my experience implementing AP:

- Easiest way to support scaling out is pre-sharding an entire database at the beginning, so there will be no need to ever reshard it. Like have 16 shards spread manually across all nodes and put into configs which nodes synchronize with which. Once you add another node simply move some shards to that node and be done with it.

- Synchronization is easy with hash/Merkle trees but they don't need to keep in-memory hashes of every single record or recalculate them on insertion or deletion. Instead every time new record is inserted its hash should be added arithmetically to the one stored in particular leaf. Same on deletion, but subtracted arithmetically. No scanning of other records for no reason or anything like that.

- Jitters are super important. Synchronizing data or doing anything periodically will cause lags if you forget to schedule at random times.

- Also, don't forget that random disk accesses are slow and organize your data in such way, that disks are read sequentially for most of the bulk access patterns, like during synchronization.

Re:Why I love databases by marknadal on Sunday November 09, 2014 @11:28PM (#8582017)
Thanks! Although I would like to disagree with some of your points:

- Sharding is only necessary for centralized systems, where there exists some "Single Source of Truth" of the record. GUN can and will store things in multiple places, so it behaves more like BitTorrent. This reduces the complexity and thus makes the database significantly easier to use and scale, however I had to solve some hard conflict resolution problems to get that to work.

- Yes, Merkle trees are awesome. But deletion is a very dangerous action in a database and should be avoided (as noted in the original article). It causes all sorts of potential data-corruption/divergence problems.

- All data syncs in realtime, cause GUN sports push notifications. However, if the there is a network partition, the data will sync (without problems) after it is restored. So yes, agreed, don't do things periodically - do them immediately!

- Disk access is slow, correct. That is why you should keep as much of the working set in memory. Doing so minimizes disk access, so the majority of your write/read ops are lightning fast.

Do you have any links to your stuff?

Re:Why I love databases by zzzcpan on Monday November 10, 2014 @07:44AM (#8583204)
I'm not gonna argue with your points, just gonna warn you, that you have too much confidence in your algorithms, be careful.

> Do you have any links to your stuff?

No, it's not open source. Maybe with next implementation I will finally be ready to share it.

Re:Why I love databases by marknadal on Tuesday November 11, 2014 @12:15AM (#8588432)
Algorithms should never be used based on confidence, only whether they work or not. Try them out and let me know your thoughts!
Re:Why I love databases by kapv89 on Sunday November 09, 2014 @08:32PM (#8581608)
Hey, can you suggest some good reads for understanding databases enough to build a simple relational database ...
Re:Why I love databases by jbergens on Monday November 10, 2014 @06:01AM (#8582899)
You can look at all the open source databases that are out there. You can also look at things like FoundationDb and their SQL layer. It shows how you can build a relational api on top of a NoSql db with transactions. I think that will be open source on GitHub https://github.com/FoundationDB/sql-layer.
Re:Why I love databases by jerrysievert on Sunday November 09, 2014 @08:55PM (#8581683)
coming soon! (i also write my own databases, because it's fun)
Re:Why I love databases by marknadal on Sunday November 09, 2014 @11:14PM (#8581979)
Jerry, would love to see your list. Have any links to your database work?
Re:Why I love databases by ddorian43 on Sunday November 09, 2014 @06:27PM (#8581288)
A database...... in javascript... The only thing next(probably already done) is a mailserver.
Re:Why I love databases by pluma on Monday November 10, 2014 @06:29AM (#8582987)
I think Haraka[0] pre-dates JS databases.

[0] https://haraka.github.io/

Re:Why I love databases by ddorian43 on Monday November 10, 2014 @06:32AM (#8582999)
Since we have:

http://bellard.org/jslinux/

We also should have the haraka package.

Re:Why I love databases by Already__Taken on Monday November 10, 2014 @05:01AM (#8582746)
Joking aside, Isn't node.js created for streaming data almost exactly like a mail server should?
Re:Why I love databases by ddorian43 on Monday November 10, 2014 @05:07AM (#8582764)
Zawinski's law of software envelopment:

Every program attempts to expand until it can read mail. Those programs which cannot so expand are replaced by ones which can.

Re:Why I love databases by al2o3cr on Monday November 10, 2014 @07:15AM (#8583107)
"The beauty of this, though, is that you can always build strong consistency out of eventual consistency (it just requires knowing X amount of peers in advance, and doing a trivial lock until you've heard back from all of them)"

Sounds like a way to trade A for maybe-C, there - what happens if Godzilla stomps one of the servers you're waiting on?

Re:Why I love databases by marknadal on Tuesday November 11, 2014 @12:35AM (#8588479)
Yes, you are right.

Lol, you get skrewed. That is the danger of globally consistent systems, unless you have slaves to that master that got stomped that can do leader-election to survive the failure.

Re:Why I love databases by sontek on Sunday November 09, 2014 @11:26PM (#8582009)
The commit messages on that repo have me worried!
Re:Why I love databases by marknadal on Sunday November 09, 2014 @11:31PM (#8582026)
Haha, sorry about that. I'm in the middle of cleaning up the directory structure to make things more coherent.

Try out some of the examples and the readme tutorial and let me know your thoughts. It is MVP status, and I'm trying to add more docs and get things ready to be stable.