Choosing a Database for Systems Design: All you need to know in one video

Jordan has no life
20 Oct 202223:58

Summary

TLDRThis video offers an in-depth exploration of databases, tailored for systems design interviews. The host begins with a review of database indices, comparing LSM trees and SS tables with B trees, highlighting their respective strengths in read and write operations. The discussion then delves into replication strategies, contrasting single leader, multi-leader, and leaderless models. Various databases are dissected, including SQL and NoSQL types like MongoDB, Cassandra, and Riak, each with their use cases and trade-offs. Technologies like Memcache, Redis, and graph databases like Neo4j are also touched upon, with special mentions of time series databases for data ingestion and VoltDB and Spanner for high-performance SQL needs. The video serves as a comprehensive guide for those preparing for technical interviews or looking to broaden their database knowledge.

Takeaways

  • 👋 The video is a comprehensive guide on databases, a topic the creator has been promising to cover since their first episode.
  • 👚 The creator shows support for women in tech by wearing an 'Women at Amazon' shirt, encouraging women to participate in the tech industry.
  • 📈 The script includes an in-depth technical discussion about databases, aiming to go more in-depth than other videos on the platform.
  • 🔍 The importance of database indices is highlighted, with a comparison between LSM trees and SS tables versus B trees, emphasizing their impact on read and write speeds.
  • 📚 A review of database concepts is provided for viewers who may not be familiar with the topic, including explanations of database indices and their types.
  • 🗣️ The video covers different types of replication strategies in databases, such as single leader, multi-leader, and leaderless replication, and their respective pros and cons.
  • 📊 The script discusses various types of databases, including SQL, NoSQL, and specific examples like MongoDB, Cassandra, Riak, HBase, Redis, and Neo4j, detailing their use cases and features.
  • 🛠️ The video provides insights into when to use SQL databases, suggesting they are best for scenarios where data correctness is more critical than speed.
  • 📈 The advantages of NoSQL databases like MongoDB and Cassandra are explained, particularly in scenarios requiring high write throughput and flexibility in data modeling.
  • 🕒 The video also touches on specialized databases like time series databases and graph databases, highlighting their unique use cases and benefits.
  • 🏢 Honorable mentions are given to NewSQL databases like VoltDB and Spanner, which offer innovative approaches to traditional SQL databases with enhanced performance.

Q & A

  • What is the main focus of the video?

    -The video focuses on explaining different types of databases, their features, and use cases, particularly in the context of system design interviews.

  • Why might the presenter be wearing a 'women at Amazon' shirt?

    -The presenter is showing support for women in the tech industry, possibly in relation to diversity and inclusion initiatives or events.

  • What are the two predominant types of database indices discussed in the video?

    -The two predominant types of database indices discussed are the LSM tree and SS table combination, and the B tree.

  • How do LSM trees and SS tables work together in a database?

    -LSM trees are in-memory balanced binary search trees that store keys and their corresponding values. When the tree grows too large, it gets flushed to an SS table on disk, which is a sorted list of keys and values. Reads first check the in-memory LSM tree, then proceed through SS tables from most recent to least recent.

  • What is the advantage of using B trees for databases?

    -B trees, which are implemented completely on disk, allow for faster reads because they enable direct access to the location of a key by following the tree structure on disk, without having to iterate through multiple SS table files.

  • What are the different types of replication strategies mentioned in the video?

    -The video mentions single leader replication, multi-leader replication, and leaderless replication as the different types of replication strategies.

  • What is the trade-off between single leader and multi-leader replication?

    -Single leader replication ensures no write conflicts but has lower write throughput because all writes go through one master node. Multi-leader replication can have higher write throughput but increases the likelihood of write conflicts.

  • Why are SQL databases recommended for use cases where correctness is more important than speed?

    -SQL databases are recommended for correctness-focused scenarios due to their support for transactions, ACID properties, and the use of B trees, which ensure data integrity even if it slows down the database performance.

  • What is MongoDB's data model, and how does it differ from SQL databases?

    -MongoDB uses a document-based data model, where data is stored in documents with potential nesting of documents, unlike SQL's relational and normalized data model that uses rows and joins to reference data across tables.

  • How does Cassandra handle write conflicts in a multi-leader or leaderless replication setup?

    -Cassandra handles write conflicts using a 'last write wins' strategy, which may not be ideal as it can lead to some writes being overwritten based on timestamp.

  • What is special about the storage model of Apache HBase compared to other databases?

    -Apache HBase uses a column-wise storage model instead of the traditional row-wise storage, which improves data locality and read performance when accessing entire columns of data.

  • Why are in-memory key-value stores like Memcached and Redis not considered the best database solutions?

    -Memcached and Redis are not the best database solutions because they are in-memory key-value stores without persistent storage, making them more suitable for caching and frequently accessed data rather than for long-term data storage.

  • What is the primary use case for graph databases like Neo4j?

    -Graph databases like Neo4j are primarily useful for modeling and traversing complex relationships and networks, such as social networks, recommendation systems, and geographical mapping.

  • What are the characteristics of time series databases that make them efficient for handling time-stamped data?

    -Time series databases are efficient for time-stamped data due to their use of LSM trees and SS tables for fast ingestion, and their ability to split data into smaller indexes that can be quickly accessed and deleted as needed.

  • What are some of the 'NewSQL' databases mentioned in the video, and how do they differ from traditional SQL databases?

    -NewSQL databases like VoltDB aim to provide the benefits of SQL databases with improved performance. VoltDB, for example, runs entirely in-memory and uses single-threaded execution to eliminate race conditions and achieve high performance, but at the cost of scalability and memory usage.

  • What is the unique feature of Google's Spanner database that helps achieve strong consistency?

    -Google's Spanner uses GPS clocks in its data centers to assign accurate timestamps to each write operation. These timestamps allow the database to order all writes and achieve strong consistency through linearizability, without the need for extensive locking mechanisms.

