transactions in mongodb 4 - percona · 2019-06-03 · transactions was announced we will talk...

22
Transactions in MongoDB 4.0 Thursday, May 30, 2019

Upload: others

Post on 25-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

Transactions in MongoDB 4.0

Thursday, May 30, 2019

Page 2: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 2© The Pythian Group Inc., 2019 2

PYTHIAN

A global IT company that helps businesses leverage disruptive technologies to better compete.

Our services and software solutions unleash the power of cloud, data and analytics to drive better

business outcomes for our clients.

Our 20 years in data, commitment to hiring the best talent, and our deep technical and business expertise

allow us to meet our promise of using technology to deliver the best outcomes faster.

Page 3: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© 2017 Pythian. Confidential 3

Years in Business

20Pythian Experts

in 35 Countries

400+Current Clients

Globally

350+

Page 4: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 4© The Pythian Group Inc., 2019 4

DIVERSIFIED, BLUE CHIP CLIENT BASE

Media/Information

Services Retail E-commerce SaaSFinancial

Services

Page 5: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 5© The Pythian Group Inc., 2019 5

45 years of working with data

Started with flat files on Tape drives

Over the years have worked with:

ISAM, Codasyl Network databases, IBM IMS, Tandem SQL/MP

IBM DB2, Sybase ASE, Informix, MySQL, Cassandra, MongoDB

PostgreSQL and many others

Helped to migrate thousands of databases at AOL from Sybase to MySQL

Today I travel around the world helping Pythian’s customers get the best return on their

database investments.

About me

Page 6: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 6© The Pythian Group Inc., 2019 6

The fun stuff:

I like to travel

I love animals, Nature, National parks, Museums, Zoos and Gardens

I take a lot of pictures of flowers and landscapes. They are patient subjects

I take very few pictures of people. They are almost never patient subjects

About me Part II

Page 7: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 7© The Pythian Group Inc., 2019 7

● With the release last year of MongoDB 4.0 support for ACID

transactions was announced

● We will talk about;

● what ACID actually means and how it now applies to the MongoDB

platform

● Why it took so long for MongoDB to release transaction support

● When you should use transactions

● When you should not

● And some general rules about how you should use them

Intro

Page 8: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 8© The Pythian Group Inc., 2019 8

● ACID

● Atomic – All or none, an operation completes or it doesn’t

● Consistent – The database remains in a consistent state at al times for all users

● Isolated – What happens inside a transaction is invisible to other users until the transaction

commits

● Durable – If a transaction successfully commits the work it performed will remain and not

be lost

● ACID transactions mean you can update multiple documents within a transaction

and be sure that either all updates are committed or none are.

ACID

Page 9: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 9© The Pythian Group Inc., 2019 9

● Mentioned here because of that word consistent

● CAP

● Consistent – all updates are seen on all replicas at the same time

● Available – The database is always available

● Partitioned – The database can be accessed even if some nodes are

missing

● You can have any two but not all three.

● Mongo Replica Sets follow the CAP theorem

● The Consistent in CAP is usually not considered to be the same

Consistent as the Consistent in ACID

CAP Distributed Database Theorem

Page 10: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 10© The Pythian Group Inc., 2019 10

● Works only on single replica sets

● Sharded version scheduled for 4.2

● Is more expensive than operations not using transactions when

using Majority consistency

● Based on built in Wild Tiger engine transaction support

● Appears to work similarly to the XA standard for a replica set if write

and read concern are set to majority

● Works with eventual consistency if write concern is set to 1.

● Uses MVCC style Snapshot views for reads allowing reads to avoid

blocking

MongoDB 4.0 Transaction Feature

Page 11: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 11© The Pythian Group Inc., 2019 11

● MySQL and most other databases offer the concept of Isolation

levels

● Example: Read-uncommitted, Read-committed, Repeatable and

Serializable

● MongoDB offers two options Read-Uncommited (which is the

default) and Snapshot which is read-committed.

Isolation

