My Sites


Tuesday, November 29, 2016

SSH key-based authentication related errors and fixes

Could not open a connection to your authentication agent.
eval `ssh-agent -s`
ssh-add


Add private key permanently with ssh-add on Ubuntu.
ssh-add keyname (This is the temporaray fix)
Permanant Fix


Add keys in ~/.ssh/config file:
  IdentityFile ~/.ssh/gitHubKey
  IdentityFile ~/.ssh/id_rsa_buhlServer 


Permissions 0777 for '/Users/username/.ssh/id_rsa' are too open. It is recommended that your private key files are NOT accessible by others. This private key will be ignored.

chmod 400 ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa


How To Configure SSH Key-Based Authentication on a Linux Server
https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server

Generate a SSH key pair by typing: ssh-keygen
Copy the contents of your ~/.ssh/id_rsa.pub key into a file in the remote account's home ~/.ssh directory called
authorized_keys.

Default ssh key is id_rsa


RSA authentication with openssh
http://cects.com/openssh-rsa-authentication-for-windows-and-linux/

Saturday, October 1, 2016

Developer Interview Essentials

Java Collections
http://javahungry.blogspot.com/2015/05/50-java-collections-interview-questions-and-answers.html

Data Structures and algorithms
http://javahungry.blogspot.com/p/data-structures-in-java.html

Big O notation cheatsheet
http://bigocheatsheet.com/

Linear and Binary Search (Best Case : Average case:   Worst Case: O(n))





























Bubble Sort  (Best Case : Average case:   Worst Case: )











Selection Sort  (Best Case : Average case:   Worst Case: O(n))


















Insertion Sort  (Best Case : Average case:   Worst Case: O(n))












Merge sort  (Best Case : Average case:   Worst Case: O(n))











Quick Sort  (Best Case : Average case:   Worst Case: O(n))



















Dijkstra's algorithm - Finding the shortest paths between nodes in a graph.






Binary search tree - http://stackoverflow.com/questions/2130416/what-are-the-applications-of-binary-trees





















Tree Rotation
















Depth-first search (DFS) -  algorithm for traversing or searching tree or graph data structures

Breadth-first search (BFS) -  algorithm for traversing or searching tree or graph data structures
http://stackoverflow.com/questions/3332947/when-is-it-practical-to-use-dfs-vs-bfs

Stack and Queue
https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm
https://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm


References : Extracted from Internet.



Monday, September 19, 2016

Nodejs Developer - Production Best Practises : Demystifying how clustering works ( CPU Threads vs Cores vs Sockets vs Processes)

First thing first. Every application needs to be optimized and perform fast. When it's come to Nodejs, you will come across many best practices to optimize your Nodejs application with tremendous performance and reliability; especially in the production environment.
Most of the Nodejs applications use Express as the Nodejs web framework. And there, they have specifically mentioned a lot of production best practices covering performance aspects and reliability aspects. Basically, the things to do in your code and environment/setup which will ultimately improve your application performance. Below article will guide through those best practices.
https://expressjs.com/en/advanced/best-practice-performance.html

But what I want to emphasize in my article is one fact among that list; which is 'Run your app in a cluster'. Basically, a single instance of Node.js runs in a single thread. To take advantage of multi-core systems  you will sometimes want to launch a cluster of Node.js processes to handle the load. Following is the note provided by them.
'Clustering is made possible with Node’s cluster module. This enables a master process to spawn worker processes and distribute incoming connections among the workers. However, rather than using this module directly, it’s far better to use one of the many tools out there that does it for you automatically; for example node-pm or cluster-service.'

Anyway, here what I want to talk about is the Node's cluster module. How it works with the CPU. Of course, you can copy paste the code given in the documentation and it will surely work as a charm. But what is actually happening inside the CPU with regards to this. Let's have a look in depth.

First We'll look how CPU (Central processing unit) works. Here mainly we talk about # of threads and # of cores. In following snapshot, in the left hand side it shows my local machine CPU information (Intel(R) Core(TM) i7-4600M CPU @ 2.90GHz) and in the right side it shows the CPU information of a AWS C1.xlarge server (Intel(R) Xeon(R) CPU E5-2651 v2 @ 1.80GHz).

CPU information can be displayed by following Linux commands.
cat /proc/cpuinfo
lscpu


Here CPU(s) means # of threads eventually logical cores. So in this case, my cluster enabled Node.js application will run in 4 processes in my local machine and will run 8 processes in the AWS server.

