Labels

Sunday, January 20, 2019

Virtual Machine vs Containers/Docker & Kubernetes

In simple words .. Just as shipping containers allow goods to be transported by ship, train, or truck regardless of the cargo inside, Software containers act as a standard unit of software deployment that can contain different code and dependencies. Containerizing software this way enables developers and IT professionals to deploy them across environments with little or no modification.

In technical words ..Containerization is an approach to software development in which an application or service, its dependencies, and its configuration (abstracted as deployment manifest files) are packaged together as a Container Image.

The Containerized Application

  • Can be Build/Deployed as a Docker/Container Image (Docker File) to the host operating system (OS).
  • Then Host/Run on top of a Docker Host/Container Host (Docker Engine) that in turn runs on the OS (Linux or Windows).




















Ref Video: 
Few more...

Here is an example to containerize Node.js application and Host on Docker.

Sample code: 

# Manually create a file as .Dockerfile
# Go to root folder of your node application (mynodejsapp)
# Add Two Line Content as below

#1 specify the node base image with your desired version node:
FROM node:10

#2 replace this with your application's default port
EXPOSE 80

# Now Go to Powershell Build and Run the Docker Image:

$ docker build -t mynodejsapp -f ./.DockerFile .   

$ docker run -it --rm --name my-running-app mynodejsapp 

How it is done:

There are already lot of Docker Images available (E.g. Node, Mongo, Ubuntu, Reddis etc). 
Now to create your own image using such available images, 
  • Create a .Dockerfile and
  • Add first line of code as FROM node:10 or FROM ubuntu:latest etc.
Then if you want to further customize the image, add further lines in same file. Like below
  •  COPY  src/  /var/www/html 
  •  WORKDIR /inetpub/wwwroot
  •  COPY ./ /inetpub/wwwroot
  •  Expose 80
Then build this as New Docker Image, using 
$ docker build -t mynodejsapp -f ./.DockerFile . 

Then run this New Docker Image as Docker Container, using Docker  (Note: A Container is a running instance of a Docker Image)
$ docker run -it --rm --name my-running-app mynodejsapp  (This command instructs the Docker Engine to spin up a New Docker Container, using Node 10 Image and map port 80 of the host machine to port 80 of this New Docker Container)

























Docker Architecture:

At a high level, in Docker Architecture, you have Docker Client, Docker Host/Engine and Docker Registry
  • Docker Client is the command line tool that we interact with. 
  • Docker Host, running Docker Daemon listens to the request coming from Docker Client and acts accordingly.
  • Docker Registry - Is a repository of Docker Images. Docker Hub, is a public Docker Registry where anyone can push/pull images.
Docker Image - Is created using Docker File (As in above diagram) and is a package that includes your application code and all it's dependencies.

Docker Containers - It's a running instance of Docker Image. Actually Docker Image becomes Docker Container when they run on Docker Host/Engine.

Publish Docker Image to Docker Hub: To do this, 
  • You need to create a Docker ID/Password from https://hub.docker.com/
  • Register this Docker Id with Docker Client running locally
    • docker login --username=YourDockerId
  • Tag Docker Image with Docker Id
    • docker tag YourDockerImageId YourDockerId/YourDockerHubAccountName
  • Finally push Docker Image to Docker Hub
    • docker push YourDockerId/YourDockerImageId 


Container Orchestration:

Containers have been around for a long time (10+ Yrs) in Linux and recently became available in Windows. Google has used Linux containers successfully for many years and built a platform in which they host many of their own service like Search and Google Maps.

When containers started becoming popular with Docker (Docker Swarm), Google also chose to open-source their Container Orchestrator and is now known as Kubernetes.

Note: In market there are multiple Container Orchestrators -
  •  Docker Swarm – Docker – 
    • Easy to setup with simple command as above in docker CLI - docker run app-name
    • But lacks auto scaling feature to required extent
  • Mesos – Apache  - Tough to setup
  • Kubernetes – Google  - 
    • This is best among. 
    • Connect between Docker and Kubernetes - Kubernetes used Docker Host to host applications in form of Docker Containers
    • Well it's not only Docker, Kubernetes uses 'Rocket' a.k.a. 'rktnetes' or 'Cri-o' too.

    • Easy to setup with simple command as above in Kubernetes CLI - kubectl run --replicas 1000 app-name
    • Setup/Scale-Up thousands of instances of same application with single command as above
    • Kubernetes can scale-up this in rolling fashion too as shown in command below in image
    • And so can rollback too, if something goes wrong.
    • Kubernetes internally manages to increase infrastructure along with scale-up of instances
    • Kubernetes allows to rollout new features in incremental mode where only few nodes will have new features and on alpha-beta testing rolls out on remaining nodes. 
    • Supported on all clouds (AWS, Google, Azure)
    • Support variety of Authentication/Authorization mechanisms