Outlines

00:00

🚀 Introduction to Databases Video

The speaker welcomes viewers to a comprehensive databases video, acknowledging the long-awaited nature of the content. They express support for women in Amazon and hint at a more technical depth than usual for such videos on YouTube. The video is described as lengthy and detailed, covering the selection of databases for system design interviews, and the importance of understanding different database types and their use cases.

05:01

📚 Reviewing Database Fundamentals

This paragraph delves into the basics of database indexing, explaining the role of indexes in speeding up reads and the trade-off with write speeds. Two predominant types of indices are discussed: LSM trees and SS tables, and B trees. The speaker provides a brief overview of how LSM trees work in-memory and how SS tables function on disk, including the process of flushing, compacting, and searching. The advantages and disadvantages of LSM trees and B trees are compared in terms of read and write performance.

10:02

🔄 Exploring Database Replication

The speaker discusses the concept of database replication, which is crucial for data retention in case of hardware or software failures. Different types of replication strategies are outlined, including single leader, multi-leader, and leaderless replication. The pros and cons of single leader replication, which offers conflict-free writes but lower throughput, are contrasted with the unlimited write throughput but potential for write conflicts in multi-leader or leaderless replication.

15:02

🗃️ SQL and NoSQL Databases Overview

The paragraph provides an overview of SQL databases, emphasizing their relational and normalized data model, which can be challenging for write operations at scale due to the need for distributed transactions. The benefits of SQL databases, such as ACID guarantees and the use of B trees for efficient reading, are highlighted. The speaker then transitions into NoSQL databases, starting with MongoDB, a document database that offers flexibility and data locality but can face issues with data synchronization and conflicts.

20:04

🌐 Cassandra and Other NoSQL Databases

The speaker introduces Cassandra as a preferred NoSQL database for system design interviews due to its wide column store model, flexible data schema, and support for multi-leader and leaderless replication. Cassandra's use of LSM tree indices for faster writes and its approach to conflict resolution using last write wins are discussed. The paragraph also mentions Riak, another NoSQL database that uses CRDTs for more sophisticated conflict resolution. The speaker corrects a mistake regarding Riak's data store type and moves on to discuss Apache HBase, highlighting its column-wide storage and performance benefits for certain use cases.

🔑 In-Memory Databases and Graph Databases

This paragraph covers in-memory databases like Memcached and Redis, which offer fast access for frequently read and written data due to their key-value store nature and lack of need for indexing. The speaker then introduces Neo4j, a graph database, explaining its efficiency for graph traversal compared to SQL databases. The unique use case for graph databases in modeling relationships, such as social networks or map data, is highlighted.

🕒 Time Series Databases and Honorable Mentions

The speaker discusses time series databases, which are optimized for fast ingestion and ordered data retrieval based on timestamps. These databases use smaller LSM tree indexes to improve read efficiency and simplify the deletion of old data. The paragraph also includes honorable mentions of NewSQL databases like VoltDB and Google's Spanner, which offer innovative approaches to SQL database performance. Additionally, data warehouses are mentioned for their use in analytics.

Mindmap

Keywords

💡Database Index

A database index is a data structure that improves the speed of data retrieval operations on a database. In the context of the video, it's highlighted that an index can significantly speed up reads by sorting data in a specific order. The video discusses two popular types of database indices: LSM trees and SS tables, and B trees, each with their own advantages and disadvantages in terms of read and write operations.

💡LSM Tree

LSM Tree stands for Log-Structured Merge-Tree, which is a type of data structure used in database systems to optimize write-heavy workloads. The video explains that LSM trees are balanced binary search trees stored in memory and when they grow too large, they get flushed to SS tables on disk. LSM trees are beneficial for fast writes but can lead to slower reads since multiple SS tables may need to be checked.

💡SS Table

SS Table stands for Sorted String Table, which is a data structure used in databases to store data on disk. According to the video, SS tables are sorted lists of keys and their values. They are created when the in-memory LSM tree becomes too large, and they help in efficiently searching for keys by maintaining a sorted order, which allows for binary search during read operations.

💡B Tree

B Tree is a self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. The video mentions that B trees are implemented completely on disk with pages that have a range of keys and pointers, making them efficient for reads but less so for writes compared to LSM trees.

💡Replication

Replication in the context of databases refers to the process of duplicating data across different nodes to ensure data availability and fault tolerance. The video script discusses different types of replication strategies such as single leader, multi-leader, and leaderless replication, each with its own trade-offs between write throughput, read correctness, and conflict resolution.

💡SQL Database

SQL, which stands for Structured Query Language, databases are relational databases that store data in a structured format using tables, rows, and columns. The video emphasizes that SQL databases are biased towards using relational and normalized data, which means they avoid data duplication and use joins to reference data across tables. SQL databases are highlighted as being particularly useful when data correctness and transactional integrity are paramount, such as in banking transactions.

💡NoSQL Database

NoSQL databases are non-relational databases that do not use the traditional SQL table-based structure for storing data. The video script mentions several types of NoSQL databases, such as MongoDB, Cassandra, Riak, and Apache HBase, each with different data models and use cases. NoSQL databases are often chosen for their flexibility, scalability, and performance in handling large volumes of data.

💡MongoDB

MongoDB is a document-oriented NoSQL database that stores data in JSON-like documents. The video explains that MongoDB provides good data locality by allowing nested documents, which can be beneficial for applications that need to store and retrieve large amounts of related data in a single operation.

