Why I prefer GCP for website hosting ?

Digital world of a business starts with a website. A secured website ( HTTPS enabled ) helps significantly in improving page rank in search results. App Engine of GCP automatically apply SSL
security on custom domains without any cost ( FREE quota, which is enough for small businesses), while others charge for SSL certificate or need few additional configurations, which are not easy even for a software developer. This case becomes more interesting when domain exists in one service provider and domain requests route to a server of a different service provider. Like, domain exists in Google domain and sever exists in any other cloud provider. Using multiple service providers bring more complexity to a system and operation becomes costlier. Especially for very small businesses finding developers for such a small task is very frustrating.
I have personally helped 3-4 businesses / professionals struggling with cases directly-indirectly related to SSL configuration and deployment of a website. Long story short, use Google domain + GCP’s App Engine to run websites of your business until requests to website are very low ( 10000 requests per day and mostly read requests ). Cost of the operation will be cheaper ( almost 80% cheaper) than working with small software vendors. Visit my youtube channel after Sep 18th, 2022 I will post a video on “How to buy domain + deploy website code + route requests to server from domain”.


Generics,Panic and Recover in Go

Coding in Go is fun, especially if you have experience of coding of a long period in C++ and Java. GO is concise, fast and scalable, but still it needs to improve on many fronts. These could be Generics, Panic, Recover and Error Handling. I have pushed one code in my Go repo to demonstrate how these concepts can be used in programs. Do not forget to visit my youtube channel’s playlist of Go.
More videos of interesting implementations will be added soon in Go playlist.
Here is the code with Generics,Panic,Recover and Error Handling. Try it out and comment.

package main

// Implementation : custom implementation of Stack

// What all are covered here : Generics, Panic() , Recover() , Error handling

// TODO : Change to a thread safe Stack
// TODO : Implement dynamic capacity increase once threshold reach
// TODO : Add Load factor percentage to support dynamic capacity

import (
	"fmt"
	"log"
	"strconv"
)

import (
	"errors"
)

var stackElements [STACK_SIZE]any
var currentIndex = 0

const STACK_SIZE = 10

func main() {

	var j int

	fmt.Println(" ******  PUSH elements to stack   ******")

	// iterate more to create panic
	for j = 0; j < 11; j++ {
		objTostk := "A:" + strconv.Itoa(j)
		push(objTostk)
	}

	fmt.Println(" ******  POP elements from stack   ******")

	// iterate more to create panic
	for j = 0; j < 11; j++ {
		object, errorInPop := pop()
		if errorInPop != nil {
			fmt.Println("No more element for POP")
		} else {
			fmt.Println("POPPed => ", object)
		}

	}

}

// PUSH generic objects
func push[T any](objectToStk T) {

	if currentIndex >= STACK_SIZE {
		defer func() {
			if err := recover(); err == nil {
				log.Println("stack overflow occurred", err)
			}
			currentIndex--
		}()
		// Notify panic and then recover
		panic(nil)
	}

	fmt.Println("Pushed => ", objectToStk)
	stackElements[currentIndex] = objectToStk
	currentIndex++
}

// POP generic objects
func pop() (object any, CustomError any) {
	// Error Handling
	if currentIndex == -1 {
		return nil, errors.New("stack underflow occurred")
	}
	poppedElement := stackElements[currentIndex]
	currentIndex--
	return poppedElement, nil
}

Go’s LinkedList & MongoDB connection

Are you following my go_101 github repo? Have a look there I have pushed few more Golang code.
Soon I will post a cloud native Go based REST application which will run in GKE. Will have a Grafana dashboard as well to monitor infrastructure and log analytics. Feel free to ping me in Linkedin if you need any help on Golang.
My YouTube videos of Golang are almost ready. will post in my channel very soon.

Custom LinkedList

package main

import "fmt"

type Node struct {
	value int
	next  *Node
}

type SinglyLinkedList struct {
	head   *Node
	opNode *Node
}

// Add new nodes to LinkedList
func (sl *SinglyLinkedList) add(someNum int) {

	newNode := &Node{
		value: someNum,
	}

	if sl.head == nil {
		sl.head = newNode
		sl.opNode = newNode
	} else {
		sl.opNode.next = newNode
		sl.opNode = newNode
	}
}

// Traverse to display value of each node
func (sld *SinglyLinkedList) displayList()  {
	list := sld.head
	for list != nil {
		fmt.Printf("%+v", list.value)
		list = list.next
		if(list!=nil){
			fmt.Printf(" -> ")
		}
	}
	fmt.Println()
}

func main() {
	sLinkList := SinglyLinkedList{}
	sLinkList.add(1)
	sLinkList.add(4)
	sLinkList.add(6)
	sLinkList.add(7)
	sLinkList.add(8)
	sLinkList.add(8)

	sLinkList.displayList()
}