Ref Video - Link 

With Docker, building and managing Container Image  is simple. In theory, you would build your microservice and package it as a Container Image , and the same code will run in production. It gives the phrase “it works on my machine” a whole another meaning.

Container vs Virtual Machine

Thus Containers therefore have a significantly smaller footprint than virtual machine (VM) images.



VMs are a software abstraction of the underlying hardware and therefore are much heavier in terms of memory and processor consumption. So Containers are definitely lightweight compared to VMs.

































VM Operating System runs above the Hypervisor layer. Thus VM believes that it is interacting with the underlying hardware.  Compare this to a Container system, the Container manager allows each application to use the underlying Operating system of the host.

This makes a big difference as multiple applications are essentially sharing the same Operating system kernel and system libraries. As a result, containers allow applications to be deployed faster compared to Virtual Machines that have to be booted from an image.

Advantages of Container compared to Virtual Machine:

  • Small: There relative memory footprint is small compared to a VM (as they do not contain OS). VMs are bulky as it packs everything you need. 
  • Fast: They don’t need to boot the whole operating system like a VM (as they share OS, so gets deployed faster), it's just the application running in the container.
  • Portability: The image can be moved to run in the Container runtime on premises or in the cloud
  • Isolate Applications: Containers also isolate applications from each other on a shared OS. Each container can run a whole web application or a service, as shown below. In this example, Docker host is a container host, and App1, App2, Svc 1, and Svc 2 are containerized applications or services.


  • Scalability -  You can scale out quickly by creating new containers for short-term tasks. From an application point of view, instantiating an image (creating a container) is similar to instantiating a process like a service or web app. 
  • Reliability - When you run multiple instances of the same image across multiple host servers, you typically want each container (image instance) to run in a different host server or VM in different fault domains.
In short, containers offer the benefits of isolation, portability, agility, scalability, and control across the whole application lifecycle workflow. The most important benefit is the environment's isolation provided between Dev and Ops.

WHY SHOULD WE USE DOCKER?
- Docker is a tool that makes it easier to create, deploy, and run applications by using a CONTAINERIZATION APPROACH. 
- These containers are lightweight and take less time to start than traditional servers. 
- These containers also increase performance and lower cost, while offering proper resource management. 
- Another benefit to using Docker is that you no longer need to pre-allocate RAM to each container.


Hope this helps.

Arun 








Sunday, September 9, 2018

Async Await...C# 5.0

Detailing post made in 2013.. https://arun-ts.blogspot.com/search?q=async








Hope this helps..

Arun Manglick




Tuesday, December 19, 2017

Angular Explained...

Release Versions: Link
  • Angular JS: 2009
  • Angular 2: May 2016
  • Angular 4: Mar 2017 (To prevent conflict with router package version, skipped version 3)
  • Angular 5: Nov 2017
  • Angular 6: May 2018 
  • Angular 7: Oct 2018 
  • Angular 8: May 2019
  • Angular 9: Feb 2020 - Moved all applications to use the Ivy compiler and runtime
  • Angular 10: Jun 2020 
  • Angular 11: Nov 2020
  • Angular 12: May 2021
  • Angular 13: Nov 2021