💡Cassandra

Cassandra is a NoSQL database known for its wide column store architecture, which allows for flexible data models and is particularly suited for handling large volumes of data distributed across many commodity servers. The video script points out that Cassandra is a good choice for systems that require high write throughput and can tolerate eventual consistency.

💡Riak

Riak is a NoSQL database that, like Cassandra, is designed for high availability, fault tolerance, and scalability. The video highlights Riak's use of Conflict-free Replicated Data Types (CRDTs) for conflict resolution, which allows for more complex logic to resolve write conflicts compared to the last-write-wins strategy used in Cassandra.

💡Apache HBase

Apache HBase is an open-source, non-relational, distributed database modeled after Google's Bigtable. It is a wide column store database built on top of the Hadoop Distributed File System (HDFS). The video script explains that HBase uses column-wise storage, which is beneficial for achieving better data locality and efficient reads on columns of data.

💡Redis

Redis is an in-memory key-value store that is used as a database, cache, and message broker. The video script mentions Redis as having a rich feature set, including support for data structures such as strings, hashes, lists, and sets. Redis is highlighted for its use in caching and for storing data that requires frequent read and write operations due to its in-memory storage.

💡Neo4j

Neo4j is a graph database management system that is designed to handle structured graph data. The video script explains that graph databases like Neo4j are efficient for storing and querying data that is interconnected, such as social networks or recommendation systems, due to their native support for graph traversal operations.

💡Time Series Database

A time series database is optimized for storing and retrieving time-stamped data. The video script describes these databases as being efficient for handling large volumes of data from multiple sources while maintaining the data's chronological order. Time series databases use LSM trees and SS tables for fast ingestion and deletion of old data.

💡VoltDB

VoltDB is a NewSQL database that is designed for high-performance transaction processing. The video script mentions that VoltDB runs entirely in-memory and uses a single-threaded architecture to eliminate race conditions and achieve high throughput. It's noted as an interesting concept but may be limited by its in-memory storage and scalability.

💡Spanner

Spanner is a fully managed, mission-critical, globally consistent, and scalable relational database service from Google Cloud. The video script describes Spanner as a NewSQL database that uses GPS clocks to assign timestamps to each write operation, allowing it to achieve strong consistency across a distributed system without the need for extensive locking.

Highlights

The video discusses the importance of choosing the right database for a given problem in a systems design interview.

It provides an in-depth technical review of database indices, including LSM trees, SS tables, and B trees.

The presenter shares insights on the trade-offs between write and read performance in different database types.

Replication strategies such as single leader, multi-leader, and leaderless replication are explained with their pros and cons.

SQL databases are highlighted for their strong consistency and transactional capabilities, best suited for data correctness over speed.

MongoDB is introduced as a document database offering flexibility and data locality but with potential consistency issues.

Cassandra is recommended for systems requiring high write throughput and flexibility in data consistency.

Riak is presented as an alternative to Cassandra, with advanced conflict resolution using CRDTs.

Apache HBase is discussed for its columnar storage model, beneficial for fast reads on a single column of data.

Memcache and Redis are mentioned as in-memory key-value stores, ideal for frequently accessed data.

Neo4j, a graph database, is showcased for its efficiency in modeling relationships and traversing nodes.

Time series databases are highlighted for their efficiency in handling large volumes of timestamped data.

VoltDB is introduced as a NewSQL database that runs entirely in-memory with a single thread for high performance.

Google's Spanner is discussed for its use of GPS clocks to achieve strong consistency across distributed databases.

The video concludes with a mention of data warehouses, typically used for analytical purposes with SQL formats.

The presenter emphasizes the redundancy of the content, aiming to compile information in a comprehensive manner.

The video is intended to be a resource for those new to the channel or needing a refresher on database selection.

Transcripts

play00:00

welcome back everyone and welcome to the

play00:03

channel if this is your first time uh

play00:05

you lazy in the comments section

play00:07

finally got me to do it and now I'm

play00:09

going to make an all-encompassing

play00:10

databases video I know I've literally

play00:13

been promising this one since the first

play00:15

episode on my channel which uh is pretty

play00:18

embarrassing that I'm only finally

play00:19

getting around to it right now

play00:21

additionally as you can see I've got my

play00:23

women at Amazon shirt on I'm definitely

play00:26

supporting the crowd so if there are any

play00:27

women watching this video

play00:29

I stand with you anyways let's continue

play00:32

let's get into this video

play00:34

um all of this information is completely

play00:37

redundant as far as I'm concerned uh

play00:39

with the other stuff that I've made on

play00:40

my channel but I've gone ahead and

play00:42

compiled it so hopefully that works and

play00:45

uh additionally

play00:46

you know I think I'm gonna go a little

play00:48

bit more technically in depth than any

play00:50

other video of this kind has ever gone

play00:52

on YouTube so uh you know if you

play00:54

appreciate that please follow me my

play00:56

accounts are dwindling and um I'm

play00:58

starting to get insecure all right

play01:00

everyone this is gonna be a kind of

play01:02

longer PowerPoint so sit back buckle up

play01:05

get some tissues and lotion and let's

play01:06

get into this so we're gonna go ahead

play01:08

and choose a database you're doing a

play01:10

systems design interview and your

play01:12

interviewer asks the famous question

play01:14

what database are you going to use in

play01:16

this problem if not more than one

play01:18

oftentimes it is better to use more than

play01:20

one so let's go ahead and talk about how

play01:22

we can think about this problem

play01:24

okay like I said there's your background

play01:26

let's start jumping into some databases

