Data Management in Large-Scale Distributed Systems Nosql Databases Fundamentals
Total Page:16
File Type:pdf, Size:1020Kb
Data Management in Large-Scale Distributed Systems NoSQL Databases Fundamentals Thomas Ropars [email protected] http://tropars.github.io/ 2021 1 References • The lecture notes of V. Leroy • The lecture notes of F. Zanon Boito • Designing Data-Intensive Applications by Martin Kleppmann I Chapters 2 and 7 2 In this lecture • Motivations for NoSQL databases • ACID properties and CAP Theorem • A landscape of NoSQL databases 3 Agenda Introduction Why NoSQL? Transactions, ACID properties and CAP theorem Data models 4 Common patterns of data accesses Large-scale data processing • Batch processing: Hadoop, Spark, etc. • Perform some computation/transformation over a full dataset • Process all data Selective query • Access a specific part of the dataset • Manipulate only the needed data I 1 record among millions • Main purpose of a database system 5 Processing / Database Link BatcH Job Stream Job e.g. senCment (Hadoop, Spark) (Spark, Storm) analysis results results data Write Write Load Database Serve queries Insert records e.g. Twier trends page Applicaon 1 Applicaon 2 Applicaon 3 3 Different types of databases • So far we used HDFS – A file system can be seen as a very basic database – Directories / files to organize data – Very simple queries (file system path) – Very good scalability, fault tolerance … • Other end of the spectrum: Relaonal Databases – SQL query language, very expressive – Limited scalability (generally 1 server) 4 Size / Complexity GrapH DB Relaonal DB Document DB Column DB Key/Value Complexity DB Filesystem Size 5 The NoSQL Jungle 6 Agenda Introduction Why NoSQL? Transactions, ACID properties and CAP theorem Data models 6 Relational databases SQL • Born in the 70's { Still heavily used • Data is organized into relations (in SQL: tables) • Each relation is an unordered collection of tuples (rows) • Foreign keys are used to define the relationships among the tables 7 About SQL Advantages • Separate the data from the code I High-level language I Space for optimization strategies • Powerful query language I Clean semantics I Operations on sets • Support for transactions 8 Motivations for alternative models see https://blog.couchbase.com/nosql-adoption-survey-surprises/ Some limitations of relational databases • Performance and scalability I Difficult to partition the data (in general run on a single server) I Need to scale up to improve performance • Lack of flexibility I Will to easily change the schema I Need to express different relations I Not all data are well structured • Few open source solutions • Mismatch between the relational model and object-oriented programming model 9 Illustration of the object-relational mismatch Figure by M. Kleppmann Figure: A CV in a relation database 10 Illustration of the object-relational mismatch Figure by M. Kleppmann f "user id":251, "first name":"Bill", "last name":"Gates", "summary":"Co −chair of the Bill& Melinda Gates; Active blogger.", "region id":"us:91", "industry id":131, "photo url":"/p/7/000/253/05b/308dd6e.jpg", "positions":[ f"job title":"Co −chair","organization":"Bill& Melinda Gates Foundation"g, f"job title":"Co −founder, Chairman","organization":"Microsoft" g ], "education":[ f"school name":"Harvard University","start": 1973,"end": 1975 g, f"school name":"Lakeside School, Seattle","start": null,"end": null g ], "contact info": f "blog":"http://thegatesnotes.com", "twitter":"http://twitter.com/BillGates" g g Figure: A CV in a JSON document 11 About NoSQL What is NoSQL? • A hashtag I NoSQL approaches were existing before the name became famous • No SQL • New SQL • Not only SQL I Relational databases will continue to exist alongside non-relational datastores 12 About NoSQL A variety of NoSQL solutions • Key-Value (KV) stores • Wide column stores (Column family stores) • Document databases • Graph databases Difference with relational databases There are several ways in which they differ from relational databases: • Properties • Data models • Underlying architecture 13 Agenda Introduction Why NoSQL? Transactions, ACID properties and CAP theorem Data models 14 Why do we need transactions? • Crashes may occur at any time I On the database side I On the application side I The network might not be reliable • Several clients may write to the database at the same time About transactions The concept of transaction • Groups several read and write operations into a logical unit • A group of reads and writes are executed as one operation: I The entire transaction succeeds (commit) I or the entire transaction fails (abort, rollback) • If a transaction fails, the application can safely retry 15 About transactions The concept of transaction • Groups several read and write operations into a logical unit • A group of reads and writes are executed as one operation: I The entire transaction succeeds (commit) I or the entire transaction fails (abort, rollback) • If a transaction fails, the application can safely retry Why do we need transactions? • Crashes may occur at any time I On the database side I On the application side I The network might not be reliable • Several clients may write to the database at the same time 15 ACID ACID describes the set of safety guarantees provided by transactions • Atomicity • Consistency • Isolation • Durability Having such properties make the life of developers easy, but: • ACID properties are not the same in all databases I It is not even the same in all SQL databases • NoSQL solutions tend to provide weaker safety guarantees I To have better performance, scalability, etc. 16 ACID: Atomicity Description • A transactions succeeds completely or fails completely I If a single operation in a transaction fails, the whole transaction should fail I If a transaction fails, the database is left unchanged • It should be able to deal with any faults in the middle of a transaction • If a transaction fails, a client can safely retry In the NoSQL context: • Atomicity is still ensured 17 ACID: Consistency Description • Ensures that the transaction brings the database from a valid state to another valid state I Example: Credits and debits over all accounts must always be balanced • It is a property of the application, not of the database I The database cannot enforce application-specific invariant • It is the responsibility of the programmer to issue transactions that make sense. I The database can check some specific invariant • A foreign key must be valid In the NoSQL context: • Consistency is (often) not discussed 18 ACID: Durability Description • Ensures that once a transaction has committed successfully, data will not be lost I Even if a server crashes (flush to a storage device, replication) In the NoSQL context: • Durability is also ensured 19 ACID: Isolation Description • Concurrently executed transactions are isolated from each other I We need to deal with concurrent transactions that access the same data • Serializability I High level of isolation where each transaction executes as if it was the only transaction applied on the database • As if the transactions are applied serially, one after the other I Many SQL solutions provide a lower level of isolation • Example: Read committed (see next slides) In the NoSQL context: • What about the CAP theorem? 20 More about isolation Alternative (weak) isolation levels Read Committed • No dirty reads: A read only sees data that has been committed • No dirty writes: A write only overwrites data that has been committed. Many SQL databases implement this level of isolation. 21 No dirty writes does not prevent all inconsistencies. Example of allowed execution with Read Committed Transaction to be executed (Increment a counter) Begin transaction Read count count = count + 1 Write count End transaction read count write 1 ClientA commit count=0 count=1 DB commit ClientB read count write 1 22 Example of allowed execution with Read Committed Transaction to be executed (Increment a counter) Begin transaction Read count count = count + 1 Write count End transaction read count write 1 ClientA commit count=0 count=1 DB commit ClientB read count write 1 No dirty writes does not prevent all inconsistencies. 22 balance A? Balance B? A+B= $900 ?? Alice commit DB commit Bank B - 100 A + 100 Some inconsistencies can also be observed with no dirty reads. Example of allowed execution with Read Committed 2 Transactions executed concurrently On the accounts of Alice: • initial state: $500 of the two accounts • Transaction 1: Transfer $100 from account B to A • Transaction 2: Check balance. 23 Some inconsistencies can also be observed with no dirty reads. Example of allowed execution with Read Committed 2 Transactions executed concurrently On the accounts of Alice: • initial state: $500 of the two accounts • Transaction 1: Transfer $100 from account B to A • Transaction 2: Check balance. balance A? Balance B? A+B= $900 ?? Alice commit DB commit Bank B - 100 A + 100 23 Example of allowed execution with Read Committed 2 Transactions executed concurrently On the accounts of Alice: • initial state: $500 of the two accounts • Transaction 1: Transfer $100 from account B to A • Transaction 2: Check balance. balance A? Balance B? A+B= $900 ?? Alice commit DB commit Bank B - 100 A + 100 Some inconsistencies can also be observed with no dirty reads. 23 The CAP theorem 3 properties of databases • Consistency I What guarantees do we have on the value returned by a read operation? I It strongly relates to Isolation in ACID (and not to consistency) • Availability I The system should always accept updates • Partition tolerance I The system