// Output : 1 -> 4 -> 6 -> 7 -> 8 -> 8

My Cloud is Your cloud !

Believe me anyone can learn basics of Cloud ( whether it is GCP,AWS or Azure ) in a few hours or max. in a week. But basics concepts are not enough to maintain and monitor any web applications or mobile applications in cloud. Most of time after FREE trial period people buy personal paid subscription of Cloud just to try some POCs or do some RnD.In general cost of such billing comes around $30/monthly if application’s computation is very low. This cost can be minimised to 5-8$/monthly if the same application runs in a shared cloud account. One person can create different set of users and host multiple applications in one Compute Engine or EC2. There are many flavours of Compute Engine or EC2 are available which can be changed based on requirement just on few clicks.
So next time when you or your friends face similar scenario try to find out a shared cloud server to minimise your cost. And yes, do not forget to take precautions below if you are planning to follow this model

      (1) Must configure a Billing limit and alert in cloud account
      (2) Before hosting any application must check data transaction rate 
      (3) Try to get a sanity test report of new deployment or new changes

This approach is really very helpful from cost and technical help perspective. Recently one person approached me for developing a web application for his coaching institute. He had arranged a quite good budget for this application development and operations. I helped him and given a plan for reducing his overall cost ( almost 40% less than earlier cost). There are so many cloud tools/stacks available today which are very helpful for startups or low budget operations.
Ping me in Linkedin if you want to discuss with me any similar usecase. I have few readymade CloudFormation Templates which are very useful in cloud cost optimisation. I charge $20/hour for my consultation service.


Kick Start – golang

There are many reasons to learn and use golang in current projects. Do you want to start your golang journey? Well, let’s kick start ! I have created a repo to add few samples of golang codes which may help you in your journey.Feel free to connect me if you need any help on golang projects.
You can start your golang 101 with below bubble sort example. Keep following my repo for further sample push

package main

import "fmt"

func main() {

    /* Array for sorting */
	var numbers = []int{5, 6, 8, 10, 23, 6, 7, 7, 7}
	var i, j int

	for i = 0; i < len(numbers)-1; i++ {
		for j = i + 1; j < len(numbers); j++ {
			if numbers[i] > numbers[j] {
				var temp = numbers[i]
				numbers[i] = numbers[j]
				numbers[j] = temp
			}
		}
	}

	for j = 0; j < len(numbers); j++ {
		fmt.Printf("%d,", numbers[j])
	}
}


How Goroutines communicate to perform a common goal

synchronized(lock){
    while(!condition){
        lock.wait();
    }
}

Code constructs like “synchronized”,”Lock”,”wait()”,”notify()” are not new for us. From decades we are coding with similar Legacy constructs that play a key role in multithreading codes. The Legacy term! 🙂 Yes, you read it correctly! When you try similar code with Go, code is very precise, compact, friendly and superfast. Lets see why Go executes multithreading (let me correct here GREEN THREADs) code faster than others. Because there is no Thread concept in Go, instead it has Green Threads.

For deeper concepts connect me in linked.