play01:29

however before we can really do any of

play01:31

this jumping into databases first we

play01:33

have to review some information I

play01:35

wouldn't be surprised if some of the

play01:36

people watching this video have not seen

play01:38

my earlier ones because you know just by

play01:40

virtue of being earlier on my channel

play01:41

not everyone has seen them and so I am

play01:43

going to do a little bit of review

play01:45

I'll make sure to put timestamps in the

play01:47

video if you actually know this stuff

play01:48

and then you can go ahead and Skip

play01:50

through to the part where I'm actually

play01:51

talking about database evaluations just

play01:53

really quickly a database index is super

play01:55

useful for speeding up reads on a

play01:57

database it does slow down rates but the

play01:59

general gist is that you are sorting in

play02:01

a specific order by key and this makes

play02:03

it the case that when you're searching

play02:05

for a specific key in the database and

play02:07

the value of it you can do so much

play02:08

faster than had you not used an index so

play02:11

in today's databases that are on disk

play02:13

there are two predominantly popular

play02:14

types of database indices we are going

play02:16

to be talking about both the first is

play02:18

the LSM tree and the SS table

play02:20

combination and the next is the B tree

play02:22

so let's look at LSM trees and SS tables

play02:25

the general point is this we have our

play02:27

LSM tree which is basically a balanced

play02:29

binary search tree in memory you can do

play02:32

that with something like a red black

play02:33

tree it doesn't really matter for the

play02:34

purposes of this video like I said it's

play02:36

review all this information is on my

play02:38

channel and more depth already so if

play02:39

you're confused about anything just go

play02:40

watch the dedicated video anyways the

play02:43

point is we have this memory tree which

play02:45

basically has keys and their

play02:46

corresponding values

play02:48

when that tree gets too big because

play02:50

there are too many keys in it it gets

play02:51

flushed to an SS table on disk an SS

play02:54

table is effectively just a sorted list

play02:56

of keys and their values on disk and

play02:59

then finally when we want to go ahead

play03:00

and read a key we first check for it in

play03:02

the mem table which is that LSM tree and

play03:05

then if it's not in there we go through

play03:07

the SS tables in order from the most

play03:09

recent one to the least recent one so

play03:11

we're never actually overwriting any

play03:12

Keys We simply just write new values for

play03:14

those keys and we keep creating new SS

play03:17

tables however the key here is if you

play03:20

know we're running out of space we can

play03:21

actually go ahead and compact our SS

play03:23

Tables by merging them together it's

play03:24

kind of like the merge sort algorithm

play03:26

where you're merging two sorted lists

play03:27

and then that way we can free up some

play03:29

space again the final point is that

play03:31

because our SS tables are sorted we can

play03:33

kind of keep a sparse index of the

play03:36

location of some keys in the SS table

play03:37

and that allows us to do searching

play03:39

really fast in O of log n time via a

play03:42

binary search so the general gist of

play03:44

things here are that the pro of this

play03:46

approach is that we have really fast

play03:47

rights because those right are going to

play03:49

an in-memory buffer in oauth login time

play03:51

again it's a balanced binary search tree

play03:53

so I'll log in and then the biggest con

play03:55

is that for reads we potentially have to

play03:57

go through many SS tables to find the

play03:59

value of a key we can use things like

play04:00

Bloom filters to speed this up but

play04:02

ultimately it's just not as good as B

play04:04

trees for reading and we'll talk about

play04:06

why that is

play04:07

so B trees on the other hand as you can

play04:09

see from the right is also a binary

play04:11

search tree however it's a binary search

play04:13

tree that is implemented completely on

play04:15

disk basically we have pages on disk

play04:18

that have a range of keys and a bunch of

play04:21

pointers so that you can figure out for

play04:23

a specific value of a key you basically

play04:24

iterate through a few of the pointers

play04:26

and then go to the proper place on disk

play04:28

this is really useful for actually being

play04:30

able to get to the right place to both

play04:32

read and write from however for writes

play04:34

because rights in an LSM tree are going

play04:36

to memory that is still going to be

play04:38

better for those so B trees are going to

play04:40

be worse for rights and faster for reads

play04:42

because we don't have to iterate through

play04:44

a bunch of ss table files we could just

play04:46

go directly to the location of the key

play04:47

by virtue of following our tree on disk

play04:50

Okay so we've touched upon indices but

play04:53

now let's go ahead and get into

play04:54

replication so just as a quick recap

play04:57

replication is what you do when you go

play04:59

ahead and duplicate the data that you

play05:00

have on one database node to another

play05:02

database node so that in the event of

play05:04

some sort of Hardware or software

play05:05

failure you can go ahead and make sure

play05:07

that that data is retained so there are

play05:09

a few different types of replication

play05:11

that we're going to quickly cover the

play05:13

first being single leader replication

play05:14

where you basically have one master node

play05:16

that goes ahead and writes to the others

play05:17

and then you can read from any of the

play05:18

nodes you have multi-leader replication

play05:20

and leaderless replication where rights

play05:22

can go to multiple nodes but a

play05:24

multi-leader replication you might have

play05:25

just a couple of liters whereas in

play05:27

leaderless replication those rights

play05:29

might be sent out to every single node

play05:30

in the cluster and in turn the reads

play05:33

might actually come from every single

play05:34

node in the cluster and you may use some

play05:36

sort of like majority voting process to

play05:38

decide what the accurate read is

play05:40

okay so to just quickly sum this up in

play05:42

terms of the pros and cons of the single

play05:44

leader versus multiple leader approach

play05:46

basically the general gist is that with

play05:49

a single leader approach you have one

play05:50

master and a bunch of slaves so as a

