Tuesday, June 14, 2016

MongoDB Design Patterns


{MongoDB}, its a NoSQL - Document database. Its ideal for most use cases. Its not ideal for a few, but you can still overcome some of the limitations in MongoDB using the following design patterns.

This article provides a solution for some of the limitations mentioned in my other article MongoDB : The Good, The Bad and the Ugly.

1. Query Command Segregation Pattern

Segregate responsibility to different nodes in the replica set.
Primary node may have priority 1 and may keep only indexes required for insert and update. The queries can be executed on secondaries.
This pattern will increase write throughput on the “priority 1” servers since fewer indexes need to
be updated and inserted on writing to a collection and secondaries benefit from having to update
fewer indexes and having a working set of memory that is optimized for their workload


2. Application level transactions Pattern

MongoDB does not support transactions and locking of documents internally. However, with
application logic a queue may be maintained.

db.queue.insert( { _id : 123,
message : { },
locked : false,
tlocked : ISODate(),
try : 0 });
var timerange = date.Now() - TIMECONSTANT;
var doc = db.queue.findAndModify( { $or : [ { locked : false }, { locked : true, tlocked : {
$lt : timerange } } ], { $set : { locked : true, tlocked : date.Now(), $inc : { try : 1 } } }
//do some processing
db.queue.update( { _id : 123, try : doc.try }, { } );

3. Bucketing Pattern

When the document has an array which grows over the period of time, use bucketing pattern.
Example: Orders. The order lines can grow or may be larger than the desired size of the document.
The pattern is handled programmatically and is triggered using a tolerance count.

     var TOLERANCE = 100;
    for( recipient in msg.to) {
db.inbox.update( {
owner: msg.to[recipient], count: { $lt : TOLERANCE }, time : { $lt : Date.now() } },
{ $setOnInsert : { owner: msg.to[recipient], time : Date.now() },
{ $push: { "messages": msg }, $inc : { count : 1 } },
{ upsert: true } );
}

4. Relationship Pattern 

Sometimes its not feasible to embed entire document. Example when we are modeling people. Use this pattern to build relationships.

1. Determine if data “belongs to” a document - is there a relation?
2. Embed when possible, especially if the data is useful and exclusive (“belongs in”).
3. Always reference using _id values at minimum.
4. Denormalize the useful parts of the relationship. Good candidates do not change value often or ever and are useful.
5. Be mindful of updates to denormalized data and repair relationships
{
_id : 1,
name : ‘Sam Smith’,
bio : ‘Sam Smith is a nice guy’,
best_friend : { id : 2, name : ‘Mary Reynolds’ },
hobbies : [ { id : 100, n :’Computers’ }, { id : 101, n : ‘Music’ } ]
}
{
_id : 2,
name : ‘Mary Reynolds’
bio : ‘Mary has composed documents in MongoDB’,
best_friend : { id : 1, name : ‘Sam Smith’ },
hobbies : [ { id : 101, n : ‘Music’ } ]
}

5. Materialized Path Pattern


If you have a tree pattern of data model where the same object type is a child of an object, you can use the materialized path pattern for more efficient search/queries. Sample is given below.
{ _id: "Books", path: null }
{ _id: "Programming", path: ",Books," }
{ _id: "Databases", path: ",Books,Programming," }
{ _id: "Languages", path: ",Books,Programming," }
{ _id: "MongoDB", path: ",Books,Programming,Databases," }
{ _id: "dbm", path: ",Books,Programming,Databases," } 
Query to retrieve the whole tree, sorting by the field path:
db.collection.find().sort( { path: 1 } )
Use regular expressions on the path field to find the descendants of Programming:
db.collection.find( { path: /,Programming,/ } )
Retrieve the descendants of Books where the Books is the top parent:
           db.collection.find( { path: /^,Books,/ } ) 


No comments:

Post a Comment