Angular JS vs Angular 2:
  • In 2009, AngularJS with Javascript, In 2016, Angular 2 with TypeScript & Dart
  • In addition to tighter integration with Typescript, Angular 2 promises a better performance (5-10 times faster). Rationale 
    • Heavy Loading At Client Side:
      • Typically, 90–95 percent of the application code runs in the browser. Only when the user needs new data or must perform secured operations such as authentication; work goes to the server. 
      • Because dependency on the server is mostly removed, it increase server availability. 
      • No matter how many users access the server simultaneously, 90–95 percent of the time the app's performance is never impacted. 
      • Also, because most of the load is running on the client, the server is idle most of the time. 
      • Low demand on server resources reduces stress on the server significantly, potentially reducing server costs.
    • Seperate Layers: Application Layer and Rendering Layer
      • Rendering performance is substantially improved in Angular 2. 
      • Most importantly, the fact that the rendering module is located in a separate module allows you to run the computation-heavy code in a worker thread. 
      • Keeping the rendering engine in a separate module allows third-party vendors to replace the default DOM renderer with one that targets non browser-based platforms. 
      • This allows reusing the application code (TypeScript Code Class) across devices, with the UI renderers for mobile devices that use native components.
      • I.e. Code of TypeScript class remains the same, but the content of the @Component annotation will contain XML or another language for rendering native components. 
  • No more battling standards between html5 compliant data-ng-click or the short and sweet ng-click, they are replaced by (click) attribute with parentheses around it.
  • Angular 2 is entirely Component-based UI & Service/Controller compared to Angular v1 MVC approach. Controllers and $Scope are replaced by Components and Directives. Also added Modules, Services, Routes etc. 
  • This helps dividing applications in terms of components with desired features which helped to improve the flexibility and reusability as compared to Angular v1.0 
  • Mobile Support: Angular 2.0 has made it possible to accomplish the native applications for a mobile platform like React. 
  • SPA: Angular2 builds Single Page Applications : SPA technology is generating high interest in the software industry because of its potential for better-performing browser and smartphone applications. 
    • If you want to see a Single Page Application in action, click https://angular-university.io and start clicking on the home page on the list of latest courses, and on the top menu.
    • If you start navigating around, you will see that the page does not fully reload – only new data gets sent over the wire as the user navigates through the application – that is an example of a single page application.
    • Advantages
      • Production Deployment - A SPA is super-simple to deploy if compared to more traditional server-side rendered applications: it's really just one index.html file, with a CSS bundle and a Javascript bundle. Of course the application will need to make calls to the backend to get its data, but that is a separate server that can be built if needed with a completely different technology: like Node, Java or PHP.
      • Versioning & Rollback: All we have to do is to version our build output (that produces the CSS and JS bundles).
      • Best User Experience :
        • Avoid the constant full page reloads as in traditional web application.Thus much-improved user experience due to less full page reloads and a better overall performance because less bandwidth is needed.
        • On a SPA, after the initial page load, no more HTML gets sent over the network. Instead, only data gets requested from the server (or sent to the server).
        • So while a SPA is running, only data gets sent over the wire, which takes a lot less time and bandwidth than constantly sending HTML
    • More details about SPA is here 

SPA & Angular 2:
  • An SPA renders only one HTML page from the server, when the user starts the app. 
  • Along with that one HTML page, the server sends an application engine to the client. I.e.
    • In a SPA after application startup, the data to HTML transformation process has been moved from the server to the client – SPAs have the equivalent of a template engine running in your browser 
  • This engine controls the entire application including processing, input, output, painting, and loading of the HTML pages. 
  • Typically, 90–95 percent of the application code runs in the browser; the rest works in the server when the user needs new data or must perform secured operations such as authentication. 
  • Because dependency on the server is mostly removed, an SPA autoscales in the Angular 2 environment. 
  • No matter how many users access the server simultaneously, 90–95 percent of the time the app's performance is never impacted.

What is Angular 2?
  • Is a client-side JavaScript MVC framework to develop a dynamic single-page web application. 
  • Angular is a TypeScript-based Open-source Front-end Web Application Platform led by the Angular Team at Google.
  • Angular was originally started as a project in Google, but now it is an open source framework.

What is Advantages of Angular 2?
  • There is many more advantage of Angular 2.
  • The Angular 2 has better performance as explained above (Heavy Loading At Client Side, Separate Application & Rendering Layer)
  • The Angular 2 has more powerful template system.
  • The Angular 2 provide simpler APIs, lazy loading and easier to application debugging.
  • The Angular 2 much more testable.
  • The Angular 2 provides to nested level components.
  • The Angular 2 execute run more than two programs at the same time