play05:52

result you never have to worry about

play05:53

right conflicts but on the flip side

play05:55

you're going to have lower right

play05:57

throughput because all of your rights

play05:58

have to go through that one master node

play06:00

you can try and mitigate this with

play06:01

things like partitioning or sharding but

play06:03

at the end of the day you know if all

play06:05

the rights do have to go to one

play06:06

partition

play06:07

you know the throughput is going to be

play06:09

limited on the exact contrary we have

play06:11

leaderless multi-leader replication

play06:13

where you know we have technically

play06:15

unlimited write throughput in terms of

play06:17

we could be writing to different

play06:18

replicas every single time however at

play06:21

the same time the more replicas that we

play06:23

write to the more likely that there is

play06:24

to be a right conflict and you know

play06:26

there are kind of smart ways that we can

play06:28

try and get about avoiding this where we

play06:29

have certain rights going to the same

play06:31

replicas but at the same time you know

play06:34

it is good to kind of be able to know

play06:36

that your data is correct and you can

play06:37

avoid conflicts at all times

play06:40

okay we've gone over some stuff so let's

play06:43

actually go ahead and get started by

play06:45

talking about some legitimate database

play06:46

examples so the first one that I'm going

play06:49

to start with are SQL databases so the

play06:51

reason I'm not breaking SQL down into

play06:52

things like MySQL and postgres and

play06:55

things like that is because the truth of

play06:56

the matter is the feature set is

play06:58

generally similar between the SQL

play07:00

databases but it's more so that there

play07:01

are many different types of nosql

play07:03

databases so I'm going to break those

play07:04

down further but just for the sake of

play07:06

getting started let's go ahead and break

play07:08

down the important features of the SQL

play07:10

databases so the first thing is just the

play07:12

actual data model itself SQL is uh you

play07:15

know kind of biased towards using

play07:17

relational and normalized data what that

play07:19

means is that you don't basically

play07:21

duplicate data in multiple places but

play07:22

rather you'll have rows and then you can

play07:24

use joins in order to kind of reference

play07:26

pieces of data with one another

play07:27

throughout the tables what this means is

play07:30

that a lot of times when you're doing

play07:32

rights you may have to write to multiple

play07:34

different tables and if those tables are

play07:36

on multiple different nodes that can be

play07:38

a problem at scale the reason being

play07:40

if you want to write to two different

play07:42

tables and they're on two different

play07:43

computers for those to either both work

play07:45

or to not work we would need something

play07:47

like a two-phase commit protocol and

play07:49

I've spoken about two phase commit

play07:50

plenty in the past but the point is if

play07:52

you plan on guaranteeing that two rights

play07:54

are going to occur on two different

play07:55

computers and you know if one fails then

play07:57

neither occurs basically saying uh you

play08:00

want a transaction over multiple

play08:01

computers that is very expensive

play08:04

distributed transactions are pretty

play08:05

unreasonable in practice and as a result

play08:07

it's going to be hard to actually go

play08:09

ahead and Implement that additionally

play08:11

with SQL databases we have asset

play08:13

guarantees meaning that we're actually

play08:15

going to be implementing transactions

play08:17

which basically says every single

play08:19

transaction acts as if it were

play08:21

serializable right they can't go ahead

play08:23

and you know kind of read each other in

play08:26

the middle of a transaction you don't

play08:27

have to worry about race conditions you

play08:29

don't have to worry about a transaction

play08:31

partially succeeding or partially

play08:33

failing it's either all going to succeed

play08:34

or all going to fail and then finally

play08:37

we're also using B trees so B trees um

play08:40

like I mentioned versus LSM trees in

play08:42

particular are better for reading in

play08:45

theory but worse for writing and just to

play08:47

quickly go back on my point about

play08:48

transactions um kind of the issue with

play08:51

transactions are that even though it's

play08:52

good for the correctness of the data it

play08:55

also potentially slows down the entire

play08:56

database due to most of these SQL

play08:58

databases using an expensive two-phase

play09:01

locking scheme in order to ensure data

play09:03

correctness

play09:04

okay so what is the actual conclusion

play09:06

about when we want to be using SQL

play09:07

databases well you know based on

play09:10

everything I've kind of just said here

play09:11

it's really good when correctness is

play09:13

more important than speed right you know

play09:15

if we're ever trying to deal with

play09:16

transactions where we're modifying

play09:18

multiple rows in a table at once we want

play09:20

to make sure that all of those things

play09:21

happen or don't you know something like

play09:23

a bank transaction

play09:25

SQL is going to be super useful because

play09:27

we can't have it be the case where you

play09:28

know I get a hundred dollars and then

play09:30

you never lose your hundred dollars now

play09:32

as the bank I've just lost a hundred

play09:33

dollars so that doesn't work another

play09:35

good situation would be job scheduling

play09:37

if we have like a status table and we're

play09:39

you know editing multiple rows of the

play09:41

status table at the same time to do

play09:42

updates we want to make sure that those

play09:44

are relatively in sync and that all of

play09:46

those rows are either being changed

play09:47

should they're not but yeah I mean like

play09:49

I said SQL databases are kind of the

play09:51

default in this situation and I don't

play09:53

think the breakdown between the specific

play09:55

types are too important because at the

play09:57

end of the day they're kind of all based

play09:58

on the same Technologies but you know

play10:00

they may have some subtle differences in

play10:01

terms of feature sets

play10:03

okay so let's move on to our first nosql

play10:06

database which is actually going to be

play10:07

mongodb so mongodb is a document

play10:10

database which basically means that as

play10:12

opposed to storing data in rows now you

play10:14

have these documents which are basically