Here I have illustrated how two Goroutines are communicating with each other through channel to print Odd and Even numbers alternatively. This may give you little insight if you are planning to write new microservices ( especially for very huge data processing and scalable products like IoT etc.. then you can think of Go!

/**
 * @author Gyanendra
 * @Date : 02/03/20
 */
package main

import (
	"fmt"
)

func main() {

	numFlag := make(chan int)
	done := make(chan bool)
	go oddNumberRoutine(numFlag, done)
	go evenNumberRoutine(numFlag, done)
	<-done
}

func oddNumberRoutine(numNeedToBePrinted chan int, printingCompleted chan bool) {

	for {
		v := <-numNeedToBePrinted
		if v >= 20 {
			printingCompleted <- true
			break
		}
		fmt.Println("Odd ==>", v)
		numNeedToBePrinted <- v + 1
	}

}

func evenNumberRoutine(numNeedToBePrinted chan int, printingCompleted chan bool) {
	numNeedToBePrinted <- 1
	for {
		v := <-numNeedToBePrinted
		if v > 20 {
			printingCompleted <- true
			break
		}
		fmt.Println("Even ==>", v)
		numNeedToBePrinted <- v + 1
	}

}

Odd ==> 1
Even ==> 2
Odd ==> 3
Even ==> 4
Odd ==> 5
Even ==> 6
Odd ==> 7
Even ==> 8
Odd ==> 9
Even ==> 10
Odd ==> 11
Even ==> 12
Odd ==> 13
Even ==> 14
Odd ==> 15
Even ==> 16
Odd ==> 17
Even ==> 18
Odd ==> 19
Even ==> 20

Get this code from my repo

Twitter live data mining using Spark streaming and Scala.

Want to work and learn live streaming data processing? Easiest way to create a twitter developer app and follow below code to ingest and store data in your AWS S3 for further analysis and processing with tools like Amazon EMR or Machine learning projects.

For deeper concepts connect me in LinkedIn. You may also outsource tech screening interview process to me.

/**
 * @author Gyanendra
 * @Date : 08/12/19
 */

import org.apache.spark.SparkConf
import org.apache.spark.streaming.twitter.TwitterUtils
import org.apache.spark.streaming.{Seconds, StreamingContext}
import twitter4j.auth.OAuthAuthorization
import twitter4j.conf.ConfigurationBuilder


object TweeterStreamReaderApp {
  def main(args: Array[String]) {

    var twitterCredentials = new Array[String](4);
    //consumerKey
    twitterCredentials(0) = "gA7xFE3S1QfVTN55Uuzb";
    //consumerSecret
    twitterCredentials(1) = "2te2Z1yFvynXcp06rc2j3zg38tNAa1zY29rOT3d5BFI";
    //accessToken
    twitterCredentials(2) = "1063309360480-61DChczOivazJZTWodLfuRRW8gDNfJ";
    //accessTokenSecret
    twitterCredentials(3) = "bFYPmpiWhFgOtdJGe95YyhOntxOQAmx0xEYtF";

    val appName = "TweeterStreamReader"
    val conf = new SparkConf()
    conf.setAppName(appName).setMaster("local[2]")
    val ssc = new StreamingContext(conf, Seconds(5))
    val Array(consumerKey, consumerSecret, accessToken, accessTokenSecret) = twitterCredentials.take(4)
    val filters = args.takeRight(args.length - 4)
    val cb = new ConfigurationBuilder
    cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey)
      .setOAuthConsumerSecret(consumerSecret)
      .setOAuthAccessToken(accessToken)
      .setOAuthAccessTokenSecret(accessTokenSecret)
    val auth = new OAuthAuthorization(cb.build)
    val tweets = TwitterUtils.createStream(ssc, Some(auth), filters)
    val englishTweets = tweets.filter(_.getLang() == "en")

    englishTweets.repartition(1)
    // lets print all rdd. Further you can store this to S3
    englishTweets.foreachRDD { (rdd, time) =>
      p(rdd)
    }

    def p(rdd: org.apache.spark.rdd.RDD[_]) = rdd.foreach(println)
    ssc.start()
    ssc.awaitTermination()
  }
}

Download this code from my repo

Alexa, build deploy my project !!

Alexa, build deploy my project !! Thinking possible ?? Yep, it is possible.
did you leave office, and forgot to trigger build ? Don’t worry your Alexa can do this for you even if you in mid of Bangalore traffic 🙂 or enjoying Friday eve hangout far far away from your Boss 🙂 !
Keep visiting here I am gonna post how you can trigger build, deploy through Alexa ( not all projects at present but later chances are very high that any project can be built like this).

Keep visiting ……. to see updated content.

Lambda version of Print numbers alternately using two threads in Java

Lambda expressions in Java 8 are very powerful and therefore very compelling. Here are just a few of the key benefits to using lambda expressions in Java:

  • Conciseness
  • Reduction in code bloat
  • Readability
  • Elimination of shadow variables
  • Encouragement of functional programming
  • Code reuse
  • Enhanced iterative syntax
  • Simplified variable scope
  • Less boilerplate code
  • JAR file size reductions
  • Parallel processing opportunities

In this post, you can see how in few lines we can finish code to print numbers alternatively by Two threads.

import java.util.stream.IntStream;

public class EvenOddThreadLambdaDemo {

    static Integer number = new Integer(1);
    static Object LOCK = new Object();

    public static void main(String[] args) {


        IntStream.range(0, 2).forEach(i -> new Thread(() -> {
            printNumbers();
        }).start());
    }

    private static void printNumber() {
        while (number != 21) {
            synchronized (LOCK) {
                System.out.println(Thread.currentThread().getName()+" "+number++);
                LOCK.notify();
                try {
                    LOCK.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
/*
 Odd--> 1
 Even--> 2
 Odd--> 3
 Even--> 4
 Odd--> 5
 Even--> 6
 Odd--> 7
 Even--> 8
 Odd--> 9
 Even--> 10
 Odd--> 11
 Even--> 12
 Odd--> 13
 Even--> 14
 Odd--> 15
 Even--> 16
 Odd--> 17
 Even--> 18
 Odd--> 19
 Even--> 20
*/