NoSQL and eventual consistency - real world examples [closed]
Asked Answered
S

3

18

I'm looking for good examples of NoSQL apps that portray how to work with lack of transactionality as we know it in relational databases. I'm mostly interested in write-intensive code, as for mostly read-only code this is a much easier task. I've read a number of things about NoSQL in general, about CAP theorem, eventual consistency etc. However those things tend to concentrate on the database architecture for its own sake and not on the design patterns to use with it. I do understand that it's impossible to achieve full transactionality within a distributed app. This is exactly why I would like to understand where and how requirements should be lowered in order to make the task feasable.

EDIT:

It's not that eventual consistency is my goal on it's own. For the time being I don't really see how to use NoSQL to certain things that are write-intensive. Say: I have a simplistic auction system, where there are offers. In theory the first person to accept an offer wins. In practice I would like at least to guarantee that there is only a single winner and that people get their results in the same request. It's probably not feasable. But how to solve it in practice - maybe some requests could take longer than usual, because something went wrong. Maybe some requests should be automatically refreshed. It's just an example.

Starchy answered 28/3, 2011 at 22:58 Comment(6)
What NoSQL "variant" would you use?Brandiebrandise
Well I don't really know. Sadly my knowledge is very theoretical for the time being. Anything that you'd recommend. Maybe my question is premature. Maybe I should read some additional material to ask a better one?Starchy
You have to know what you want. For example, how many servers do you want to use? Do you want failover? The CAP theorem tells that you can have TWO out of three of Consistency, Availability and Partition Tolerance, not ZERO of three!Brandiebrandise
Ok, on this level I can definitely say that I want A+P. So the consistency part goes away. For example I noticed that some information takes minutes to get properly propagated here on SO. I suppose they also use those 2/3.Starchy
But in your example it seems you want C :-) (and that people get their results in the same request). The other part of the request is doable (at least to guarantee that there is only a single winner). When the auction is ended (it's a time-based auction like ebay, not a time-from-last-bid auction right?) all the servers "realign" and the winner is selected.Brandiebrandise
From what I know A+P is much more popular. My understanding is that both BigTable and Amazon SimpleDB follow this approach. Also I guess it's not a common practice to use two datastores at the same time - one supporting A+P and one supporting C+P. So I have to choose. And as typically websites cannot be available just from time to time, I will probably choose A+P and will be left with a problem of implementing this sort of thing nevertheless. However this was just meant to serve as an example.Starchy
C
37

Let me explain CAP in purely intuitive terms. First, what C, A and P mean:

  • Consistency: From the standpoint of an external observer, each "transaction" either fully completed or is fully rolled back. For example, when making an amazon purchase the purchase confirmation, order status update, inventory reduction etc should all appear 'in sync' regardless of the internal partitioning into sub-systems

  • Availablility: 100% of requests are completed successfully.

  • Partition Tolerance: Any given request can be completed even if a subset of nodes in the system are unavailable.

What do these imply from a system design standpoint? what is the tension which CAP defines?

To achieve P, we needs replicas. Lots of em! The more replicas we keep, the better the chances are that any piece of data we need will be available even if some nodes are offline. For absolute "P" we should replicate every single data item to every node in the system. (Obviously in real life we compromise on 2, 3, etc)

To achieve A, we need no single point of failure. That means that "primary/secondary" or "master/slave" replication configurations go out the window since the master/primary is a single point of failure. We need to go with multiple master configurations. To achieve absolute "A", any single replica must be able to handle reads and writes independently of the other replicas. (in reality we compromise on async, queue based, quorums, etc)

To achieve C, we need a "single version of truth" in the system. Meaning that if I write to node A and then immediately read back from node B, node B should return the up-to-date value. Obviously this can't happen in a truly distributed multi-master system.

So, what is the solution to your question? Probably to loosen up some of the constraints, and to compromise on the others.

For example, to achieve a "full write consistency" guarantee in a system with n replicas, the # of reads + the # of writes must be greater or equal to n : r + w >= n. This is easy to explain with an example: if I store each item on 3 replicas, then I have a few options to guarantee consistency:

A) I can write the item to all 3 replicas and then read from any one of the 3 and be confident I'm getting the latest version B) I can write item to one of the replicas, and then read all 3 replicas and choose the last of the 3 results C) I can write to 2 out of the 3 replicas, and read from 2 out of the 3 replicas, and I am guaranteed that I'll have the latest version on one of them.

Of course, the rule above assumes that no nodes have gone down in the meantime. To ensure P + C you will need to be even more paranoid...

There are also a near-infinite number of 'implementation' hacks - for example the storage layer might fail the call if it can't write to a minimal quorum, but might continue to propagate the updates to additional nodes even after returning success. Or, it might loosen the semantic guarantees and push the responsibility of merging versioning conflicts up to the business layer (this is what Amazon's Dynamo did).

