mongodb and mysql which one is a better fit for me (1)...how is acid represented in mysql? atomicity...

55
Room 204 - 2:20PM-3:10PM MongoDB and Mysql: Which one is a better fit for me?

Upload: others

Post on 12-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Room 204 - 2:20PM-3:10PM

MongoDB and Mysql: Which one is a better fit for me?

Page 2: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

About us● Adamo Tonete

○ MongoDB Support Engineer ● Agustín Gallego

○ MySQL Support Engineer

Page 3: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Agenda● What are MongoDB and MySQL; ● NoSQL and Relational concepts; ● Main differences between MySQL and MongoDB; ● MongoDB and MySQL similarities; ● Query Language; ● Performance comparison; ● Security; ● Best usage cases; ● Q&A

Page 4: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

What are MongoDB and MySQL

Page 5: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

What is MongoDB● Document Oriented Database ● NoSQL ● Open source ● It is currently the most common NoSQL database out there. ● High Performance Database ● Different storage engines for different use cases

Page 6: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

What is MySQL● Relational Database Management System ● The "M" in LAMP stack ● Second most popular RDBMS

○ According to DB-Engines' ranking ● Its architecture supports use of different storage engines ● Many different kinds of topologies used:

○ Master - Slave ○ Master - Master (active and passive) ○ Multimaster - Slave ○ Ring replication ○ Tree or pyramid ○ Multimaster Cluster (Group Replication or Galera Cluster)

Page 7: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

NoSQL and Relational concepts

Page 8: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Database ConceptA database is an organized collection of:

● Data ● Schemas ● Tables ● Queries ● Reports ● Views ● Other elements.

Wikipedia

Page 9: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Relational Database Concept● Written in the early 70s ● Records and attributes define relations ● Uses normalizations ● SQL Language ● Procedures ● Triggers ● Foreign keys ● Transactions - ACID

Page 10: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Non-Relational Database Concept● Started in the 2000s; ● Non-relational concept. No tables or normalization; ● Query language is different than standard SQL; ● Made for new programming languages. ● Fast development; ● Relies on CAP theorem.

Page 11: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Main differences between MySQL and MongoDB

Page 12: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Differences between MongoDB and MySQL● Some features we will compare:

○ Normalization ○ Transactions ○ Query language ○ Data storage and retrieval ○ Indexes differences ○ How to distribute and scale

Page 13: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

How different is MongoDB from MySQL/RDBS● NoSQL and SQL are not enemies

○ they are meant to complete each other

● While MongoDB is a young NoSQL database, MySQL is a mature relational database.

● In some cases, using MongoDB as the main database is not the best thing to do.

● However, MongoDB can offer a very fast growing environment without too much effort.

Page 14: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

How different is MongoDB from MySQL/RDBS● Comparing Data distribution:

○ MongoDB expects data to grow beyond machine limitations. ○ MySQL does have a few add-ons to allow data distribution among instances. ○ MySQL expects to work in a single machine (at least for writes). ○ MongoDB doesn't allow ACID transactions, but it works with the CAP theorem.

Page 15: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

NormalizationNormal forms

● MongoDB features best practices to organize your data, but there are no hard rules to do so.

● MySQL strongly suggests using 3rd normal form (3NF) to avoid data duplication.

Page 16: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Normalization

@ each intersection is a single scalar value

Page 17: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

{ "_id" : ObjectId("507f1f77bcf86cd799439011"), "studentID" : 100, "firstName" : "Jonathan", "middleName" : "Eli", "lastName" : "Tobin", "classes" : [ { "courseID" : "PHY101", "grade" : "B", "courseName" : "Physics 101", "credits" : 3 }, { "courseID" : "BUS101", "grade" : "B+", "courseName" : "Business 101", "credits" : 3 } ]

}

Normalization

Page 18: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

ACID transactions● What is ACID?

○ Atomicity ■ transactions should function as a single, indivisible unit of work

○ Consistency ■ the database should always move from one consistent state to the next

○ Isolation ■ the results of a transaction are (usually) invisible to other transactions until the

transaction is finished ○ Durability

■ once committed, a transaction's changes are permanent

Page 19: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

● How is ACID represented in MySQL? ○ Atomicity

■ if autocommit=ON (default), every statement is committed immediately ■ if not, COMMIT or ROLLBACK should be used explicitly

○ Consistency ■ uses the doublewrite buffer and crash recovery

○ Isolation ■ various isolation levels from which to choose from: RU, RC, RR and S

○ Durability ■ there are many configuration options available for this, among which are:

innodb_flush_log_at_trx_commit and sync_binlog

ACID transactions

Page 20: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

● How is ACID represented in MongoDB? ○ Atomicity

■ single document level & no snapshotting for reads ○ Consistency

■ primary = strong ■ secondaries = your choice

○ Isolation ■ not really, but $isolated can help

○ Durability ■ configurable w:majority and/or j:true

ACID transactions

Page 21: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

CAP theorem

● CAP theorem was proposed by Eric Allen in 2000

● A distributed system can't have the 3 guarantees at the same time. One must be sacrificed

Page 22: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

CAP theorem

● Consistence ● Availability ● Partition Tolerant

Anyone will get the same response, data is consistent among instances

A

PC

Page 23: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

CAP theorem

● Consistence ● Availability ● Partition Tolerant

System will always respond to requests, no downtime.

