Slide 1

Slide 1 text

It’s 10PM: Do you know where your writes are? Jeremy Mikola jmikola

Slide 2

Slide 2 text

It’s 11AM: Do you know where your writes are? Jeremy Mikola jmikola

Slide 3

Slide 3 text

On the roadmap Retryable writes Zombie cursor cleanup Cluster-wide killOp

Slide 4

Slide 4 text

Retryable Writes

Slide 5

Slide 5 text

Retryable Writes

Slide 6

Slide 6 text

Retryable Writes

Slide 7

Slide 7 text

You’re updating a document

Slide 8

Slide 8 text

You’re updating a document db.coll.updateOne( { _id: 16 }, { $inc: { count: 1 }} );

Slide 9

Slide 9 text

Murphy’s Law kicks in

Slide 10

Slide 10 text

Is it safe to retry the update?

Slide 11

Slide 11 text

Did our message never make it to the server?

Slide 12

Slide 12 text

Did we lose the server’s reply?

Slide 13

Slide 13 text

Did something else happen?

Slide 14

Slide 14 text

Did something else happen?

Slide 15

Slide 15 text

Did something else happen?

Slide 16

Slide 16 text

Did something else happen?

Slide 17

Slide 17 text

Did something else happen?

Slide 18

Slide 18 text

There’s no way to retrieve an operation’s state

Slide 19

Slide 19 text

Let’s review some best practices How To Write Resilient MongoDB Applications

Slide 20

Slide 20 text

This was our update… db.coll.updateOne( { _id: 16 }, { $inc: { count: 1 }} );

Slide 21

Slide 21 text

Errors and retry strategies Transient network error Persistent outage Command error Never retry Always retry Retry once

Slide 22

Slide 22 text

Errors and retry strategies Transient network error Persistent outage Command error Never retry May undercount Always retry Retry once

Slide 23

Slide 23 text

Errors and retry strategies Transient network error Persistent outage Command error Never retry May undercount OK Always retry Retry once

Slide 24

Slide 24 text

Errors and retry strategies Transient network error Persistent outage Command error Never retry May undercount OK OK Always retry Retry once

Slide 25

Slide 25 text

Errors and retry strategies Transient network error Persistent outage Command error Never retry May undercount OK OK Always retry May overcount Retry once

Slide 26

Slide 26 text

Errors and retry strategies Transient network error Persistent outage Command error Never retry May undercount OK OK Always retry May overcount Wastes time Retry once

Slide 27

Slide 27 text

Errors and retry strategies Transient network error Persistent outage Command error Never retry May undercount OK OK Always retry May overcount Wastes time Wastes time Retry once

Slide 28

Slide 28 text

Errors and retry strategies Transient network error Persistent outage Command error Never retry May undercount OK OK Always retry May overcount Wastes time Wastes time Retry once May overcount

Slide 29

Slide 29 text

Errors and retry strategies Transient network error Persistent outage Command error Never retry May undercount OK OK Always retry May overcount Wastes time Wastes time Retry once May overcount OK

Slide 30

Slide 30 text

Errors and retry strategies Transient network error Persistent outage Command error Never retry May undercount OK OK Always retry May overcount Wastes time Wastes time Retry once May overcount OK OK

Slide 31

Slide 31 text

There’s no good solution for transient network errors

Slide 32

Slide 32 text

We can safely retry idempotent operations Transient network error Persistent outage Command error Retry once

Slide 33

Slide 33 text

We can safely retry idempotent operations Transient network error Persistent outage Command error Retry once OK

Slide 34

Slide 34 text

We can safely retry idempotent operations Transient network error Persistent outage Command error Retry once OK OK

Slide 35

Slide 35 text

We can safely retry idempotent operations Transient network error Persistent outage Command error Retry once OK OK OK

Slide 36

Slide 36 text

Safe-to-retry inserts db.coll.insertOne( { _id: 18, name: "Alice" } );

Slide 37

Slide 37 text

Safe-to-retry deletes db.coll.deleteOne( { _id: 20 } ); db.coll.deleteMany( { status: "inactive" } );

Slide 38