play10:16

just lists of items but then within that

play10:18

document you can have more nested

play10:20

documents so now you can store a ton of

play10:22

data in these Collections and you know

play10:24

if you actually wanted to go ahead and

play10:25

store your data that way it can provide

play10:27

you with really good data locality so

play10:30

just to give an example you know with

play10:32

SQL for example you could have a table

play10:34

of books and a table of authors and

play10:36

those might be stored on different

play10:37

computers and you know your author table

play10:39

might have an ID that helps you to fetch

play10:42

the relevant book rows right that being

play10:45

said in a document data model you know

play10:47

you might have one author document but

play10:49

all of those book documents will

play10:50

actually be stored within the author

play10:52

document which is really nice because if

play10:54

you're only going to be updating those

play10:56

at the same time it means that you can

play10:58

do that really easily you have great

play11:00

data locality at the same time though

play11:02

you know say you have multiple documents

play11:04

that rely on kind of the same piece of

play11:07

information if one piece of information

play11:09

gets updated but the other doesn't then

play11:11

those documents are out of sync and

play11:12

that's kind of the issue with

play11:13

denormalized data additionally as far as

play11:16

mongodb goes in terms of Technology

play11:18

under the hood I believe they do use

play11:20

transactions and also Beatrice so think

play11:23

of it as terms of like similar

play11:25

performance to SQL but you do get a

play11:26

little bit more flexibility in terms of

play11:28

the document data model

play11:30

that being said you know a lot of people

play11:31

are kind of familiar with relational

play11:33

databases and that data model so that

play11:35

seems to be why SQL is kind of still the

play11:37

norm but documents do have their own

play11:39

usage so again in terms of the systems

play11:41

to sign an interview I don't think

play11:42

mongodb is like particularly

play11:44

groundbreaking but if you just want kind

play11:46

of like the SQL technology with kind of

play11:49

a more advanced way of actually storing

play11:52

your data then can be a little bit

play11:54

useful

play11:55

okay let's move on to Cassandra I would

play11:58

say this is really the nosql database of

play12:00

choice that you should probably be

play12:01

listing in most of your systems assigned

play12:04

interviews and there are a few good

play12:06

reasons for that for starters it is a

play12:08

wide column data score which is

play12:10

essentially just an Excel spreadsheet

play12:12

more or less except you know other than

play12:14

the required sharding column and sorting

play12:17

column you can basically put any other

play12:19

columns in there and that can change

play12:20

from row to row which is really nice in

play12:22

terms of having data flexibility but

play12:25

additionally there's also going to be

play12:27

multi-leader and leaderless replication

play12:29

and this is configurable so what that

play12:31

means is you know you can choose to use

play12:33

something like Quorum rights but at the

play12:34

same time

play12:35

you can also just choose to you know

play12:37

have like one node kind of be the leader

play12:40

and propagate that out elsewhere or have

play12:42

just a couple nodes be the leaders but

play12:44

it's not necessarily going to be a

play12:46

quorum read or write so you don't always

play12:47

need the majority

play12:49

um that's really good because compared

play12:51

to single leader replication you can

play12:52

have much faster rights but at the same

play12:54

time it also puts into question the

play12:56

correctness of these rights if you're

play12:57

going to have right conflicts Cassandra

play12:59

in particular lets you use last right

play13:01

wins in order to resolve rate conflicts

play13:03

which isn't always ideal because it

play13:05

means that certain rights can be

play13:06

clobbered if they have the lower time

play13:08

stamp keep in mind that time stamps and

play13:10

distributed systems are not reliable

play13:12

unless you're using something like a GPS

play13:14

clock and we'll talk about that later

play13:15

with spanner

play13:16

but as long as that's the case it means

play13:19

that certain rights may be unfairly

play13:21

clobbered and then the last useful part

play13:23

about Cassandra is that they are using

play13:25

an LSM tree index which means there

play13:26

should be faster rights albeit slower

play13:29

reads okay so what's Cassandra really

play13:31

useful for um well it's really great

play13:33

when you want Hive right throughput and

play13:35

you know if you don't care as much about

play13:37

the consistency it's okay if the

play13:38

occasional piece of data is overwritten

play13:40

or lost then Cassandra is definitely

play13:42

going to be the way to go so something

play13:44

where this could be really useful is

play13:45

something like a Facebook messenger chat

play13:47

where you know maybe your sharding key

play13:49

would be like the chat ID and then from

play13:51

there you would have all of your

play13:52

messages ordered using timestamp as the

play13:55

sort key

play13:57

okay let's talk about rayak I've got a

play13:59

bunch of information listed here but the

play14:01

general point of ryak is this it's

play14:03

basically the same as Cassandra but one

play14:05

weakness of Cassandra that I mentioned

play14:07

was the fact that conflict resolution

play14:09

was completely done using last right

play14:11

wins on the other hand riak actually has

play14:13

something called crdts in order to help

play14:15

you with conflict resolution where

play14:17

basically crdts are these conflict-free

play14:19

replicated data types that act as ways

play14:22

of kind of aggregating certain

play14:24

conflicting rights and then allowing you

play14:25

to use more complex logic in order to

play14:28

actually resolve the conflicts this is

play14:30

useful if you want to make sure that

play14:31

you're not just having rights clobbered

play14:33

and it can allow you to perform things

play14:35

like sets and counters in a multi-leader

play14:38

or leaderless replication setup that are

play14:40

eventually consistent which is really

play14:42

great so again kind of the same use

play14:44

cases as Cassandra but it's just another

play14:45

nice point to touch upon that you can

play14:48

actually go ahead and you know kind of

play14:51

