MongoDB change flow (Change Stream) Introduction

1. What is Stream Change 
Change Stream MongoDB is used to implement change tracking solutions, similar to trigger a relational database, but not exactly the same principle:

| | Change Stream | trigger |
| -------- ------ | ----------------- | --------------- |
| trigger | asynchronous | synchronization (affairs guarantee) |
| trigger position | application callback event | database triggers |
| trigger number | of clients each subscription event | 1 times (trigger) |
| recovery | retriggered from last breakpoint | transaction is rolled back |

2. Change Stream implement the principle of
Change Stream is based oplog achieve. It opens a tailable cursor on oplog to track copy operation on a set of changes,
eventually calling the callback function defined in the application. Change events are tracked include:

· INSERT / Update / the Delete: insert, update, delete;

drop:. Collection is deleted;

the rename:. Collection is renamed;

dropDatabase:. Database is deleted;

. Invalidate: drop / rename / dropDatabase will lead to invalidate is triggered, and close the Stream Change;

3. Change Stream and re-read
Change Stream only push change operation has been presented at the most nodes. That is "repeatable read" changes.
This is verified by {readConcern: "majority"} achieved. Thus:

· unopened majority readConcern unusable cluster Stream Change;

* if the cluster can not be met: When {w "majority"}, does not trigger Change Stream (e.g. PSA architecture downtime reason S).

4. Change Stream change the filter
if only interested in certain types of events change, the filtration step may be used to filter events Polymer pipes.
For example:
`` `
var db.collection.watch CS = ([{
$ match: {
OperationType: {
$ in: [ 'INSERT', 'Delete']
}
}
}])
` ``
5. The Stream Change Recovery
Assuming a series of writes, the subscription Change Stream of applications received "write 3" at time t0 after the crash, restart subsequent change how to do?
[! qr] (./ images / 026_t_1.png)
want to continue to get change from a stream where you left off, to keep only the last _id can change notification.
Shown on the right is a data Stream Change callback returned. Each such data with a _id, breakpoints can be used to recover the _id. For example:

var db.collection.watch CS = ([], {resumeAfter: <_ ID>})
to a notification from the interrupt continue receiving the subsequent change notification.
6. Change Stream usage scenarios
change copy * across the cluster - the cluster source subscription Change Stream, Once any changes immediately written to the target clusters.

· Micro Services linkage - When a micro database service changes, other micro-services to be informed and make changes in response.

· Any other required linkage scene.

Guess you like

Origin www.cnblogs.com/w3liu/p/12359333.html