Angular 2 Major Building Blocks:
  • Module 
    • Angular applications are modular. Every Angular application has at least one module— the root module, conventionally named AppModule. 
    • As the developer, it's up to you to decide how to use the modules concept. Typically, you map major functionality or a feature to a module. 
    • Let's say you have five major areas in your system. Each one will have its own module in addition to the root module, totaling six modules.
  • Component - 
    • A component controls a patch of the page, called a view.
    • A component contains a class, a template, and metadata. A template is a form of HTML that tells Angular how to render the component. A component can belong to one and only one module.
    • All of the components that are used must be made known via bootstrap. They also have to be imported on the page.
  • Services/Observable Services
    • A service provides any value, function, or feature that your application needs.
    • Components are big consumers of services. Services are big consumers of microservices.
  • Routes:
    • Routes enable navigation from one view to the next as users perform application tasks. 
    • A route is equivalent to a mechanism used to control menus and submenus.
  • Component Interaction (Using @Input & @Output)
  • Template
  • Data Binding
  • Directive
  • Dependency Injection
  • Pipes
  • Guards (To guard Route/Child Route)
  • Resolvers (To load object, before route is activated) 
  • AOT (Ahead of Time Compiler)
    • Faster Rendering
    • Smaller Angular Framework download size (typically vendor.ts)
    • Better Security (becoz of no html download which reduceds possibiltity of HTML Injection)
    • Early Template Errors detection
    • Fewer Async Calls to Server

Angular 4 - Updates & New Features:

  1. View Engine With Less Code - Reduced it’s bundled file size by 60%. - The code generated is reduced and has accelerated the application development. Here the developed code can be used for prod mode and debug.
  2. ngIf with a new else statement -
  3. Template - 'ng-template' tag will be utilized rather than the just 'template' 
  4. Http: Adding search parameters to an HTTP request has been simplified.
  5. Pipes: Angular 4 introduced a new pipe -  titlecase : It changes the first letter of each word into uppercase.
  6. More  compatible with newer versions TypeScript 2.1 and TypeScript 2.2. This helps with better type checking and also enhanced IDE features for Visual Studio Code.
  7. Router ParamMap -
    1. Before, simple object structures used to store route parameters -  These Parameters were assessed by simple standard JavaScript syntax : (parameterObject[‘parameter-name’] )
    2. But now in Angular 4, these parameters are available as a map. To use these parameters simple methods are called - (parameterMap.get(‘parameter-name’))
  8. Module-ID Removed: Used to resolve relative paths for your stylesheets and templates. Angular4 added a new SystemJS plugin (systemjs-angular-loader.js) to SystemJS configuration. This plugin dynamically converts "component-relative" paths in templateUrl and styleUrls to "absolute paths".
  9. Animation Package Seperated - Animation functions were provided as a part of @angular/core module (whether you need or not). Now animation is part of a separate package. This has eliminated the unnecessary bundles with large sized files.
 Reference: Link1, Link2 

Angular 5 - Updates & New Features:
  1. TypeScript 2.4 support  - String-based Enums, Weak-Type-Detection (Types are weak if they only own optional parameters) etc.
  2. Forms - Earlier validation is performed potentially with every keystroke - Thus worse performance. Now you can specify when validators should be executed in forms.  You can select change, which specifies the previous behavior, submit or blur.
  3. HttpClient -  Introduced with Angular 4. Now supports a parameter object for headers and parameters.
  4. Router - Extended with additional events. E.g., show progress when a route is changed. The corresponding events are ActivationStart and ActivationEnd or ChildActivationStart and ChildActivationEnd.
  5. Animations - Extended with several syntax ingredients. It is now possible to respond to numerical value changes by using :increment and :decrement in order to animate according transitions. 
Reference: Link1 

Angular 6 - Updates & New Features:
  1. Ivy – The New Rendering Engine (Third Engine so far).  Only noticeable thing is application being more nimble and responsive.
  2. Locality - Locality allows for more flexibility, and the Angular 6 updates mean that developers no longer have to tediously compile a specific library into an AOT.
  3. Tree Shaking - Tree shaking is a tool for optimizing the building process. It attempts to prevent unused lines of code from ending up in the final product. The Ivy renderer greatly improves the tree shaking process. Instead of making use of an interpreter, the new renderer will create template information directly. What this means is that bundles are much smaller and startup time should be super fast
  4. ng update -  Feature that allows for automatic updates of Angular dependencies in the package.json file. The ng update is an attempt to improve bug detection, updating the code without manual command.  (e.g. ng update @angular/core) 
  5. ng add - Provides you a seamless way to add new functionalities to an application.It works the same as npm, but it doesn’t replace it.  (e.g. ng add @angular/elements) 
  6. TypeScript 2.7 & RxJS 6 - Angular 6 now depends on TypeScript 2.7 and RxJS 6. RxJS 6 has new and simpler import paths and gets away with chainable operators in favor of pipeable operators. This makes the library as a whole more tree-shakable and will result in smaller bundles. RxJS 6 contains some breaking changes, but a new package, rxjs-compat, can be installed alongside RxJS 6 to provide a compatibility layer while upgrading your code to the new syntax.