resolve those conflicts one piece of

play14:52

information that I did actually failed

play14:54

to mention on this slide is that I've

play14:55

wrote that it's a wide column data store

play14:57

it's actually a key value store so

play14:59

that's an error on my part

play15:01

okay

play15:02

next let's talk about Apache hbase so

play15:05

what are the key features of hbase well

play15:07

basically this is actually a wide column

play15:09

store so Sam basically you know kind of

play15:12

outer interface as Cassandra however

play15:14

there are some pretty subtle differences

play15:16

that do make a difference in performance

play15:17

so the first thing is that it is built

play15:20

on top of the hdfs or Hadoop distributed

play15:23

file system which means that you are

play15:25

using single leader replication you

play15:27

write to a single node and then from

play15:29

there that data gets replicated over

play15:30

other nodes so this is going to be

play15:33

slower than leaderless replication but

play15:35

at the same time it means we don't have

play15:36

to worry about write conflicts

play15:37

additionally the indexes are based off

play15:40

of the LSM tree so we should have

play15:42

relatively fast rights but the

play15:44

interesting thing about Apache hbase is

play15:46

that the actual storage itself instead

play15:48

of using a row wise storage model

play15:50

actually uses column wide storage which

play15:53

essentially means that instead of

play15:54

storing data within the same row

play15:56

together you actually store data within

play15:59

the same column of the table together

play16:00

and as a result you can achieve much

play16:02

better data locality when you're trying

play16:04

to read the entirety of just one column

play16:06

so that's really useful

play16:09

basically this is going to be really

play16:10

great for times where you know you're

play16:12

trying to get a very fast read on a

play16:14

column of data so for example you know

play16:16

let's say we're like Tick Tock or

play16:18

something or you know even PornHub here

play16:20

where when you actually press on a video

play16:21

it shows you a few images of that video

play16:23

that would be a great place to use

play16:25

Apache hbase because you can actually

play16:27

store that raw image data in the table

play16:30

itself and use column compression to

play16:32

quickly pull all of those images from

play16:34

that column whereas you know if it were

play16:36

row storage we would have to read

play16:37

multiple rows you'd have less data

play16:38

locality and the read would be slower

play16:41

okay let's next talk about memcache and

play16:44

redis so these are technically not the

play16:47

best database Solutions and the reason

play16:48

for that is that they are key value

play16:50

stores that are actually implemented in

play16:52

memory the subtle difference here is

play16:54

that redis I believe has a little bit

play16:55

more of a rich feature set you can do

play16:57

things like um geosharting and you know

play16:59

assorted sets and Hyper log logs but at

play17:02

the end of the day they're mostly just

play17:03

key value stores in memory and the point

play17:05

is is that instead of using anything

play17:07

like an SS table LSM tree combo or a B

play17:10

tree combo You're simply just using a

play17:12

hash map to store your data and so you

play17:13

don't need an index

play17:15

the one thing to note about a hash map

play17:17

is that it's worse for range queries so

play17:19

anyways the point about memcache and

play17:21

redis is that it's really useful for

play17:22

data where it's going to be you know

play17:25

written and read very frequently it's

play17:26

good for your most necessary data and

play17:29

especially you know for small data sets

play17:31

it's useful because you know storing

play17:33

stuff in memory is just a lot more

play17:34

expensive than storing things on disk so

play17:37

this is really great for things like

play17:39

caches or if you just have an essential

play17:41

part of your application that's getting

play17:42

written to and read from really often it

play17:44

can also be good for that so one example

play17:46

is actually the geospatial index in an

play17:49

app like Uber or Lyft where you see it's

play17:51

constantly being updated and read from

play17:53

with all the updated positions of

play17:54

drivers and Riders and so that's going

play17:57

to be very useful there

play17:58

okay next we have neo4j so this is a

play18:01

less popular one but the reason for that

play18:03

is that it's actually a graph database

play18:04

so to quickly explain the premise behind

play18:06

neo4j the point of a graph database is

play18:09

this if we wanted to implement a graph

play18:11

with a SQL database we could do it but

play18:14

it would have to be using a many-to-many

play18:15

relationship and the issue with the

play18:17

many-to-many relationship is that we

play18:19

would basically have a table with you

play18:21

know the IDS of two nodes and basically

play18:23

the fact that two nodes were in that

play18:25

many-to-many table together would

play18:27

indicate that there was an edge between

play18:28

them so you could do that however that's

play18:31

going to be very slow the reason for

play18:33

this being is as that table gets bigger

play18:35

we know that our index for that table is

play18:38

going to get bigger and the index keep

play18:41

in mind that the time complexity of

play18:43

reading from an index is proportional to

play18:45

log of the number of elements in that

play18:47

index so as the database table grows

play18:50

bigger our graph database would get

play18:51

slower if we implemented it using a SQL

play18:54

table instead neo4j is actually a native

play18:57

graft database which basically means

play18:59

that you have pointers to the actual

play19:01

location of the node corresponding to

play19:03

the other end of The Edge on disk so you

play19:05

can more quickly Traverse them in

play19:07

constant time as opposed to having to do

play19:09

logarithmic work every single time you

play19:11

want to Traverse a node so again this is

play19:13

kind of a niche topic I can't really

play19:15

think of too many systems to sign

play19:16

questions where they actually want you

play19:18

to use a graph database but if it ever

play19:20

does come up this is really useful for

play19:22

things like maybe map data and perhaps

play19:24

also something like modeling Out friends

play19:25

on social media

play19:28

okay another type of database that we're

play19:29

going to talk about are time series

play19:31

databases so you see I listed a couple

play19:33

examples at the top of this slide but