A

PC

Page 24: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

CAP theorem

● Consistence ● Availability ● Partition Tolerant

System can handle errors (network, hardware failure)

A

PC

Page 25: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

CAP theorem

A

PC

Relational DatabasesMySQL Postgres

CassandraRiaki

MongoDBRedis

Page 26: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

● MySQL has predefined table definitions ● Each column can have one (and only one) data type assigned to it ● There are some limits imposed:

○ columns: 4096 ○ row length: 64 Kb ○ these can change depending on which storage engine is used

● SQL is a declarative language ● We can tell MySQL what we want, without worrying about how it is looked for ● From the application side, there are connectors available for communicating

with the server ○ https://www.mysql.com/products/connector/

Data Storage and Data Retrieval

Page 27: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

● Unlike MySQL, MongoDB doesn't have a predefined schema but it does use declarative query language.

● Documents can have different fields with different data types, for example {x : 1, y : ['test']}

and {x : 'percona', y : ISODate('2018-01-01')}

are both valid MongoDB documents for the same collection.

Data Storage and Data Retrieval

Page 28: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

● MongoDB doesn't use 3rd form normalization but MySQL does.

● All documents must contain as much information as possible. There are no joins, only linked documents.

● Max document size is 16 MB.

Data Storage and Data Retrieval

Page 29: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

● Replica-sets ● Clusters and shards

● Master Slave

Comparing topologies

Page 30: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

● What is scalability? ○ "the ability to add capacity by adding resources"

● Scale up (a.k.a.: vertically) ○ improve hardware resources

● Scale out (a.k.a.: horizontally) ○ add more nodes

Scalability

Page 31: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

● MongoDB: ○ uses shards to scale writes ○ uses secondaries to scale reads

● MySQL: ○ can use partitioning and sharding to scale writes (but it's not easy to implement) ○ uses slaves to scale reads

Scalability

Page 32: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

MongoDB and MySQL similarities

Page 33: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

● But these databases are not completely different ● They share:

○ Security ○ Indexing ○ Multi-user access ○ Concurrency

How similar is MongoDB to MySQL

Page 34: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

● Database terms and concept mapping

How similar is MongoDB to MySQL

MySQL MongoDB

Database Database

Table Collection

Row Document

Column Key

Page 35: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Security: ● Granular security level ● User roles

Different storage engines: ● Both mongodb and MySQL share the idea of pluggable storage engine ● MongoDB engines are: WiredTiger, MMAPv1, InMemory, RocksDB ● MySQL engines are: InnoDB, MyISAM, MyRocks, Memory, and many more

How similar is MongoDB to MySQL

Page 36: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Query Language

Page 37: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

● We will compare mongo SQL and mysql SQL languages briefly ● and we'll see simple workflow for both:

○ create schema ○ create table ○ insert into table ○ select from table ○ update and delete ○ select with join (mysql only)

Query Language

Page 38: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Query Language - MySQL

Page 39: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Query Language - MySQL

Page 40: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

https://dev.mysql.com/doc/refman/5.7/en/select.html

SQL Definition

Page 41: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

MongoDB Query Language

Page 42: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

● NoSQL ● CQL ● Graph ● Javascript

"NoSQL" Query Language

Page 43: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Security

Page 44: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

● Both databases feature user and roles as well as enhanced security such as LDAP integration, certificates, and audits

● Percona Server for MongoDB and Percona Server for MySQL do offer entreprise-grade authentication plugins for free

Security

Page 45: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

● MongoDB has roles since version 2.4 ● Currently we can set collection at table level granularity ● LDAP is only available on MongoDB Enterprise but Percona server comes

with this plugin free of charge. ● Audit plugin

Security - MongoDB

Page 46: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

● Roles will be available on version 8.0+ ● We can set permission at database and table-level granularity ● Grants can be further refined into more atomic privileges

○ CREATE ○ SELECT ○ INSERT ○ UPDATE ○ ...

● MySQL Enterprise functionality (provided to some extent by Percona Server): ○ LDAP authentication ○ Encryption ○ Audit

Security - MySQL

Page 47: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Performance comparison

Page 48: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

There is no way to compare both performance. Each database has its own features and some are faster than others. As a document oriented database MongoDB doesn't a predefined schema and neither a relationship among collections which makes finds really fast (when reading only one document) For example: If using lookups in mongodb the query can be very slow.

In the other hand, mysql does that with majesty as it is design to work with tables and conjunctions...

Performance

Page 49: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Generic concepts to keep best performance

● Create indexes (not for all the fields) ● Split or purge data to keep the database small ● Be precise on your query (avoid reading unnecessary documents, columns) ● Use fast disks when the working set doesn't fit in RAM ● More cores means more parallel job and so on...

Performance

Page 50: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Best usage cases

Page 51: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

● There is no right nor wrong answer here although mongodb tend to be more used in application that doesn't require transactions (as its nature) mysql are often used when ACID is required.

https://www.percona.com/about-percona/customers

https://www.percona.com/about-percona/case-studies

Best Use case

Page 52: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Q&A

Page 53: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Q&A

Page 54: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

#54

Rate Our Session

Page 55: MongoDB and MySQL Which One Is a Better Fit for Me (1)...How is ACID represented in MySQL? Atomicity if autocommit=ON (default), every statement is committed immediately if not, COMMIT

Thank You!