r/AskComputerScience • u/final-getsuga • Sep 23 '15
How do databases work with things like Youtube comments and Facebook likes?
I just started reading about Brewer's Cap Theorem and it made me realized how much I didn't know about databases. There's so much new terms I need to look at like ACID and BASE. My question is about the "setup" of databases here. How do the databases work here? Is the database duplicated across all connected computers or is there a powerful constantly-monitored computer that holds the database and the server/nodes read from/write to that database?
My question for now is when I make a comment in a youtube video, how is it stored? Obviously a host/computer in the youtube network is going to receive it and insert it into a database. However, is this a database that's part of a "local" enormous database that's going to be copied to other regions of the world?
For example if I was in New York and I made a comment to video1, does it go to the eastern USA "local" youtube database that's going to be copied over to the midwest + west database in a few minutes and then to the other "large" database youtube nodes across the world?
I'm trying to understand what it means to be consistent in the Brewer's Cap Theorem. How are nodes making sure they have the most updated version? One large database or each computer have its own database?
Thanks!
2
u/omapuppet Sep 23 '15
Is the database duplicated across all connected computers or is there a powerful constantly-monitored computer that holds the database
Both of those models have their uses. Very large distributed systems like YouTube and Facebook are usually set up as many regional clusters of many individual data servers with replication moving data across the system. Within a cluster the data may be sharded such that individual servers handle different parts of the data store. Because of the way data replicates across the system different users may not all see the same view of the data at the same time (that is, C is less important than A and P). This is acceptable because in such a system the lack of consistency isn't normally a significant problem, unless replication latency is really high they probably won't ever notice. Other kinds of systems may use a database system that looks like one gigantic, high-powered database that gives strong guarantees about what users will see when. This is useful for some kinds of business systems where C and A are very important, but P isn't as critical (like users are mostly on the same campus, so regional network problems are low risk).
The actual replication strategy varies depending on what kind of database technology is in use and the requirements of the system with regards to factors like acceptable latency, conflict resolution, etc.
1
u/berlinbrown Sep 23 '15
How does Facebook or Reddit or Youtube decide which components need a distributed database and which parts don't? And how does a front end web developer consume services from different pieces.
I am thinking of reddit. But let's say I want to add 'likes' next to a link. Is that data for the likes pulled from the likes service? And then what about other pieces of the page? Upvotes vs number of readers?
27
u/BonzoESC Sep 23 '15
Disclosure: I work as an engineer at Basho, makers of the Riak database. All opinions and such below are my own.
Facebook and YouTube use an enormous database that's copied to multiple regions of the world. Individual nodes don't care about the most updated version of the database: they care about the version of a record with the most history, or that they have the record at all.
The CAP theorem basically says that during a Partition, you can sacrifice either Consistency or Availability. For YouTube comments or Facebook likes, it's okay if they're a bit out of sync for a while (lack of consistency), but absolutely not okay if they don't work (lack of availability).
Here's how I believe YouTube comments to work:
Posting a comment to video1 will go from your browser to a local or regional YouTube app server
The app server will store your comment in multiple collections: once for the screen that shows all of your comments, once for the whole set of comments on the video, and possibly for other views of comments too. Denormalization spends disk space and write complexity to reduce disk seeks on read. From now on, let's just talk about the copy of the comment on the video.
The app server will write the comment to the local database cluster, which is almost certainly an AP system: comments taking time to show up is a better scenario than not having comments at all.
The local database cluster will store the comment to several nodes within the cluster, and queue it up for replication to other datacenters.
When the remote cluster receives the comment (may be seconds or minutes, depending on how much commenting is going on compared to peak network bandwidth) it too will store it on several nodes. Depending on how the replication system works, it may queue it for replication to other clusters, or simply trust that your local cluster handled fan-out.
At some point, there may be a "full sync" operation, which uses a Merkle tree to sync up large datasets while minimizing coordination and bandwidth.
Deleting comments is similar: just save a new comment-like entity called a "tombstone" that references the original comment and says it's deleted.
YouTube view counts and Facebook like counts can use a CRDT counter.
Facebook likes work basically the same way: add an entry to the CRDT set of likes for a like-able thing, and merge the set at every datacenter. Un-liking something uses whatever CRDT set's semantics are for removing a set member.
Amazon's Dynamo paper is a great description of how a multi-node database can be constructed: http://www.read.seas.harvard.edu/~kohler/class/cs239-w08/decandia07dynamo.pdf
Basho's open-source Riak KV database uses Merkle trees to continuously check and resolve conflicting or missing data: http://docs.basho.com/riak/latest/theory/concepts/aae/
Riak Enterprise Edition (paid, closed-source) has publicly-visible documentation for how its multi-datacenter replication works: http://docs.basho.com/riak/latest/ops/mdc/v3/architecture/
Great reads on how databases handle network partitions: https://aphyr.com/tags/Jepsen
Facebook research into how to preserve causality in distributed systems: https://queue.acm.org/detail.cfm?id=2610533
Fixing some of the scalability issues Facebook ran into implementing the above: http://www.bailis.org/papers/explicit-socc2012.pdf
What are CRDTs? http://pagesperso-systeme.lip6.fr/Marc.Shapiro/papers/RR-6956.pdf
Me, explaining CRDTs: https://vimeo.com/45433300