And here we can find Core(s) per socket and No. of sockets. In my local machine, the total No. of cores are 2 which is Dual core (Actual hardware). And in AWS server, there is only 1 core (Actual hardware). And from Thread(s) per core will give you the idea of about the relationship between threads and cores.

A core is the physical hardware that works on the thread. In general a processor can only work on one thread per core, CPUs with hyper-threading enabled can work on up to two threads per core. In this case, my local machine is hyper-threading enabled.







This i7 CPU has only 2 cores, but can handle 2x2=4 threads , and hence appears as a quad core to the OS.(4 logical cores).

Hope this will give you an understanding about, how CPU behaves and how clustering relates with the CPU. Cheers!!! :)

Friday, July 15, 2016

Mongo backup and authentication

mongodump -d myDatabase -o ~/backups/first_backup
$mongod --auth
db.createUser({user:"admin_name", pwd:"1234",roles:["readWrite","dbAdmin"]})

If you want to add without roles (optional):
db.createUser({user:"admin_name", pwd:"1234", roles:[]}) to check if authenticated or not:
db.auth("admin_name", "1234") it should give you:
1 else :
Error: Authentication failed. 0



mongo restore

mongorestore dump-2013-10-25/
https://docs.mongodb.com/manual/tutorial/backup-and-restore-tools/

install mongo
https://docs.mongodb.com/v3.0/tutorial/install-mongodb-on-amazon/


Everything you need to know about nvm

Install/ Update NVM
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.2/install.sh | bash
or
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.2/install.sh | bash

source ~/.bashrc
nvm ls-remote command results in “N/A”
export NVM_NODEJS_ORG_MIRROR=http://nodejs.org/dist

nvm ls-remote
nvm install 5.0
nvm use 5.0
nvm run 5.0 --version
nvm which 5.0

Saturday, July 2, 2016

Node.js Clustering - 4 Core = 4 Processes

var cluster = require('cluster');

if (cluster.isMaster) {
  // Count the machine's CPUs
  var cpuCount = require('os').cpus().length;

  // Create a worker for each CPU
  for (var i = 0; i < cpuCount; i += 1) {
    cluster.fork();
  }

  // Listen for dying workers
  cluster.on('exit', function () {
    cluster.fork();
  });
} else {
  require('./server');
}

Nginx - Proxy to Express Application, Load Balancer, Static Cache Files

sudo apt-get update
sudo apt-get install nginx
sudo service nginx start 
sudo service nginx stop
nano /etc/nginx/site_enabled/default

upstream project {
server 33.22.22.5:3000;
server 33.22.22.6:3000;
server 33.22.22.7:3000;
}
server {
listen 80;
location / {
proxy_pass http://project;
}
location ~* \.(css|js|gif|jpe?g|png)$ {
expires 168h;
}
location /api {
expires 10m;
}
}

How add an automatic load to the server (Performance wise check)
Bench test for 1000 requests
ab -c 40 -n 1000 http://33.22.22.5/

Thursday, May 26, 2016

Blocked loading mixed active content

It means you're calling http from https

What is Mixed Content?
When a user visits a page served over HTTP, their connection is open for eavesdropping and man-in-the-middle (MITM) attacks. When a user visits a page served over HTTPS, their connection with the web server is authenticated and encrypted with SSL and hence safeguarded from eavesdroppers and MITM attacks.

However, if an HTTPS page includes HTTP content, the HTTP portion can be read or modified by attackers, even though the main page is served over HTTPS. When an HTTPS page has HTTP content, we call that content “mixed”. The webpage that the user is visiting is only partially encrypted, since some of the content is retrieved unencrypted over HTTP. The Mixed Content Blocker blocks certain HTTP requests on HTTPS pages.

You can disable mixed content blocking completely.

Tuesday, April 19, 2016

Set Java 8 Environment in Linux Ubuntu (Quick Essentials)

sudo gedit /etc/profile

JAVA_HOME=/usr/local/java/jdk1.8.0_77
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
JRE_HOME=/usr/local/java/jdk1.8.0_77/jre
PATH=$PATH:$HOME/bin:$JRE_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH

. /etc/profile


sudo nano .bashrc

export JAVA_HOME="/usr/local/java/jdk1.8.0_77/"
export PATH="$JAVA_HOME/bin:$PATH"

source ~/.bashrc

java --version

Wednesday, April 13, 2016

Technology Secrets behind Netflix