play19:35

generally the point is this time series

play19:37

databases are really useful for when

play19:39

you're modeling a bunch of ingestion

play19:41

from many different sources of data but

play19:43

also want to keep them in order relative

play19:45

to their timestamp right so let's say we

play19:47

have like five sensors and we want to

play19:49

model one day's worth of data for all of

play19:51

those sensors so the reason these are

play19:53

useful is they do use LSM trees and SS

play19:56

tables which means that you can have

play19:58

relatively fast ingestion because things

play20:00

go to that in-memory buffer first

play20:01

however they also take a little bit of a

play20:04

turn on them to kind of make them even

play20:06

more efficient for starters as opposed

play20:08

to having one huge LSM index for the

play20:10

entire table what they do instead is

play20:12

split the table into many small error

play20:14

indexes the reason for this being is

play20:17

that you're likely only going to need to

play20:19

be able to access a small chunk of data

play20:21

at a time in a Time series database and

play20:23

being able to make these indexes uh into

play20:25

a kind of small chunk indexes is going

play20:28

to allow you to put that entire index in

play20:30

memory the SS table I mean and as a

play20:33

result really quickly read from it and

play20:35

so you can use kind of the CPU cache

play20:37

much more efficiently by having all

play20:39

these smaller indexes additionally this

play20:41

is also really efficient for deleting

play20:43

those indexes because a lot of times

play20:45

with time series databases as the data

play20:46

gets too old you want to throw it out

play20:48

this is really inefficient with a

play20:50

typical LSM index where you wouldn't

play20:52

actually delete the data directly but

play20:54

you would put in a tombstone into the

play20:56

LSM tree where you're saying that a key

play20:58

is going to be deleted and then

play20:59

eventually when those SS table files get

play21:01

merged together that key is going to be

play21:03

thrown out however by actually just

play21:05

deleting the index itself this is a much

play21:07

faster process than having to wait for

play21:09

table compaction to occur so basically

play21:11

again pretty Niche kind of type of

play21:14

database but if you're ever dealing with

play21:15

assistance design problem about you know

play21:17

ingesting a ton of metrics or logs or

play21:20

sets or data or anything like that A

play21:22

Time series database is definitely a way

play21:24

to go for at least part of that problem

play21:27

okay kind of honorable mentions here

play21:29

that um you know probably won't come up

play21:31

as much in interviews unless you're

play21:32

having you know just like a fun

play21:33

discussion about databases first we have

play21:36

voltdb so new SQL is kind of the

play21:38

category that I'm talking about here and

play21:40

new SQL basically means they're SQL

play21:41

databases however they're implemented in

play21:44

interesting ways so as to get better

play21:45

performance than the traditional SQL

play21:47

database so Vault DB basically goes

play21:50

ahead and takes a SQL database however

play21:52

it runs it all in memory and it also

play21:54

runs it using only a single thread of a

play21:56

CPU so what this means is that there

play21:59

quite literally can't be any race

play22:00

conditions because there's only one

play22:02

thread being run you know there's no

play22:04

concurrent operations that are actually

play22:05

occurring and by running them all in

play22:08

memory we can basically make all these

play22:10

operations happen so fast that using

play22:12

single threaded execution is actually

play22:14

feasible for performance the issue with

play22:16

volt DB is obviously that it's going to

play22:18

be expensive it's running in memory and

play22:19

also it's only going to be good for

play22:21

small data sets because it's running in

play22:22

memory but it is kind of an interesting

play22:24

concept additionally we have spanner

play22:27

which is actually a Google product and

play22:29

the premise of spanner is that you're

play22:31

also using SQL however in order to kind

play22:33

of avoid having to do a ton of locking

play22:35

to figure out what right came after you

play22:37

know what right what it does instead is

play22:39

it puts GPS clocks in the actual data

play22:41

center itself in order to assign each

play22:43

right a relatively accurate timestamp

play22:45

and then use those timestamps to figure

play22:48

out you know kind of what right came

play22:50

after what and once you can order all of

play22:52

these rights it means that all of the

play22:53

databases are able to go in a consistent

play22:55

State and we can basically achieve

play22:57

strong consistency by virtue of

play22:58

linearizability spanner is also very

play23:01

expensive because we were no longer

play23:03

using commodity Hardware we're actually

play23:04

putting you know clocks in the data

play23:06

center which is not an easy thing to do

play23:08

so again you know cool discussions to

play23:11

have but also not really something you

play23:13

need to know as much a last kind of

play23:15

final honorable mention that I forgot to

play23:16

even make a slide about was just kind of

play23:18

data warehouses in general those are

play23:21

generally for SQL formats and probably

play23:23

after you would do some sort of batch

play23:24

work to properly format the data but

play23:27

yeah if it ever comes down to an

play23:28

analytic question something like a data

play23:30

warehouse could be very useful alrighty

play23:32

guys well I hope you enjoyed I'm not

play23:35

really one for redundancy normally and

play23:36

you know this is all information that's

play23:38

on my channel I highly encourage that

play23:40

you check out the full videos on these

play23:41

topics because there'll be a lot more in

play23:43

depth but at the end of the day I guess

play23:45

there is some value in terms of

play23:46

compilation and you know worse comes to

play23:48

worst hopefully it'll attract some new

play23:50

people to the channel who kind of you

play23:52

know haven't been exposed to these

play23:53

topics before so anyways have a good one

play23:55

look forward to seeing you all in the

play23:57

next video

Rate This

5.0 / 5 (0 votes)

Related Tags
DatabasesSystem DesignSQLNoSQLCassandraMongoDBTime SeriesData ModelingLSM TreeB-Tree