SlideShare a Scribd company logo
#MDBlocal
Using Change Streams to Keep
Up With Your Data
24th April, 2018 | Bengaluru
#MDBlocal
Ankit Kakkar
Technical Services Manager | MongoDB
#MDBlocal
1. Change Stream Characteristics
2. Operation Types
3. Development Tips
4. Underlying Technology
AGENDA
#MDBlocal
-- Terry Pratchett
#MDBlocal
REAL-TIME IS AN EXPECTATION
#MDBlocal
INTRODUCING: CHANGE STREAMS
Allows you to watch all the changes against a given collection.
CHANGESTREAMSAPI
Event Notifications
● Application
● MongoDB Stitch
● Message Queue
#MDBlocal
CHANGE STREAMS: USE CASES
● Refreshing trading apps as
stock prices change
● Syncing changes across
microservices
● Updating dashboards,
analytics systems, search
engines
● IOT data pipelines - generating
alarms in response to connected
asset failures
● Push new credit card transaction
into Machine Learning models to
recalculate risk
● Maintaining multiplayer game
scoreboards.
#MDBlocal
MongoD
B
Action
Handle
r
#MDBlocal
changeStream = db.collection(“foo”).watch();
changeStream.on(“change”, function(change){
console.log(change)
});
#MDBlocal
CHANGE STREAM
CHARACTERISTICS
#MDBlocal
CHANGE STREAMS UTILISE
COLLECTION ACCESS CONTROLS,
PRESENT A DEFINED API, AND
ENABLE SCALING ACROSS
PRIMARIES AND SECONDARIES.
#MDBlocal
SHARD
A
SHARD
C
SHARD
B
mongos
TOTAL ORDERING OF CHANGES
ACROSS SHARDS
#MDBlocal
SHARD
A
SHARD
C
SHARD
B
mongos
TOTAL ORDERING OF CHANGES
ACROSS SHARDS
1 2 3
#MDBlocal
XPRIMARY
PRIMARY
CHANGES ARE DURABLE
SECONDARY
SECONDARY
#MDBlocal
X
PRIMARY
CHANGES ARE RESUMABLE
SECONDARY
{_id: <resumeToken>,
operationType: ‘update’,
ns: {db: ‘data’, coll: ‘temperature’},
documentKey: {_id: ‘room12345’},
updateDescription: {updatedFields: {temperature: 27},
removedFields: [‘humidity’]}
}
SECONDARY
var newChangeStream = coll.watch({
resumeAfter: <cachedResumeToken>
});
#MDBlocal
CHANGE STREAMS UTILISE THE POWER
OF THE AGGREGATION FRAMEWORK
$match $project $addFields $replaceRoot $redact
var changeStream = coll.watch([
{$match: {$or: [{operationType: 'delete' },
{operationType:'replace'}]}}
]);
#MDBlocal
1. COLLECTION ACCESS CONTROLS
2. DEFINED API
3. ENABLE SCALING
4. TOTAL ORDERING
5. DURABLE
6. RESUMABLE
7. POWER OF AGGREGATION
#MDBlocal
OPERATION TYPES
#MDBlocal
5 OPERATION TYPES
DELETE
INSERT
REPLACE
UPDATE
INVALIDATE
#MDBlocal
{ _id: (resumeToken),
operationType: ‘insert’,
ns: {db: ‘test’, coll: ‘foo’},
documentKey: {userName: ‘anish123’,
_id: ObjectId("58a4eb4a30c75625e00d2820")},
fullDocument: {_id: ObjectId("58a4eb4a30c75625e00d2820"),
userName: ‘anish123’,
name: ‘Anish Bhanwala’}
}
5 OPERATION TYPES: INSERT
#MDBlocal
DEFINING THE documentKey
{ _id: (resumeToken),
operationType: ‘insert’,
ns: {db: ‘test’, coll: ‘foo’},
documentKey: {userName: ‘anish123’,
_id: ObjectId("58a4eb4a30c75625e00d2820")},
fullDocument: {_id: ObjectId("58a4eb4a30c75625e00d2820"),
userName: ‘anish123’,
name: ‘Anish Bhanwala’}
}
#MDBlocal
DEFINING THE documentKey
The “_id” field of the document created or modified by
the operation.
● Replica Set : ‘_id’
● Sharded Cluster : ‘_id’ + ‘shard key’
#MDBlocal
{_id: (resumeToken),
operationType: ‘update’,
ns: {db: ‘test’, coll: ‘foo’},
documentKey: {userName: ‘neerajchopra’,
_id: ObjectId("58a4eb4a30c75625e00d2820")},
updateDescription: {updatedFields: {email: ‘neeraj@cwg’},
removedFields: [‘phoneNumber’]}
}
5 OPERATION TYPES: UPDATE
#MDBlocal
UPDATE
changeStream = coll.watch([], {fullDocument:'updateLookup'});
{_id: (resumeToken),
operationType: ‘update’,
ns: {db: ‘test’, coll: ‘foo’},
documentKey: {userName: ‘neerajchopra’,
_id: ObjectId("58a4eb4a30c75625e00d2820")},
updateDescription: {updatedFields: {email: ‘neeraj@cwg’},
removedFields: [‘phoneNumber’]}
}
#MDBlocal
UPDATE
fullDocument
var changeStream = coll.watch( {fullDocument:'updateLookup'} );
{_id: (resumeToken),
operationType: ‘update’,
ns: {db: ‘test’, coll: ‘foo’},
documentKey: {userName: ‘neerajchopra’,
_id: ObjectId("58a4eb4a30c75625e00d2820")},
updateDescription: {updatedFields: {email: ‘neeraj@cwg’},
removedFields: [‘phoneNumber’]},
fullDocument: { _id: ObjectId("58a4eb4a30c75625e00d2820"),
name: ‘Neeraj Chopra’,
userName: ‘neerajchopra’,
email: ‘neeraj@cwg’,
team: ‘replication’}
}
#MDBlocal
{_id: (resumeToken),
operationType: ‘replace’,
ns: {db: ‘test’, coll: ‘foo’},
documentKey: {userName: ‘manika123’,
_id:
ObjectId("58a4eb4a30c75625e00d2820")},
fullDocument: {_id: ObjectId("58a4eb4a30c75625e00d2820"),
userName: ‘manika123’,
name: ‘Manika Batra’}
}
5 OPERATION TYPES: REPLACE
#MDBlocal
{_id: (resumeToken),
operationType: ‘delete’,
ns: {db: ‘test’, coll: ‘foo’},
documentKey: {userName: ‘alice123’,
_id:
ObjectId("58a4eb4a30c75625e00d2820")
}
}
5 OPERATION TYPES: DELETE
#MDBlocal
5 OPERATION TYPES: INVALIDATE
{_id: (resumeToken),
operationType: ‘invalidate’,
ns: {db: ‘test’, coll: ‘foo’}
}
#MDBlocal
DEVELOPMENT TIPS
#MDBlocal
MongoD
B
Action
Handle
r
#MDBlocal
#MDBlocal
Action Handler
var changeStream = coll.watch(
[{ $match: { 'fullDocument.temperature': { $gte:27 }}],
{ fullDocument: 'updateLookup' });
{_id: (resumeToken),
operationType: ‘insert’,
ns: {db: ‘data’, coll: ‘temperature’},
documentKey: {_id: ‘LivingRoomGreen’},
fullDocument: {_id: ‘LivingRoomGreen’),
temperature: 30,
username: ‘ankit_kakkar’}
}
temperature: 30
username: ankit_kakkar
#MDBlocal
Action Handler
var changeStream = coll.watch(
[{ $match: { 'fullDocument.temperature': { $gte:27 }}],
{ fullDocument: 'updateLookup' });
{_id: (resumeToken),
operationType: ‘insert’,
ns: {db: ‘data’, coll: ‘temperature’},
documentKey : {_id: ‘OrangeRoomC’
fullDocument: {_id: ‘OrangeRoomC’,
temperature: 29,
username: ‘Ankur_raina’ }
}
temperature: 30
username: ankit_kakkar
#MDBlocal
Action Handler
var changeStream = coll.watch(
[{ $match: { 'fullDocument.temperature': { $gte:27 }}],
{ fullDocument: 'updateLookup' });
{_id: (resumeToken),
operationType: ‘update’,
ns: {db: ‘data’, coll: ‘temperature’},
documentKey: {_id: ‘LivingRoomGreen’)},
updateDescription: {updatedFields: {temperature: 25}
fullDocument: {_id: ‘LivingRoomGreen’,
temperature: 25,
username: ‘ankit_kakkar’}
}
temperature: 30
username: ankit_kakkar
#MDBlocal
IF YOUR APPLICATION REQUIRES STATE
ALWAYS MATCH ON UNCHANGING
FIELDS
#MDBlocal
Action Handler
var changeStream = coll.watch(
[{ $match: { 'fullDocument.username: ‘ankit_kakkar’ }],
{ fullDocument: 'updateLookup' });
{_id: (resumeToken),
operationType: ‘insert’,
ns: {db: ‘data’, coll: ‘temperature’},
documentKey: {_id: ‘blueRoomB’},
fullDocument: {_id: ‘blueRoomB’,
temperature: 32,
username: ‘ankit_kakkar’}
}
temperature: 30
username: ankit_kakkar
temperature: 32
username: ankit_kakkar
#MDBlocal
Action Handler
var changeStream = coll.watch(
[{ $match: { 'fullDocument.username: ‘ankit_kakkar’ }],
{ fullDocument: 'updateLookup' });
{_id: (resumeToken),
operationType: ‘delete’,
ns: {db: ‘data’, coll: ‘temperature’},
documentKey: {_id: ‘blueRoomB’}
}
temperature: 30
username: ankit_kakkar
temperature: 32
username: ankit_kakkar
#MDBlocal
Action Handler
var changeStream = coll.watch(
[{ $match: { 'fullDocument.username: ‘ankit_kakkar’ }],
{ fullDocument: 'updateLookup' });
{_id: (resumeToken),
operationType: ‘replace’,
ns: {db: ‘data’, coll: ‘temperature’},
documentKey : {_id: ‘blueRoomB’},
fullDocument: {_id: ‘blueRoomB’,
temperature: 29,
username: ‘nishant’}
}
temperature: 30
username: ankit_kakkar
temperature: 32
username: ankit_kakkar
#MDBlocal
IF YOUR APPLICATION NEEDS TO NOTIFY ON
DELETED DATA
ALWAYS HANDLE DELETES AND
REPLACES APPROPRIATELY
#MDBlocal
IF YOU NEED TO SEE EVERY CHANGE (EVEN
OUTDATED CHANGES) TO A DOCUMENT
HAVE MATCHING FIELDS IN THE
DOCUMENT KEY
#MDBlocal
Action Handler
var changeStream = coll.watch(
[{ $match: { ‘documentKey._id.username’: ‘ankit_kakkar’}],
{ fullDocument: 'updateLookup' });
{_id: (resumeToken),
operationType: ‘insert’,
ns: {db: ‘data’, coll: ‘temperature’},
documentKey: {_id: {roomId: ‘blueRoomB’,
username: ‘ankit_kakkar’}},
fullDocument: {_id: {roomId:‘blueRoomB’,
username:’ankit_kakkar’},
temperature: 32}
}
temperature: 30
username: ankit_kakkar
temperature: 32
username: ankit_kakkar
#MDBlocal
Action Handler
var changeStream = coll.watch(
[{ $match: { ‘documentKey._id.username’: ‘ankit_kakkar’}],
{ fullDocument: 'updateLookup' });
{_id: (resumeToken),
operationType: ‘delete’,
ns: {db: ‘data’, coll: ‘temperature’},
documentKey: {_id: {roomId: ‘blueRoomB’,
username:
‘ankit_kakkar’}}
}
temperature: 30
username: ankit_kakkar
temperature: 32
username: ankit_kakkar
#MDBlocal
Action Handler
var changeStream = coll.watch(
[{ $match: { ‘documentKey._id.username’: ‘ankit_kakkar’}],
{ fullDocument: 'updateLookup' });
{_id: (resumeToken),
operationType: ‘replace’,
ns: {db: ‘data’, coll: ‘temperature’},
documentKey: {_id: {roomId:‘LivingRoomGreen’,
username: ‘ankit_kakkar’}},
fullDocument: {_id: {roomId: ‘LivingRoomGreen’,
username: ‘nishant’},
temperature: 26}
}
temperature: 30
username: ankit_kakkar
#MDBlocal
1. If your application requires state:
Match on unchanging fields.
2. If your application needs to notify on deleted data:
Handle deletes and replaces appropriately.
3. If your application needs to see every change
(even outdated changes):
Have matching fields in the document key.
#MDBlocal
UNDERLYING
TECHNOLOGY
#MDBlocal
REPLICATION
#MDBlocal
The secondaries replicate the primary’s
oplog and apply the operations to their
data sets such that the secondaries’ data
sets reflect the primary’s data set.
REPLICATION
#MDBlocal
4 3 2 15
CAPPED COLLECTION (OPLOG)
#MDBlocal
5 4 3 26
CAPPED COLLECTION (OPLOG)
#MDBlocal
7 6 5 4 3 2 1
CACHED RESUME TOKEN : 3
NEWEST OLDEST
DELETED
RESUMABLE
#MDBlocal
7 6 5 4 3 2 1
CACHED RESUME TOKEN : 1
NEWEST OLDEST
DELETED
NON-RESUMABLE
#MDBlocal
HOW TO GET STARTED
● MongoDB v3.6 (WiredTiger)
● Replica Set (Data bearing node)
#MDBlocal
QUESTIONS ?

More Related Content

PPTX
[MongoDB.local Bengaluru 2018] Just in Time Validation with JSON Schema
PPTX
[MongoDB.local Bengaluru 2018] Introduction to MongoDB Stitch
PPTX
[MongoDB.local Bengaluru 2018] Keynote
PPTX
MongoDB BI Connector & Tableau
PPTX
MongoDB Stitch Introduction
PPT
Ken 20150306 心得分享
PPTX
[MongoDB.local Bengaluru 2018] Jumpstart: Introduction to Schema Design
PPTX
Performance and Security Enhancements in MongoDB's BI Connector
[MongoDB.local Bengaluru 2018] Just in Time Validation with JSON Schema
[MongoDB.local Bengaluru 2018] Introduction to MongoDB Stitch
[MongoDB.local Bengaluru 2018] Keynote
MongoDB BI Connector & Tableau
MongoDB Stitch Introduction
Ken 20150306 心得分享
[MongoDB.local Bengaluru 2018] Jumpstart: Introduction to Schema Design
Performance and Security Enhancements in MongoDB's BI Connector

What's hot (20)

PDF
Dropwizard with MongoDB and Google Cloud
PPTX
Jumpstart: Building Your First App with MongoDB
PDF
VBA API for scriptDB primer
PDF
Authorization iii
PPTX
Using Change Streams to Keep Up with Your Data
PPTX
Event-Driven Systems With MongoDB
PDF
Bringing Transactional Guarantees to MongoDB
KEY
Introduction to Restkit
PPTX
KEYNOTE: Node.js interactive 2017 - The case for node.js
PPTX
MongoDB.local Sydney: How and When to Use Multi-Document Distributed Transact...
PPTX
Testando a integracao entre serviços - Agile Brazil 2014
PDF
CQRS & event sourcing in the wild
PPTX
From CRUD to messages: a true story
PPTX
Goa tutorial
PPTX
Wireless pres ba
PDF
Seeking the truth from mobile analytics
PDF
MongoDB and its usage
PDF
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDB
PDF
Programming services-slides
PPTX
Javascript 2
Dropwizard with MongoDB and Google Cloud
Jumpstart: Building Your First App with MongoDB
VBA API for scriptDB primer
Authorization iii
Using Change Streams to Keep Up with Your Data
Event-Driven Systems With MongoDB
Bringing Transactional Guarantees to MongoDB
Introduction to Restkit
KEYNOTE: Node.js interactive 2017 - The case for node.js
MongoDB.local Sydney: How and When to Use Multi-Document Distributed Transact...
Testando a integracao entre serviços - Agile Brazil 2014
CQRS & event sourcing in the wild
From CRUD to messages: a true story
Goa tutorial
Wireless pres ba
Seeking the truth from mobile analytics
MongoDB and its usage
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDB
Programming services-slides
Javascript 2
Ad

Similar to [MongoDB.local Bengaluru 2018] Using Change Streams to Keep Up With Your Data (20)

PPTX
Using Change Streams to Keep Up with Your Data
PPTX
Using Change Streams to Keep Up with Your Data
PPTX
Using Change Streams to Keep Up with Your Data
PPTX
SH 1 - SES 7 - Change-Streams-Tel-Aviv.pptx
PPTX
Insight on MongoDB Change Stream - Abhishek.D, Mydbops Team
PDF
MongoDB World 2018: Using Change Streams to Keep Up with Your Data
PPTX
MongoDB World 2018: Time for a Change Stream - Using MongoDB Change Streams t...
PPTX
MongoDB World 2018: Time for a Change Stream - Using MongoDB Change Streams t...
PPTX
MongoDB.local DC 2018: Scaling Realtime Apps with Change Streams
PDF
MongoDB .local Toronto 2019: Using Change Streams to Keep Up with Your Data
PDF
MongoDB Meetup
PPTX
Mongo db tips and advance features
PDF
MongoDB World 2019: High Performance Auditing of Changes Based on MongoDB Cha...
PPTX
What's new in MongoDB 3.6?
PDF
Getting Started with MongoDB: 4 Application Designs
PPTX
MongoDB Workshop.pptx computer science and engineering
PDF
MongoDB .local Chicago 2019: Using MongoDB Transactions to Implement Cryptogr...
PPTX
Neue Features in MongoDB 3.6
PDF
Evolving your Data Access with MongoDB Stitch
PPTX
MongoDB.local DC 2018: Ch-Ch-Ch-Ch-Changes: Taking Your MongoDB Stitch Applic...
Using Change Streams to Keep Up with Your Data
Using Change Streams to Keep Up with Your Data
Using Change Streams to Keep Up with Your Data
SH 1 - SES 7 - Change-Streams-Tel-Aviv.pptx
Insight on MongoDB Change Stream - Abhishek.D, Mydbops Team
MongoDB World 2018: Using Change Streams to Keep Up with Your Data
MongoDB World 2018: Time for a Change Stream - Using MongoDB Change Streams t...
MongoDB World 2018: Time for a Change Stream - Using MongoDB Change Streams t...
MongoDB.local DC 2018: Scaling Realtime Apps with Change Streams
MongoDB .local Toronto 2019: Using Change Streams to Keep Up with Your Data
MongoDB Meetup
Mongo db tips and advance features
MongoDB World 2019: High Performance Auditing of Changes Based on MongoDB Cha...
What's new in MongoDB 3.6?
Getting Started with MongoDB: 4 Application Designs
MongoDB Workshop.pptx computer science and engineering
MongoDB .local Chicago 2019: Using MongoDB Transactions to Implement Cryptogr...
Neue Features in MongoDB 3.6
Evolving your Data Access with MongoDB Stitch
MongoDB.local DC 2018: Ch-Ch-Ch-Ch-Changes: Taking Your MongoDB Stitch Applic...
Ad

More from MongoDB (20)

PDF
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
PDF
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
PDF
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
PDF
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
PDF
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
PDF
MongoDB SoCal 2020: MongoDB Atlas Jump Start
PDF
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
PDF
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
PDF
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
PDF
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
PDF
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
PDF
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
PDF
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
PDF
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
PDF
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
PDF
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Approach and Philosophy of On baking technology
PDF
Empathic Computing: Creating Shared Understanding
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Programs and apps: productivity, graphics, security and other tools
PPT
Teaching material agriculture food technology
PDF
cuic standard and advanced reporting.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
sap open course for s4hana steps from ECC to s4
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Review of recent advances in non-invasive hemoglobin estimation
Approach and Philosophy of On baking technology
Empathic Computing: Creating Shared Understanding
NewMind AI Weekly Chronicles - August'25 Week I
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Programs and apps: productivity, graphics, security and other tools
Teaching material agriculture food technology
cuic standard and advanced reporting.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
sap open course for s4hana steps from ECC to s4
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Chapter 3 Spatial Domain Image Processing.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

[MongoDB.local Bengaluru 2018] Using Change Streams to Keep Up With Your Data

  • 1. #MDBlocal Using Change Streams to Keep Up With Your Data 24th April, 2018 | Bengaluru
  • 3. #MDBlocal 1. Change Stream Characteristics 2. Operation Types 3. Development Tips 4. Underlying Technology AGENDA
  • 6. #MDBlocal INTRODUCING: CHANGE STREAMS Allows you to watch all the changes against a given collection. CHANGESTREAMSAPI Event Notifications ● Application ● MongoDB Stitch ● Message Queue
  • 7. #MDBlocal CHANGE STREAMS: USE CASES ● Refreshing trading apps as stock prices change ● Syncing changes across microservices ● Updating dashboards, analytics systems, search engines ● IOT data pipelines - generating alarms in response to connected asset failures ● Push new credit card transaction into Machine Learning models to recalculate risk ● Maintaining multiplayer game scoreboards.
  • 11. #MDBlocal CHANGE STREAMS UTILISE COLLECTION ACCESS CONTROLS, PRESENT A DEFINED API, AND ENABLE SCALING ACROSS PRIMARIES AND SECONDARIES.
  • 15. #MDBlocal X PRIMARY CHANGES ARE RESUMABLE SECONDARY {_id: <resumeToken>, operationType: ‘update’, ns: {db: ‘data’, coll: ‘temperature’}, documentKey: {_id: ‘room12345’}, updateDescription: {updatedFields: {temperature: 27}, removedFields: [‘humidity’]} } SECONDARY var newChangeStream = coll.watch({ resumeAfter: <cachedResumeToken> });
  • 16. #MDBlocal CHANGE STREAMS UTILISE THE POWER OF THE AGGREGATION FRAMEWORK $match $project $addFields $replaceRoot $redact var changeStream = coll.watch([ {$match: {$or: [{operationType: 'delete' }, {operationType:'replace'}]}} ]);
  • 17. #MDBlocal 1. COLLECTION ACCESS CONTROLS 2. DEFINED API 3. ENABLE SCALING 4. TOTAL ORDERING 5. DURABLE 6. RESUMABLE 7. POWER OF AGGREGATION
  • 20. #MDBlocal { _id: (resumeToken), operationType: ‘insert’, ns: {db: ‘test’, coll: ‘foo’}, documentKey: {userName: ‘anish123’, _id: ObjectId("58a4eb4a30c75625e00d2820")}, fullDocument: {_id: ObjectId("58a4eb4a30c75625e00d2820"), userName: ‘anish123’, name: ‘Anish Bhanwala’} } 5 OPERATION TYPES: INSERT
  • 21. #MDBlocal DEFINING THE documentKey { _id: (resumeToken), operationType: ‘insert’, ns: {db: ‘test’, coll: ‘foo’}, documentKey: {userName: ‘anish123’, _id: ObjectId("58a4eb4a30c75625e00d2820")}, fullDocument: {_id: ObjectId("58a4eb4a30c75625e00d2820"), userName: ‘anish123’, name: ‘Anish Bhanwala’} }
  • 22. #MDBlocal DEFINING THE documentKey The “_id” field of the document created or modified by the operation. ● Replica Set : ‘_id’ ● Sharded Cluster : ‘_id’ + ‘shard key’
  • 23. #MDBlocal {_id: (resumeToken), operationType: ‘update’, ns: {db: ‘test’, coll: ‘foo’}, documentKey: {userName: ‘neerajchopra’, _id: ObjectId("58a4eb4a30c75625e00d2820")}, updateDescription: {updatedFields: {email: ‘neeraj@cwg’}, removedFields: [‘phoneNumber’]} } 5 OPERATION TYPES: UPDATE
  • 24. #MDBlocal UPDATE changeStream = coll.watch([], {fullDocument:'updateLookup'}); {_id: (resumeToken), operationType: ‘update’, ns: {db: ‘test’, coll: ‘foo’}, documentKey: {userName: ‘neerajchopra’, _id: ObjectId("58a4eb4a30c75625e00d2820")}, updateDescription: {updatedFields: {email: ‘neeraj@cwg’}, removedFields: [‘phoneNumber’]} }
  • 25. #MDBlocal UPDATE fullDocument var changeStream = coll.watch( {fullDocument:'updateLookup'} ); {_id: (resumeToken), operationType: ‘update’, ns: {db: ‘test’, coll: ‘foo’}, documentKey: {userName: ‘neerajchopra’, _id: ObjectId("58a4eb4a30c75625e00d2820")}, updateDescription: {updatedFields: {email: ‘neeraj@cwg’}, removedFields: [‘phoneNumber’]}, fullDocument: { _id: ObjectId("58a4eb4a30c75625e00d2820"), name: ‘Neeraj Chopra’, userName: ‘neerajchopra’, email: ‘neeraj@cwg’, team: ‘replication’} }
  • 26. #MDBlocal {_id: (resumeToken), operationType: ‘replace’, ns: {db: ‘test’, coll: ‘foo’}, documentKey: {userName: ‘manika123’, _id: ObjectId("58a4eb4a30c75625e00d2820")}, fullDocument: {_id: ObjectId("58a4eb4a30c75625e00d2820"), userName: ‘manika123’, name: ‘Manika Batra’} } 5 OPERATION TYPES: REPLACE
  • 27. #MDBlocal {_id: (resumeToken), operationType: ‘delete’, ns: {db: ‘test’, coll: ‘foo’}, documentKey: {userName: ‘alice123’, _id: ObjectId("58a4eb4a30c75625e00d2820") } } 5 OPERATION TYPES: DELETE
  • 28. #MDBlocal 5 OPERATION TYPES: INVALIDATE {_id: (resumeToken), operationType: ‘invalidate’, ns: {db: ‘test’, coll: ‘foo’} }
  • 32. #MDBlocal Action Handler var changeStream = coll.watch( [{ $match: { 'fullDocument.temperature': { $gte:27 }}], { fullDocument: 'updateLookup' }); {_id: (resumeToken), operationType: ‘insert’, ns: {db: ‘data’, coll: ‘temperature’}, documentKey: {_id: ‘LivingRoomGreen’}, fullDocument: {_id: ‘LivingRoomGreen’), temperature: 30, username: ‘ankit_kakkar’} } temperature: 30 username: ankit_kakkar
  • 33. #MDBlocal Action Handler var changeStream = coll.watch( [{ $match: { 'fullDocument.temperature': { $gte:27 }}], { fullDocument: 'updateLookup' }); {_id: (resumeToken), operationType: ‘insert’, ns: {db: ‘data’, coll: ‘temperature’}, documentKey : {_id: ‘OrangeRoomC’ fullDocument: {_id: ‘OrangeRoomC’, temperature: 29, username: ‘Ankur_raina’ } } temperature: 30 username: ankit_kakkar
  • 34. #MDBlocal Action Handler var changeStream = coll.watch( [{ $match: { 'fullDocument.temperature': { $gte:27 }}], { fullDocument: 'updateLookup' }); {_id: (resumeToken), operationType: ‘update’, ns: {db: ‘data’, coll: ‘temperature’}, documentKey: {_id: ‘LivingRoomGreen’)}, updateDescription: {updatedFields: {temperature: 25} fullDocument: {_id: ‘LivingRoomGreen’, temperature: 25, username: ‘ankit_kakkar’} } temperature: 30 username: ankit_kakkar
  • 35. #MDBlocal IF YOUR APPLICATION REQUIRES STATE ALWAYS MATCH ON UNCHANGING FIELDS
  • 36. #MDBlocal Action Handler var changeStream = coll.watch( [{ $match: { 'fullDocument.username: ‘ankit_kakkar’ }], { fullDocument: 'updateLookup' }); {_id: (resumeToken), operationType: ‘insert’, ns: {db: ‘data’, coll: ‘temperature’}, documentKey: {_id: ‘blueRoomB’}, fullDocument: {_id: ‘blueRoomB’, temperature: 32, username: ‘ankit_kakkar’} } temperature: 30 username: ankit_kakkar temperature: 32 username: ankit_kakkar
  • 37. #MDBlocal Action Handler var changeStream = coll.watch( [{ $match: { 'fullDocument.username: ‘ankit_kakkar’ }], { fullDocument: 'updateLookup' }); {_id: (resumeToken), operationType: ‘delete’, ns: {db: ‘data’, coll: ‘temperature’}, documentKey: {_id: ‘blueRoomB’} } temperature: 30 username: ankit_kakkar temperature: 32 username: ankit_kakkar
  • 38. #MDBlocal Action Handler var changeStream = coll.watch( [{ $match: { 'fullDocument.username: ‘ankit_kakkar’ }], { fullDocument: 'updateLookup' }); {_id: (resumeToken), operationType: ‘replace’, ns: {db: ‘data’, coll: ‘temperature’}, documentKey : {_id: ‘blueRoomB’}, fullDocument: {_id: ‘blueRoomB’, temperature: 29, username: ‘nishant’} } temperature: 30 username: ankit_kakkar temperature: 32 username: ankit_kakkar
  • 39. #MDBlocal IF YOUR APPLICATION NEEDS TO NOTIFY ON DELETED DATA ALWAYS HANDLE DELETES AND REPLACES APPROPRIATELY
  • 40. #MDBlocal IF YOU NEED TO SEE EVERY CHANGE (EVEN OUTDATED CHANGES) TO A DOCUMENT HAVE MATCHING FIELDS IN THE DOCUMENT KEY
  • 41. #MDBlocal Action Handler var changeStream = coll.watch( [{ $match: { ‘documentKey._id.username’: ‘ankit_kakkar’}], { fullDocument: 'updateLookup' }); {_id: (resumeToken), operationType: ‘insert’, ns: {db: ‘data’, coll: ‘temperature’}, documentKey: {_id: {roomId: ‘blueRoomB’, username: ‘ankit_kakkar’}}, fullDocument: {_id: {roomId:‘blueRoomB’, username:’ankit_kakkar’}, temperature: 32} } temperature: 30 username: ankit_kakkar temperature: 32 username: ankit_kakkar
  • 42. #MDBlocal Action Handler var changeStream = coll.watch( [{ $match: { ‘documentKey._id.username’: ‘ankit_kakkar’}], { fullDocument: 'updateLookup' }); {_id: (resumeToken), operationType: ‘delete’, ns: {db: ‘data’, coll: ‘temperature’}, documentKey: {_id: {roomId: ‘blueRoomB’, username: ‘ankit_kakkar’}} } temperature: 30 username: ankit_kakkar temperature: 32 username: ankit_kakkar
  • 43. #MDBlocal Action Handler var changeStream = coll.watch( [{ $match: { ‘documentKey._id.username’: ‘ankit_kakkar’}], { fullDocument: 'updateLookup' }); {_id: (resumeToken), operationType: ‘replace’, ns: {db: ‘data’, coll: ‘temperature’}, documentKey: {_id: {roomId:‘LivingRoomGreen’, username: ‘ankit_kakkar’}}, fullDocument: {_id: {roomId: ‘LivingRoomGreen’, username: ‘nishant’}, temperature: 26} } temperature: 30 username: ankit_kakkar
  • 44. #MDBlocal 1. If your application requires state: Match on unchanging fields. 2. If your application needs to notify on deleted data: Handle deletes and replaces appropriately. 3. If your application needs to see every change (even outdated changes): Have matching fields in the document key.
  • 47. #MDBlocal The secondaries replicate the primary’s oplog and apply the operations to their data sets such that the secondaries’ data sets reflect the primary’s data set. REPLICATION
  • 48. #MDBlocal 4 3 2 15 CAPPED COLLECTION (OPLOG)
  • 49. #MDBlocal 5 4 3 26 CAPPED COLLECTION (OPLOG)
  • 50. #MDBlocal 7 6 5 4 3 2 1 CACHED RESUME TOKEN : 3 NEWEST OLDEST DELETED RESUMABLE
  • 51. #MDBlocal 7 6 5 4 3 2 1 CACHED RESUME TOKEN : 1 NEWEST OLDEST DELETED NON-RESUMABLE
  • 52. #MDBlocal HOW TO GET STARTED ● MongoDB v3.6 (WiredTiger) ● Replica Set (Data bearing node)