Simply for me, Netflix is awesome !! It's not all about the content that has on it, but the amazing technology that Netflix uses behind the curtain. It can be considered as the main leading global content streaming platform. So in North America, Netflix is the 3rd of the consumer internet traffic in peak times. And it has a huge library of content more than a petabyte. (1000 terabytes). Netflix expanded into 130 countries, now reaching just about every nation on Earth. So how are they handling such a huge traffic in a way that the consumers has no disappointment in loading content to their TVs, smartphones , gaming consoles and etc. Let's find out what is there behind the screen. :)

CDN - They have their own CDN which they call it Open Connect. Netflix has this partnership with local Internet Service Providers (ISPs) in order to deliver content efficiently  to their customers. Basically, it is localizing the internet traffic. So during quiet periods between midnight and lunchtime it prepopulates the servers with the content it thinks people will want to watch, reducing bandwidth use in peak hours.
They use FreeBSD as the open source OS, Nginx as the web server which will deliver the content via HTTP to the client application, and also Bird internet routing daemon to enable the transfer of network topology from ISP networks to the Netflix control system that directs clients to sources of content.
Talking about the main technologies,
Node.js is the heart of Netflix which many dependent technologies rely on top of node.js. Let's find out what the dependent technologies they are using.
Restify - Restify is a node.js module built specifically to enable you to build correct REST web services
Falcor - A JavaScript library for efficient data fetching
React.js - A JavaScript library for building user interfaces
NoSQL Databases/Tools used in Netflix - Amazon SimpleDB, Hadoop/Apache HBase and Cassandra.
Memcached- Free & open source, high-performance, distributed memory object caching system
Many microservice REST APIs are written in java and python.
AWS- (EC2 /S3 / Dynamodb/ EMR)
RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.
Docker
Titus
Prana
Reactive Socket
Qiro

Reference : http://techblog.netflix.com/
For more info : http://stackshare.io/netflix/netflix

Sunday, April 10, 2016

A Developer guide to design principles and patterns

A design pattern represents a set of guidelines which will help to avoid having a bad software design. The SOLID principle is the best examples for commonly used design principles. 

S - Single Responsibility Principle
O - Open/Closed Principle
L - Liskov Substitution Principle 
I - Interface Segregation Principle
D - Dependency Inversion Principle

Single Responsibility Principle - Classes/ Modules must have only one reason to change. High cohesion should be there, means class should has a single focus and single thing well done. And also a class should be loosely coupled.The single responsibility should be encapsulated by the class.Therefore strive for high cohesion and loosely coupled systems.

Open/Closed Principle - Classes/Modules should be open for extension and closed for modification. That means new behaviour can be added in future but it should not modify the existing source code and make it complex. This embrace to use or reply on abstractions/Interfaces.

Liskov Substitution Principle - Subtypes can be substitutable for their base types. It's about programming to super type. This allows us to use polymorphism in our application.

Interface Segregation Principle - Clients should not be depend on methods that they do not use. Use multiple small cohesive interfaces instead of one big fat interface. Again loosely coupled and high cohesion is embraced.

Dependency Inversion Principle - High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstraction should not depend on details/concrete. And details/concretes must depend on abstractions. Dependency Injection is based on Inversion of control. It is a design pattern which tells how one object knows about its other dependent object. Basically, it is about how components get hold in their dependencies.
Programming to interface not to the implementation.

A design pattern is a general reusable solution , which can be used to solve a problem in our application. It is kind of like a proven template.
Design patterns can be categorized to Creational, Structural, Behavioural, Concurrency and Architectural and etc.

Design pattern common usage
Designing Service Layers - Module Pattern
Overly Complicated Object Interfaces - Façade Pattern

Visibility Into State Changes / Messaging Middlewares - Observer Pattern

What is CommonJS pattern ?

CommonJS pattern in commonly used in many javascript frameworks, libraries and environments. The best example is Node.js. The objective of this pattern is to encapsulate your javascript files into reusable modules so that it can be referenced by other modules.
  • Get reference to dependency
var dependency = require('/path/to/file');
  • Declare Module
var MyModule ={}
  • Expose to others
module.exports = MyModule;

Saturday, April 9, 2016

What is Machine Learning ?