Reference: Link1, Link2

Angular 7 - Updates & New Features:
  1. Updates regarding Application Performance
  2. Angular Material & CDK,
  3. Virtual Scrolling,
  4. Improved Accessibility of Selects, 
  5. Content Projection using Web standard for custom elements, 
  6. Dependency updates regarding Typescript 3.1, RxJS 6.3, Node 10

Angular 8 - Updates & New Features:
      1. Featuring Differential loading for all application code, 
      2. Dynamic imports for lazy routes, 
      3. Web workers, 
      4. TypeScript 3.4 support, 
      5. Angular Ivy as an opt-in preview - Includes Below
        • Generated code that is easier to read and debug at runtime
        • Faster re-build time
        • Improved payload size
        • Improved template type checking
        • Backwards compatibility
              Angular 9 - Updates & New Features:
              1. Moved all applications to use the Ivy compiler and runtime by default. 
              2. Angular has been updated to work with TypeScript 3.6 and 3.7. 
              3. In addition to hundreds of bug fixes, the Ivy compiler and runtime offers numerous advantages:
                • Smaller bundle sizes
                • Faster testing
                • Better debugging
                • Improved CSS class and style binding
                • Improved type checking
                • Improved build errors
                • Improved build times, enabling AOT on by default
                • Improved Internationalization
              Angular 10 - Updates & New Features:
              1. New Date Range Picker (Material UI library)
              2. Warnings about CommonJS imports
              3. Optional Stricter Settings
              4. Keeping Up to Date with the Ecosystem
              5. New Default Browser Configuration
              6. Deprecations and Removals
              Angular 11 - Updates & New Features:
              1. Available With Typescript 4.0, Webpack 5
              2. Updated Hot Module Replacement (HMR) Support
              3. Updates on Operation Byelog
              4. Automatic Inlining of Fonts
              5. Component Test Harnesses
              6. Updated Language Service Preview
              7. Faster Builds
              8. Experimental Webpack 5 Support
              9. Moving to ESLint.
                          Angular 12 - Updates & New Features:
                          1. Available With Typescript 4.2, Webpack 5
                          2. Ivy Everywhere: Angular v12
                          3. Migrating from legacy i18n message IDs
                          4. Protractor: planning for future
                          5. Nullish Coalescing
                          6. Improvements in styling
                          7. Deprecating support for IE11

                                    Angular CLI over WebPack:

                                    A bundler is software that bundles your application code along with its resources into a minimized, zipped bundle that can be easily deployed on the server.
                                    There are many bundlers out there, most widely used are Grunt, Gulp and lately, Webpack.

                                    While Grunt and Gulp simply bundle all js files and all assets, Webpack does extra:
                                    •  Maintains a dependency tree (by scanning import statements) and that allows it to only bundle resources and js files your code actually uses, 
                                    •  Identify chunks of code – using code splitting – and bundle chunks together for a more efficient bundle.
                                    The angular team went the extra mile and created Angular-CLI – a very powerful tool that goes way beyond the simple bundler or generator.
                                    • It has Webpack under the hood, already pre-configured, so you enjoy the benefits without the hassle of configuration.
                                    • It is very easy to use with a set of cli commands, the main ones are:
                                      • ng new – create a new angular-cli enabled project
                                      • ng init – initialize the current project for angular-cli
                                      • ng test – run all unit tests (using karma/jasmine stack)
                                      • ng e2e – run all protractor e2e tests
                                      • ng serve – will run your app in a local web server
                                      • ng build – will compile TypeScript code, bundle the dependency tree and dump it to the dist folder.
                                      • ng build --prod – will also minify, zip, hash etc.
                                    • It comes with a code generator – you can use it to create skeletons of the most common ones (Components, Directives, Services and Pipes) by simply using the cli command ng g .

                                    Reference: Link1


                                    TypeScript:
                                    • TypeScript is a form of JavaScript which knows types and classes and  can be compiled to JavaScript. 
                                    • It is open source. TypeScript includes many aspects of object orientation such as Lambdas, Iterators, Inheritance, Generics, Interfaces etc.
                                    • TypeScript is an extension of ECMAScript, in fact: TypeScript = ES6 + Types + Annotations
                                    • TypeScript is a superset of ECMAScript 6 (ES6), and is backwards compatible with ECMAScript 5 (i.e.: JavaScript)
                                    • In previous versions of ECMAScript, everything was still defined by a JS prototype. Now classes are defined and it makes it almost as readable as Java code. 
                                    • TypeScript is actually from Microsoft, which means the new Angular is also likely to be popular for .NET developers. 
                                    • TypeScript Concepts
                                      • Variables
                                      • Let vs Var
                                      • Const
                                      • Functions
                                      • Overloading
                                      • Arrays
                                      • Destructuring
                                      • Type Guards
                                      • Type Assertions
                                      • Tuple
                                      • Interfaces
                                      • Classes
                                      • Static vs Read-only
                                      • Generics
                                      • Inheritance
                                      • Namespace
                                      • Module
                                    Angular Learning:



























                                    •  ngOnChanges: Called after a bound input property changes
                                    •  ngOnInit: Called once the component is initialized (After Constructor)
                                    •  ngDoCheck: Called during every change detection run
                                      •  ngAfterContentInit: Called after content (ng-content) has been projected into view
                                      •  ngAfterContentChecked: Called everytime the projected content has been checked
                                      •  ngAfterViewInit: Called after the component's view (& child views) has been initialized
                                      •  ngAfterViewChecked: Called everytime the component's view (& child views) has been checked
                                    •  ngOnDestroy: Called once the component is about to be destroyed

                                    Ref:
                                    • https://magenic.com/thinking/angular-2-vs-angularjs-1-which-to-choose
                                    • https://www.angularminds.com/blog/article/comparison-difference-between-angular1-vs-angular2-vs-angular4.html
                                    • https://dzone.com/articles/typed-front-end-with-angular-2
                                    • https://www.code-sample.com/2016/06/angular-2-interview-questions-and.html
                                    • With Code:
                                      • https://www.infoq.com/articles/Angular2-TypeScript-High-Level-Overview
                                      • https://www.ibm.com/developerworks/library/wa-implement-a-single-page-application-with-angular2/index.html

                                    Hope this helps!!

                                    Arun Manglick

                                    Saturday, December 2, 2017

                                    Node.JS & EventLoop Explained..

                                    Overview:
                                    • Node.js is a Server-Side platform developed by Ryan Dahl in 2009 for easily building fast, scalable network applications. 
                                    • Node.js is a JavaScript Runtime or platform which is built on Google Chrome’s JavaScript v8 engine. This runtime allows executing the JavaScript code on any machine outside a browser (this means that it is the server that executes the Javascript and not the browser).
                                    • Node.js = Runtime Environment + JavaScript Library
                                    • Nodde.js built on Chrome’s JavaScript Engine (V8)  and designed with a Non-blocking, Event-driven I/O. 
                                      • Note: Its Non-blocking I/O is due to its use of asynchronous functionality, which keeps the server away from waiting on data to be returned before being able to move on to other tasks.
                                      • Note: Below are JavaScript Engines for different browsers that run the JavaScript of web pages.
                                        • Chrome : V8.
                                        • Firefox : Spidermonkey
                                        • IE: Chakra
                                        • Safari : JavaScriptCore 
                                    • Node.js is also cross-platform, able to be run on Windows, Linux and OS X. 
                                    • Node.js uses an Event-Driven, Non-Blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
                                    • Node.JS packaging eco-system is the largest eco-system of open libraries in the world.
                                    • Node.js is an open source, cross-platform runtime environment for developing server-side and networking applications built on Google Chrome's JavaScript Engine (V8 Engine).

                                    Why Node.JS (Reasons to use NodeJS)
                                    • Speed: Node.js is a JavaScript Runtime that uses the V8 Engine developed by Google for use in Chrome. V8 compiles and executes JavaScript at lightning speeds mainly due to the fact that V8 compiles & translates JavaScript code into more efficient machine code instead of  bytecode or any intermediate code.
                                    • Larget Eco-System: npm is the Node.js package manager having collection of more than 3.5 lacs of packages and it is excellent.
                                      • Note : Node Package Manager (NPM) makes it easy to install packages that others have developed to perform any number of tasks. This helps to encourage code-sharing among developers and to reduce the amount of custom development that has to be done when one or more packages can take care of that coding for you.
                                    • One Language: It runs Javascript, so you can use the same language on server and client, and even share some code between them (e.g. for form validation, or to render views at either end.)
                                    • Single Threaded & Event Driven : The single-threaded event-driven system is fast even when handling lots of requests at once, and also simple, compared to traditional multi-threaded Java or ROR frameworks.
                                    • Real-time, Made Easy with Websockets: Node.js is a great platform for creating real-time apps. With a module such as Sockiet.io, you can create real-time chats, games and more. Socket.io is one of the most popular websocket libraries in use, and makes collaborative web applications dead simple.
                                      • Note:  Websockets are simply two-way communications channels between the client and server. So the server can push data to the client just as easily as the client can. 
                                    UseCases:
                                    • APIs based Applications
                                    • I/O bound Applications
                                    • Streaming Data
                                    • A Real-time Applications like online games, collaboration tools and chat rooms etc.
                                    • Single Page Applications
                                    • JSON based Applications
                                    • PROXY Applications


                                    Node.JS Drawbacks:
                                    • Callback Hell: In Node.JS everything is asynchronous by default. This means you are likely to end up using tons of nested callbacks, which can cause multiple issues (like Debugging, Error Handling, Error Stacks etc.). Nevertheless, there are a number of solutions to this problem, e.g. async/await. Well this problem is specific to JavaScript, not Node.JS.
                                    • Event Loop: While the event loop is one of its' largest advantages, not understanding how this works is one of its' biggest disadvantages as well.  Do *NOT* do long running, synchronous, cpu bound logic in Node.js.  Instead spin it off to a worker queue, another thread/process (use a pool), or any number of other ways to remove the heavy lifting from your main event loop.
                                    Few Testimonials:
                                    • Paypal: Node.js and an all Javascript development stack helped PayPal bring efficiencies in engineering and helped rethink and reboot product, design and operational thinking.
                                    • Uber: Uber used Node.js from the beginning, and its distributed system was a perfect fit, since a lot of network requests are made. The non-blocking asynchronous I/O used by Node.js helped to ensure those requests could be made and handled quickly to provide the best service possible.
                                    jQuery vs Angular vs Node.Js:
                                    • jQuery (Client Side): Is a Library: Is a fast, small, lightweight, "write less, do more", and feature-rich JavaScript library - HTML document traversal and manipulation, event handling, animation et
                                    • Angular: (Client Side) : Is a client-side JavaScript MVC framework to develop a dynamic single-page web application. Angular was originally started as a project in Google, but now it is an open source framework.
                                    • Node.JS (Server-Side): Is an open source, cross-platform runtime environment for developing server-side and networking applications built on Google Chrome's JavaScript Engine (V8 Engine).

                                    Node JS Event Loop:




















                                    Node.JS - CallStack, CallBack & Event-Loop:

                                    Consider below code-snippet and see Node.js Event-Driven, Non-Blocking Model works:

                                    console.log('Starting App')
                                    setTimeout(() => {
                                      console.log('First Callback 2000ms');
                                    }, 2000);
                                    setTimeout(() => {
                                      console.log('Second Callback 0ms');
                                    }, 0);
                                    console.log('Finisihng App')

                                    Step1:

                                    1. First thing happens here is: main() statement is loaded in CallStack and kick-offs the execution.
                                    2. Next the very first statement is loaded in CallStack and removed after execution.
                                    3. Next statement loads in CallStack. But as this is a Node API and not available in V8 engine, moves/registers to Node API section and finally gets removed from CallStack. Here in Node APIs section API starts it's execution in parallel to CallStack.
                                    4. Next statement (with zero sec) adds in CallStack. But as this is a Node API and not available in V8 engine, moves/registers to Node API section and finally gets removed from CallStack. Here in Node APIs section API starts it's execution in parallel to CallStack.
                                    5. After this assume, API with zero sec is completed. Thus it moves to CallBack queue, marking itself ready for execution. Ideally CallBack queue is the place where the operation resides and wait for execution immediately after CallStack complete it's all operations and becomes empty. This is all taken care by 'Event Loop', where it does not push any operation waiting in CallBack queue, unless CallBack becomes empty.
                                    6. Thus next statement comes to execute is 'Finishing App' in CallStack. Once it's completed main() statement moves out of CallStack.
                                    7. Now here 'Event Loop' finds empty CallStack, it moves first waiting operation in CallBack Queue to CallStack. This CallBack executes Console.Log ('Second CallBack') and then removes from CallStack.
                                    8. We still have one more operation running in Node API, which moves to CallBack queue after completion. Event Loop then moves this operation in Call Stack as in Step 7.
                                    9. Once all the operations are complete and every section - CallStack, Node API and CallBack Queue is empty, whole operation is marked complete.
                                    Step 1


                                    Step 2

                                    Step 3 (a)

                                    Step 3 (b)

                                    Step 3(c)

                                    Step 4(a)

                                    Step 4(b)

                                    Step 4(c)

                                    Step 5

                                    Step 6(a)

                                    Step 6(b)

                                    Step 6(c)

                                    Step 7(a)

                                    Step 7(b)

                                    Step 7(c)
                                    Step 8(a)

                                    Step 8(b)

                                    Step 8(c)



                                    Hope this helps!!

                                    Arun Manglick

                                    Saturday, November 25, 2017

                                    Cache Coherence..

                                    Overview

                                    In a shared memory multiprocessor system with a separate cache memory for each processor(CPU), it is possible to have many copies of shared data: One copy in the main memory and One in the local cache of each processor that requested it. When one of the copies of data is changed, the other copies must reflect that change, otherwise system will have further have data integrity issues/problems. This is resolved by a concept called as 'Cache Coherence'.

                                    Cache Coherence is the discipline which ensures that the changes in the values of shared operands(data) are propagated throughout the system in a timely fashion.

                                    Share Memory:  In computer hardware, Shared Memory refers to a (typically large) block of random access memory (RAM) that can be accessed by several different central processing units (CPUs) in a multiprocessor computer system. Below is an illustration of a shared memory system of three processors.



                                    Multi-Processing: Multiprocessing is the use of two or more central processing units (CPUs) within a single computer system.

                                    More Details:

                                    Consider two clients have a cached copy of a particular memory block from a previous read. Suppose the client on the above updates/changes that memory block, the client on the bottom could be left with an invalid cache of memory without any notification of the change. In such cases, Cache coherence is intended to manage such conflicts by maintaining a coherent view of the data values in multiple caches.



                                    More Technical

                                    We discussed, when multiple processors with on-chip caches are placed on a common bus sharing a common memory, then it's necessary to ensure that the caches are kept in a coherent state.

                                    Let's understand more technically the problem and solution using Coherence Cache.
                                    Here assume main memory has value '200' stored in it's location 'x'.

                                    Step 1: 
                                    • Processor_A reads location x. Copy of x transferred to PA's cache.
                                    • Processor_B also reads location x. Copy of x transferred to PB's cache too.





















                                    Step 2:
                                    • PA adds 1 to x. x is in PA's cache, so there's a cache hit.
                                    • If PB reads x again (perhaps after synchronizing with PA), it will also see a cache hit. However it will read a stale value of x.
























                                    Problem Resolution:

                                    This problem is avoided by adding 'Cache Coherence' hardware to the system interface. This hardware monitors the bus for transactions which affect locations cached in this processor.
                                    Here cache needs to generate 'Invalidate Transactions' when it writes to shared locations.

                                    I.e.
                                    • When PA updates x, the PA cache generates an Invalidate Transaction.(I.e. Simply communicating all the processors, the address of a cache line which has been invalidated) .
                                    • When PB's hardware sees the invalidate x transaction, it finds a copy of x in its cache and marks it 'Invalid'.
                                    • Now a read x by PB will cause a cache miss and initiate a databus transaction to read x from main memory.






                                    Invalidate Transaction:  Is an address-only transaction: it simply communicates the address of a cache line which has been invalidated to all the other processors.













                                    Further..
                                    • When PA's hardware sees the memory read for x (by PB), it detects the modified copy in its own cache, and emits a retry response, causing PB to Suspend the read transaction.
                                    • PA now writes (Flushes) the modified cache line to main memory.
                                    • PB later continues its suspended transaction and reads the correct value from main memory.
























                                    Hope this helps!!

                                    Arun Manglick