Page 12: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 12© The Pythian Group Inc., 2019 12

Example Transaction using Python

with client.start_session() as s:

s.start_transaction()

collection_one.insert_one(doc_one, session=s)

collection_two.insert_one(doc_two, session=s)

s.commit_transaction()

Or if the transaction needs to fail

s.abort_transaction();

Page 13: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 13© The Pythian Group Inc., 2019 13

● MongoDB has always supported atomic operations on a single document

● When using MongoDB you should model you should not:

● Normalize your Tables/Collections

● Split related data apart unless necessary

● So the need was not as strong as it would normally be for a relational database

● Also transactions can be miss-used to cause severe concurrency problems in a

database

● But….

● Updates to multiple documents in the same collection aren’t protected without transactions

● Updates to related documents in other collections aren’t protected without transactions

Why did it take so long?

Page 14: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 14© The Pythian Group Inc., 2019 14

● Keep your transactions brief

● Wild Tiger has to keep a snapshot view of the database throughout the transaction. As

the transaction goes longer that can result in significant space consumption

● The longer a transaction lasts the greater the chance another client request will be

made to update documents in the current transaction.

● Never do a transaction that extends across user interactions

● Try not to update more than a few hundred documents in a single transaction

● When updating documents in different collections always use the same sequence

of updates in your code

● Limits the risk of deadlocks

Now they are here, How should you use Transactions

Page 15: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 15© The Pythian Group Inc., 2019 15

● Don’t use transactions to undo what you could easily do in your

code.

● Do your field edits first before you start a transaction

● Verify the funds are in the bank before you start your withdrawal

● Make sure you can probably complete the transaction before you start it

● Transaction rollbacks should be uncommon

Now they are here, How should you use Transactions

Page 16: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 16© The Pythian Group Inc., 2019 16

Don’t do this

with client.start_session() as s:

s.start_transaction()

collection_one.insert_one(doc_one, session=s)

User Interaction here

collection_two.insert_one(doc_two, session=s)

s.commit_transaction()

X

Page 17: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 17© The Pythian Group Inc., 2019 17

Do this

User Interaction

with client.start_session() as s:

s.start_transaction()

collection_one.insert_one(doc_one, session=s)

collection_two.insert_one(doc_two, session=s)

s.commit_transaction()

User Interaction

Page 18: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 18© The Pythian Group Inc., 2019 18

Don’t Do this

APP 1

with client.start_session() as s:

s.start_transaction()

collection_one.insert_one(doc_one, session=s)

collection_two.insert_one(doc_two, session=s)

s.commit_transaction()

APP 2

with client.start_session() as s:

s.start_transaction()

collection_two.insert_one(doc_one, session=s)

collection_one.insert_one(doc_two, session=s)

s.commit_transaction()

X

Page 19: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 19© The Pythian Group Inc., 2019 19

Do this

APP 1

with client.start_session() as s:

s.start_transaction()

collection_one.insert_one(doc_one, session=s)

collection_two.insert_one(doc_two, session=s)

s.commit_transaction()

APP 2

with client.start_session() as s:

s.start_transaction()

collection_one.insert_one(doc_one, session=s)

collection_two.insert_one(doc_two, session=s)

s.commit_transaction()

Page 20: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 20© The Pythian Group Inc., 2019 20

● Works only on single replica sets

● Is more expensive than operations not using transactions when

using Majority consistency

● Based on built in Wild Tiger engine transaction support

● Appears to work similarly to the XA standard for a replica set if write

and read concern are set to majority

● Works with eventual consistency if write concern is set to 1.

● Uses MVCC style Snapshot views for reads allowing reads to avoid

blocking

Summary

Page 21: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© The Pythian Group Inc., 2018 21

John Schulz

[email protected]

Thank You

Page 22: Transactions in MongoDB 4 - Percona · 2019-06-03 · transactions was announced We will talk about; what ACID actually means and how it now applies to the MongoDB platform Why it

© 2017 Pythian. Confidential 22