What is actually ML ?

  • We can call it as a sub-filed of Artificial Intelligence.
  • It is basically about predict future. Think like you have a bunch of raw data. Through ML we can create a model which will identify patterns that exist on those data. So when a new data set is entered, the created model will be able to recognize those patterns and predict the future.
  • There are multiple applications of ML, such as detecting credit card fraud, Determining whether a customer likely switches to a competitor. (Customer churn / loss of clients or customers), NLP (Natural language processing) , Computer vision, including object recognition, Robot locomotion , Sentiment analysis, Speech & handwriting recognition, Stock market analysis, Deciding when to do a maintenance on a factory robot.
  • Simply we can describe the process as                                                     A bunch of data --> Identify Patterns --> Create a model (Ability to recognize patterns) --> Predict Future
  • This process is iterative and should be periodically repeated if you want to get a good model.
  • Deep Blue is the first ever chess-playing computer developed by IBM, which I found interesting when it's come to real-world AI . AlphaGo is another fascinating example , which is able to play the board game Go. Nowadays the big topic is Tesla,(electric vehicles manufacturers). They use ML for the auto pilot mode in Tesla model 2/3.
  • There are plenty of open source libraries we can use to implement machine learning, such as scikit learning and Tenserflow.
  • Machine learning is the study of algorithms that learn from examples and experience.
What is a Classifier ?
  • We can call it as a function , it takes some data as input and assigns a label to it as output. Create a classifier by finding patterns in example is called Supervised Learning.Simply supervised learning is the technique to write classifier automatically.

Thursday, April 7, 2016

JavaScript Best Practices

The latest ECMAScript standard defines 7 data types:
  • Six data types that are primitives:
    • Boolean
    • Null
    • Undefined
    • Number
    • String
    • Symbol (new in ECMAScript 6)
  • and Object
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures

best way to check something is available
if(typeof x !== 'undefined'){
    console.log('Exists');
}else{
    console.log('Not Exists');
}

Equality
==   If variables are 2 diffrent types , it will convet them to the same type and check
=== There will be no type conversion

Use === as default.

Node.js ENV variable
Use  node-foremon or nodemon

Tuesday, March 29, 2016

Node.js Essentials

http://expressjs.com/en/4x/api.html

    req.params
    req.body
    req.query
----------------------------------------------------------------
    app.get('/user/:id', function(req, res){
      res.send('user ' + req.params.id);
    });

-----------------------------------------------------------------
    var app = require('express')();
    var bodyParser = require('body-parser');
    var multer = require('multer'); // v1.0.5
    var upload = multer(); // for parsing multipart/form-data

    app.use(bodyParser.json()); // for parsing application/json
    app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded


    app.post('/profile', upload.array(), function (req, res, next) {
      console.log(req.body);
      res.json(req.body);
    });

Tuesday, March 1, 2016

Angular 2 + Common Issues and Fixes



The main tutorial 
https://angular.io/docs/ts/latest/quickstart.html#!#main
https://www.youtube.com/watch?v=W14_ZArh6eo

Angular2 QuickStart npm start is not working correctly.
  • make sure set access permission to /usr/lib/mode_modules.
  • make sure to run with sudo.
  • sudo npm install -g concurrently     
  • sudo npm install -g lite-server     
  • sudo npm install -g typescript

Wednesday, February 10, 2016

Angular Nodejs Session and Cookie Management

Angular Cookie
var testApp = angular.module('testAppModule',['ngRoute','ngMaterial','ngCookies','ngMessages']);

testApp.controller("TestController", function($cookieStore,$timeout,$routeParams, $q, $log,$http,$scope,$rootScope,$location,$mdDialog) {
var value =[];

value[0] = dataObj1;
value[1] = dataObj2;
$cookieStore.put('sessionCookie', value);

var cookieRelease =$cookieStore.get('sessionCookie');

});


https://docs.angularjs.org/api/ngCookies/service/$cookieStore
https://docs.angularjs.org/api/ngCookies/service/$cookies
An HTTP cookie (also called web cookie, Internet cookie, browser cookie or simply cookie) 4093 bytes

HTML 5 Local Storage

http://www.w3schools.com/html/html5_webstorage.asp
http://stackoverflow.com/questions/19867599/what-is-the-difference-between-localstorage-sessionstorage-session-and-cookies

if (typeof(Storage) !== "undefined") {
    // Store
    localStorage.setItem("lastname", "Smith");
    // Retrieve
    document.getElementById("result").innerHTML = localStorage.getItem("lastname");
} else {
    document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}


Node.js Cookie parser and Express-Session
https://www.npmjs.com/package/express-session 
https://www.npmjs.com/package/cookie-parser