Slide 38 text

Safe-to-retry updates db.coll.updateOne( { _id: 22 }, { $set: { status: "active" }} );

Slide 39

Slide 39 text

Why can’t we retrieve an operation’s state?

Slide 40

Slide 40 text

In MongoDB 3.4, state is tied to connection objects

Slide 41

Slide 41 text

MongoDB 3.6 introduces logical sessions

Slide 42

Slide 42 text

MongoDB 3.6 introduces logical sessions Sessions allow us to maintain cluster-wide state about the user and their operations.

Slide 43

Slide 43 text

MongoDB 3.6 introduces logical sessions Sessions allow us to maintain cluster-wide state about the user and their operations. Sessions are not tied to connections.

Slide 44

Slide 44 text

Retrying writes with a session

Slide 45

Slide 45 text

Retrying writes with a session

Slide 46

Slide 46 text

Retrying writes with a session

Slide 47

Slide 47 text

Retrying writes with a session update

Slide 48

Slide 48 text

Retrying writes with a session

Slide 49

Slide 49 text

Retrying writes with a session update

Slide 50

Slide 50 text

Retrying writes with a session

Slide 51

Slide 51 text

We can trust the server to Do the Right Thing™

Slide 52

Slide 52 text

We can trust the server to Do the Right Thing™ If the write already executed, return the result we missed.

Slide 53

Slide 53 text

We can trust the server to Do the Right Thing™ If the write already executed, return the result we missed. If the write never executed, do it now and return its result.

Slide 54

Slide 54 text

Sessions are cluster-wide

Slide 55

Slide 55 text

Sessions are cluster-wide update

Slide 56

Slide 56 text

Sessions are cluster-wide update

Slide 57

Slide 57 text

Sessions are cluster-wide

Slide 58

Slide 58 text

Sessions are cluster-wide

Slide 59

Slide 59 text

Sessions are cluster-wide update

Slide 60

Slide 60 text

Taking advantage of retryable writes ?retryWrites=true mongodb://…

Slide 61

Slide 61 text

One down, two to go Retryable writes Zombie cursor cleanup Cluster-wide killOp

Slide 62

Slide 62 text

Zombie Cursor Cleanup

Slide 63

Slide 63 text

Zombie Cursor Cleanup

Slide 64

Slide 64 text

You’re running a long query

Slide 65

Slide 65 text