Different subsets of data can have different guarantees (ie single point of failure might be OK for critical data, or it might be OK to block on your write request until the minimal # of write replicas have successfully written the new version)

There is more to talk about, but let me know if this was helpful and if you have any followup questions, we can continue from there...

[Continued...]

The patterns for solving the 90% case already exist, but each NoSQL solution applies them in different configurations. The patterns are things like partitioning (stable/hash-based or variable/lookup-based), redundancy and replication, in memory-caches, distributed algorithms such as map/reduce.

When you drill down into those patterns, the underlying algorithms are also fairly universal: version vectors, merckle trees, DHTs, gossip protocols, etc.

The same can be said for most SQL solutions: they all implement indexes (which use b-trees under the hood), have relatively smart query optimizers which are based on known CS algorithms, all use in-memory caching to reduce disk IO. The differences are mostly in implementation, management experience, toolset support, etc

unfortunately I can't point to some central repository of wisdom which contains all you will need to know. In general, start with asking yourself what NoSQL characteristics you really need. That will guide you to choosing between a key-value store, a document store or a column store. (those are the 3 main categories of NoSQL offerings). And from there you can start comparing the various implementations.

[Updated again 4/14/2011]

OK here's the part which actually justifies the bounty.. I just found the following 120 page whitepaper on NoSQL systems. This is very close to being the "NoSQL bible" which I told you earlier doesn't exist. Read it and rejoice :-)

NoSQL Databases, Christof Strauch

Compilation answered 3/4, 2011 at 10:38 Comment(4)
I really like what you've written, especially this voting part. My question than would be: how much such algorithms need to be reinvented for each new webapp that is written and how much is there a set of universal solutions that let's say cover 90% of typical use cases? And if it exists, where to look for it? Thanks again.Starchy
I added a bit more content to my answer, feel free to follow up some more...Compilation
Many thanks. I think I'll read about the terms that you mentioned for a start. Seems like there isn't any easy path to learn it all, but it's not a problem. I really appreciate your help.Starchy
Very helpful answer and the linked paper is brilliant!Dichromaticism
P
5

There are many applications where eventual consistency is fine. Consider Twitter as a rather famous example. There's no reason that your "tweets" have to go out to all of your "followers" instantaneously. If it takes several seconds (or even minutes?) for your "tweet" to be distributed, who would even notice?

If you want non-web examples, any store-and-forward service (like email and USENET) would be require eventual consistency.

Psychodrama answered 3/4, 2011 at 7:6 Comment(1)
Yea, I agree. Event SO has some eventual consistency built in, it seems. The thing is, I would like to look into source code, or at least read some paper about a concrete implementation.Starchy
M
1

It's not impossible to get transactions or consistency in NoSQL. A lot of people define NoSQL in terms of a lack of transactions or as requiring eventual consistency at best, but this isn't accurate. There are transactional nosql products out there - consider tuple spaces, for example - that scale very well even while providing app consistency.

Magnify answered 29/3, 2011 at 18:40 Comment(8)
I might say something totally wrong, however as I understand CAP theorem you cannot really have any transactionality in common meaning, availability and consistency at the same time. And this applies to anything really, apps included. So what you just said, sounds very confusing to me. Note: my knowledge is very theoretical for now.Starchy
CAP isn't all-or-nothing, for one thing. You can decide how much of each aspect you want, and design and deploy accordingly. It really does come down to deployment. If all data items are equally sharded, and there's no preference given to data, and you don't care about replication or sync, well... but you CAN care.Magnify
I'll maybe add some explaination through an edit.Starchy
Well, if I could be so bold, I'd just say: use a datastore that provides actual transactions! That would eliminate some possibilities, sure, because they aim for different capabilities. But there are products that can grab TX locks on data - yes, NoSQL products that can do that do exist - and the one who grabs the data would "win." If the TX fails, the lock is dropped, data is restored to its original state, and everyone wins.Magnify
The point is, I would like to understand how to write the real world NoSQL based apps. However the resources that I come by only explain the technical side of the problem. It not really helping with understanding how to use it properly to build anything real.Starchy
Well, the key is to just write them. NoSQL isn't magic, it's just another datastore, with different characteristics than the traditional RDMS. Typically NoSQL can be far more scalable; there are costs with the NoSQL approach (typically transactions and ordering, for example.) That said, you can't just say "Oh, NoSQL means a fluid datastructure" because it doesn't mean a fluid datastructure; it's just that some NoSQL implementations use a document model rather than a fixed schema. You can't say "no transactions" because some NoSQL implementations use transactions.Magnify
Ok, my experience is that most people who write webapps, write broken webapps. I don't say that they don't work in practice, but the only reason for that is that they are exposed to a very low traffic. Therefore I don't really want to become an analogue of such people for NoSQL datastores. I like to know some fundamentals before I actually get to use something. Especially if there isn't really any time pressure, and I investigate subject on my own.Starchy
Sounds like a sound plan to me. :)Magnify

© 2022 - 2024 — McMap. All rights reserved.