Friday, December 21, 2018

JTD-DesignerSeries-17-MongoDB-101


A brief Context
Relational Databases with its three decades of legacy, have provided robust monolith applications. MongoDB on the other hand, has created no-sql DB engine that allow you to work with your data anyway you want, scale in & scale out with your workloads and run it anywhere by deploying on a single node or on different clouds. MongoDB is a document based DB engine that provides drivers in languages like Python, Javascript, Ruby, Java and allows you to quickly build modern applications.

Architecture

mongod is a daemon, a core runtime process of MongoDB designed with architectural layers of MQL, Document Data Model and Storage Layer, that accepts connection requests and persist data to the hard drives.

MongoDB Query Language (MQL) - Data & operations send by client drivers in the form of BSON messages are translated to mongoDB instructions by this layer. It provides different operators & aggregation engine to support CRUD activities.

MongoDB Document Data Model - This layer is responsible for applying the CRUD operations in a distributed replicas & its data structures by handling requirements related to replication, durability.

Storage Layer - This layer handles the disk level system calls on a single node while also providing things like compression, encryption. Wired Tiger is a default storage engine for MongoDB.

Security & Admin are traversal layers for user management & database admin activities.

MongoDB is a distributed DBMS and supports high availability & automatic failover with a replication mechanism between primary & secondary nodes in a replica set. MongoDB manages scalability needs with mongos managing shards of replica sets.


Documents & Data Structures

Data in mongoDB is stored in a hierarchical structure with database at the top level with one of more collections. Then there are documents in the collections and multiple documents represent your data set. MongoDB stores JSON documents as binary representation of JSON (BSON). Drivers for different languages returns documents in JSON format and responsible for translation from BSON to JSON format.

CRUD Operations
a) Create new documents in a collection.
db.users.insertOne({name:"sue", age: 26, status: "pending"})

b) Retrieve documents in a collection with query filters.
db.users.find({age: {$gt: 18}})

c) Modify existing documents in a collection.
db.users.updateMany({age: {$lt: 18}}, {$set: { status: "reject"}})

d) Remove documents from a collection.
db.users.deleteMany({status: "reject"})


Aggregation Framework
Modeled on the concept of data processing pipelines, pipelines are composition of several stages that operate on documents and filter & transforms them into aggregated results.

Syntax:
db.userColl.aggregate([{stage1}, {stage2}, {stage3}], {options})


Replication
Replication provides redundancy and increases data availability.With multiple copies of data on a different server, replication provides level of fault tolerance against the loss of single database server. A replica set in MongoDB is a group of mongod processes that maintain the same data set. MongoDB uses statement based replication mechanism, a process by which DB operations are translated to idempotent op log entries in the primary node which are then used by secondary for replicating data.

Sharding
Database growth can be supported by either vertical scaling or horizontal scaling. Vertical scaling resorts to approach of increasing CPU on a single node, whereas horizontal scaling is an approach of splitting data across multiple nodes.
Sharding is a method of dividing data across multiple replicas based on a shard key. MongoDB implements sharding by routing queries through the mongos and storing metadata and configuration settings in Config Servers.
It is quite important to pick a good shard key based on the cardinality [High], frequency [Low], monotonic change [Avoid]. In some cases, it may be beneficial to use the hashed shard key but then you loose the ability to target a range query and mongos will always perform scatter gather query.

Storage
Storage Engine is a component that manages how data is stored to the disk. Wired Tiger is the default storage engine for MongoDB but there are other options like In-Memory, MMAPv1. WiredTiger supports atomicity at a document level during the write operations and creates checkpoint after writing snapshot data to the disk. MongoDB can recover from a last checkpoint in case of failure writing the new checkpoint and can refer to journal to replay all the data between the checkpoints. With WiredTiger MongoDB supports compression for all collections and indexes.

Monday, December 10, 2018

JTD-DesignerSeries-16-JavaScript-101


A Brief on Web applications
Let's simply say that anything that runs from a browser with a URL is a web application, and complexity can vary based on the content that the browser presents to the user. HTML / CSS helps creative teams design beautiful static content whereas Javascript helps UI programmers manipulate the DOM to create dynamic content & behavior. JS Engine is a runtime that interprets the Javascript code and can be a part of browser as V8 in chrome or other runtime server environment like NodeJS

Basic Notion
a) JS Engine creates a global execution context, along with a global object referenced by this variable, which in case of browser happens to be a window object. At a global execution context, reference to Outer Environment is null.
b) Execution Context has a creation phase before the execution phase, and hoisting, a part of creation phase, allocates memory space for the variables & functions, so that they are available in the execution phase irrespective of where they are written lexically in an execution context.
c) Each invocation of a function creates an execution context, and execution stack controls the layering of execution context with global execution context as the base of the stack.
d) Each execution context has its own variable environment, and can uniquely assigned a value irrespective of the same variable name.Though, when variable is not declared in the function context, it goes down the scope chain referenced by outer environment of execution context. Scope chain depends upon the lexical environment, which means where the function physically written in the JS file.

Objects
a) Objects are collections of name/value pairs and names can again be collection of name/value pairs. Objects have properties [primitive types, objects] & methods [function] and you can access the them with the dot operator.
b) Object Literals is a preferred way of creating objects. [var person = { firstname: John, lastname: Doe };]
c) Javascript Object Notiation (JSON), a ubiquitous data exchange standard over the internet for webapps can be easily converted to object literal & vice-versa in Javascript.
var jsonString = JSON.stringify(jsObjectLiteral);
var jsObjectLiteral = JSON.parse(jsonString);
d) Objects are being passed as reference & all primitives types are passed by value. When you mutate an object by adding a property, all references to that object will able to access the new property.


Functions
a) In Javascript, functions are a special type of objects, with optional property: 'name' & invocable property: 'code as the function body.
b) Functions are first class in Javascript, which means they can be assigned to variables, created on the fly, passed as parameters, just like other Javascript types.
c) Usually 'this' variable references the global object inside a function but when function is defined as a property method of an object, 'this' variable references the object that contains the method definition rather than global object. A common pattern is that you can mutate the object in function by using the this variable and best practice is to assign this to a self variable.
d) Immediately Invoked Function Expression (IIFE) is an anonymous function expression wrapped in parentheses, and created & invoked on the fly as first class function.
(function(name) {var greeting = "Inside IIFE: Hello "; console.log(greeting + ' ' + name); }('John'));

Closures
As functions can be passed around & can be returned from another function in Javascript, Closures, a feature of JS language, allows the returned function to close in the variables during its execution. For eg:

function parentClosure(varClosure) {
return function () {
console.log("Closing in on " + varClosure + " created in parent function and claimed in child");
}
}

var childClosure = parentClosure('patang');
childClosure();

When the anonymous function returned by the parentClosure('patang') function, patang / kite was created in parentClosure function variable environment and even though parentClosure function was done executing, JS engine provided patang assigned to varClosure variable to the childClosure function when it was invoked.

This phenomenon of keeping the scope chain intact until execution context closed in all of the variables is termed as closures.

Callback
A process of passing first-class function as a parameter to function and invocation of a parameter function after the complete execution of a function is called a callback. For eg: Timeout function will output "Hello Callback" after 3 sec by invoking an anonymous function passed as its parameter.
setTimeout(function() {console.log("Hello Callback")}, 3000);

Prototypal Inheritance
Inheritance, in object oriented languages like Java, is an approach with derived class gets access to properties and methods of the base class. Every object in Javascript has a proto object and automatically gets access to the properties & method attached to its proto object. It also goes down in the prototype chain if the property or method is attached to its immediate proto object.