You’re running a long query cursor = db.coll.find(); cursor.forEach(function() { // lengthy processing… });

Slide 66

Slide 66 text

You’re running a long query cursor = db.coll.find(); cursor.forEach(function() { // lengthy processing… });

Slide 67

Slide 67 text

Cursors have a timeout

Slide 68

Slide 68 text

Cursors have a timeout A er 10 minutes, the server will close a cursor due to inactivity.

Slide 69

Slide 69 text

Cursors have a timeout A er 10 minutes, the server will close a cursor due to inactivity. Issuing a getMore resets the clock.

Slide 70

Slide 70 text

Disabling cursor timeouts cursor = db.coll.find( { }, { noCursorTimeout: true } ); cursor.forEach(function() { // lengthy processing…

Slide 71

Slide 71 text

Disabling cursor timeouts cursor = db.coll.find( { }, { noCursorTimeout: true } ); cursor.forEach(function() { // lengthy processing…

Slide 72

Slide 72 text

Executing our long query

Slide 73

Slide 73 text

Executing our long query find

Slide 74

Slide 74 text

Executing our long query

Slide 75

Slide 75 text

Executing our long query getMore

Slide 76

Slide 76 text

Executing our long query

Slide 77

Slide 77 text

Executing our long query getMore

Slide 78

Slide 78 text

Executing our long query

Slide 79

Slide 79 text

Executing our long query getMore

Slide 80

Slide 80 text

Executing our long query getMore

Slide 81

Slide 81 text

Executing our long query getMore

Slide 82

Slide 82 text

Executing our long query getMore

Slide 83

Slide 83 text

Executing our long query getMore

Slide 84

Slide 84 text

Executing our long query getMore

Slide 85

Slide 85 text

Executing our long query getMore

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

A zombie cursor is born > db.serverStatus() { "metrics": { "cursor": { "open": { "noTimeout": 1, "total": 1

Slide 91

Slide 91 text

What happened last night?

Slide 92

Slide 92 text

What happened last night? (from the server’s POV)

Slide 93

Slide 93 text

What happened last night? (from the server’s POV)

Slide 94

Slide 94 text

What happened last night? (from the server’s POV) find

Slide 95

Slide 95 text

What happened last night? (from the server’s POV)

Slide 96

Slide 96 text

What happened last night? (from the server’s POV) getMore

Slide 97

Slide 97 text

What happened last night? (from the server’s POV)

Slide 98

Slide 98 text

What happened last night? (from the server’s POV) getMore

Slide 99

Slide 99 text

What happened last night? (from the server’s POV)

Slide 100

Slide 100 text

What happened last night? (from the server’s POV)

Slide 101

Slide 101 text

What happened last night? (from the server’s POV)

Slide 102

Slide 102 text

Avoiding zombie cursors with logical sessions

Slide 103

Slide 103 text

Avoiding zombie cursors with logical sessions Sessions also have a timeout.

Slide 104

Slide 104 text

Avoiding zombie cursors with logical sessions Sessions also have a timeout. We can associate queries with a session

Slide 105

Slide 105 text

Querying with a session session = client.startSession(); cursor = db.coll.find( { }, { session: session } );

Slide 106

Slide 106 text

Executing our long query

Slide 107

Slide 107 text

Executing our long query

Slide 108

Slide 108 text

Executing our long query

Slide 109

Slide 109 text

Executing our long query find

Slide 110

Slide 110 text

Executing our long query

Slide 111

Slide 111 text

Executing our long query getMore

Slide 112

Slide 112 text

Executing our long query

Slide 113

Slide 113 text

Executing our long query getMore

Slide 114

Slide 114 text

Executing our long query

Slide 115

Slide 115 text

Executing our long query

Slide 116

Slide 116 text

Executing our long query session expires

Slide 117

Slide 117 text

Executing our long query

Slide 118

Slide 118 text

Did we just punt on the timeout issue?

Slide 119

Slide 119 text

Session timeouts are non-negotiable

Slide 120

Slide 120 text

Session timeouts are non-negotiable Idle sessions will expire.

Slide 121

Slide 121 text

Session timeouts are non-negotiable Idle sessions will expire. Any operation using the session resets the clock.

Slide 122

Slide 122 text

Two down, one to go Retryable writes Zombie cursor cleanup Cluster-wide killOp

Slide 123

Slide 123 text

Cluster-wide killOp

Slide 124

Slide 124 text

Cluster-wide killOp

Slide 125

Slide 125 text

You’re running an operation that may never complete

Slide 126

Slide 126 text

You’re running an operation that may never complete cursor = db.coll.find( { … } // table scans for days );

Slide 127

Slide 127 text

You’ve made a terrible mistake

Slide 128

Slide 128 text

Step 1: Find the operation ID > db.currentOp() { "inprog" : [ { "desc" : "conn2", "threadId" : "140181791471360", "connectionId" : 2, "client" : "127.0.0.1:49456", "appName" : "MongoDB Shell", "active" : true, "opid" : 132921,

Slide 129

Slide 129 text

Step 2: Kill the operation ID > db.killOp(132921) { "info": "attempting to kill op", "ok": 1 }

Slide 130

Slide 130 text

Lather, rinse, repeat

Slide 131

Slide 131 text

Lather, rinse, repeat > connect("mongodb://shard-2.example.com")

Slide 132

Slide 132 text

Lather, rinse, repeat > connect("mongodb://shard-2.example.com") > db.currentOp() { "inprog" : [ // … ] }

Slide 133

Slide 133 text

Lather, rinse, repeat > connect("mongodb://shard-2.example.com") > db.currentOp() { "inprog" : [ // … ] } > db.killOp(…)

Slide 134

Slide 134 text

Lather, rinse, repeat > connect("mongodb://shard-2.example.com") > db.currentOp() { "inprog" : [ // … ] } > db.killOp(…)

Slide 135

Slide 135 text

How did this happen? mongos shard 1 shard 2 shard 3

Slide 136

Slide 136 text

How did this happen? mongos shard 1 shard 2 shard 3

Slide 137

Slide 137 text

How did this happen? mongos shard 1 shard 2 shard 3

Slide 138

Slide 138 text

Cluster-wide killOp with logical sessions

Slide 139

Slide 139 text

Cluster-wide killOp with logical sessions Any operation may be associated with a session.

Slide 140

Slide 140 text

Cluster-wide killOp with logical sessions Any operation may be associated with a session. Terminating a session will end all of its associated operations.

Slide 141

Slide 141 text

Terminating a session session = client.startSession(); cursor = db.coll.find( { … }, // table scans for days { session: session } );

Slide 142

Slide 142 text

Querying with sessions mongos shard 1 shard 2 shard 3

Slide 143

Slide 143 text

Querying with sessions mongos shard 1 shard 2 shard 3

Slide 144

Slide 144 text

Querying with sessions mongos shard 1 shard 2 shard 3

Slide 145

Slide 145 text

Querying with sessions mongos shard 1 shard 2 shard 3

Slide 146

Slide 146 text

Querying with sessions mongos shard 1 shard 2 shard 3

Slide 147

Slide 147 text

Querying with sessions mongos shard 1 shard 2 shard 3

Slide 148

Slide 148 text

Querying with sessions mongos shard 1 shard 2 shard 3

Slide 149

Slide 149 text

Querying with sessions mongos shard 1 shard 2 shard 3

Slide 150

Slide 150 text

Querying with sessions mongos shard 1 shard 2 shard 3

Slide 151

Slide 151 text

Querying with sessions mongos shard 1 shard 2 shard 3

Slide 152

Slide 152 text

That’s a wrap Retryable writes Zombie cursor cleanup Cluster-wide killOp

Slide 153

Slide 153 text

One last point

Slide 154

Slide 154 text

Resilence is primarily the driver’s domain

Slide 155

Slide 155 text

Resilence is primarily the driver’s domain Server discovery and monitoring

Slide 156

Slide 156 text

Resilence is primarily the driver’s domain Server discovery and monitoring Elections and failover recovery

Slide 157

Slide 157 text

Resilence is primarily the driver’s domain Server discovery and monitoring Elections and failover recovery Load-balancing mongos connections

Slide 158

Slide 158 text

Resilence is primarily the driver’s domain Server discovery and monitoring Elections and failover recovery Load-balancing mongos connections Routing queries by read preference

Slide 159

Slide 159 text

Addressing resilence on the server-side

Slide 160

Slide 160 text

Addressing resilence on the server-side Tracking operation state

Slide 161

Slide 161 text

Addressing resilence on the server-side Tracking operation state Cluster-wide sessions

Slide 162

Slide 162 text

Providing a relatively easy upgrade path

Slide 163

Slide 163 text

Providing a relatively easy upgrade path No need to rewrite applications

Slide 164

Slide 164 text

Providing a relatively easy upgrade path No need to rewrite applications Opting in to retryable writes

Slide 165

Slide 165 text

Providing a relatively easy upgrade path No need to rewrite applications Opting in to retryable writes New API for client session objects

Slide 166

Slide 166 text

Providing a relatively easy upgrade path No need to rewrite applications Opting in to retryable writes New API for client session objects Pass session option as needed

Slide 167

Slide 167 text

Inside the spec process mongodb/specifications

Slide 168

Slide 168 text

Inside the spec process /sessions mongodb/specifications

Slide 169

Slide 169 text

Inside the spec process /sessions /retryable-writes mongodb/specifications

Slide 170

Slide 170 text

Inside the spec process /sessions /retryable-writes /causal-consistency mongodb/specifications

Slide 171

Slide 171 text

Inside the spec process /sessions /retryable-writes /causal-consistency /retryable-reads mongodb/specifications

Slide 172

Slide 172 text

In the meantime… How To Write Resilient MongoDB Applications

Slide 173

Slide 173 text

Thanks!