Monday 30 July 2018

Redis vs Memcached

One major difference is that Memcache has an upper memory limit at all times, while Redis does not by default (but can be configured to). If you would always like to store a key/value for certain amount of time (and never evict it because of low memory) you want to go with Redis. Of course, you also risk the issue of running out of memory

======================================================================

Memcached is good at being a simple key/value store and is good at doing key => STRING. This makes it really good for session storage.
Redis is good at doing key => SOME_OBJECT.
=======================================================================




Prototype pattern

Prototype pattern can be achieved by deep cloning of an object.

For example:
Problem:
1) I have invoked the DB for an employee details, assign the object to X reference
2) I need one more object of the same employee, we can reuse the X reference, like Employee Y = X;
3) But there is a problem here. If any value is changed in X reference, that will get reflected on Y reference as well.

Problem:
To avoid this, we need to invoke the DB again at step 2, that would be an expensive call.

Solution:
To avoid all these issues, we can go with an Deep Cloning of an Employee object. Hence we are creating a prototype of an X object.

Employee Y = X.clone();

If we use Shallow Cloning, we may not get values of the inner most objects. 
For example, Empoyee ==> Address details.

To achieve this, we need to go with Deep cloning. 

Builder Pattern

We can build the object as per our convenience, 
there is no hard rule to pass certain amount of parameters.

For example, if we have a constructor with 3 parameters, 
and if we need to create an object, 
we need to pass the values to the constructors even if we don't know what is age. 

To avoid it, we can use Builder pattern. 
Where we can build the object based on the values we have.

public class Employee {
    private String empname;
    private String age;
    private String department;

    public String getEmpname() {
        return empname;
    }

    public Employee setEmpname(String empname) {
        this.empname = empname;
        return this;
    }

    public String getAge() {
        return age;
    }

    public Employee setAge(String age) {
        this.age = age;
        return this;
    }

    public String getDepartment() {
        return department;
    }

    public Employee setDepartment(String department) {
        this.department = department;
        return this;
    }
}

public class EmployeeTest {
    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.setAge("10").setDepartment("IT").setEmpname("Doll");

        System.out.println(emp.getDepartment());
    }
}

Friday 27 July 2018

Java 9 - New features

JShell : 
It's an editor where users can quickly test/learn the Java skills.
To test even System.out.println("Hello.."); --> we need to create class, public static void main(String st[]) etc....
We need to know what is Class/String/Object/System classes....
To avoid all these, Java has released JShell, it's an editor, it just allows user to enter print("hello....");
To start jshell, we just need to enter C:/> jshell PRINTING
This command opens editor jshell> 
Now user can just enter jshell>print("hello"); ==> This prints out hello
Can also calculate jshell> 10+20 ==> prints 30
Can also test the operators jshell> 10 > 20 ==> print false.


JPMS (Java Platform Module System):
From Java 9 onwards, JDK has become Modularized.
Till Java 8, Java is completely runs on Jar file approach.
These are few problems till Java 8 (with Jar file approach):
1) NoClassDefNotFoundError Exception
2) Version conflicts
3) Anybody can access jar files code (Security issues)
4) To execute 'hello world' code, we require 400 MB JRE (includes rt.jar) - its completely a monolithic approach
All these issues combinely called "Jar Hell" issue
From Java 9, there is no tools.jar/rt.jar, rt.jar is several modules.
Only required modules we can use at runtime, no need to load the entire rt.jar

JLINK (Java Linker):
It's a tool create our own customized JRE with small size. No need to load complete rt.jar/tools.jar
Till Java8, to execute 'hello world' code, we require 400 MB JRE (includes rt.jar/tools.jar).
By using Java9, we can use only required classes (Class/String/Object/System classes....) we can run "Hello world" code, and create our own JRE.
Once we get our own JRE, we can deploy that JRE and run our small JRE on IOT devices/Micro Chips etc.
To create our own JRE use this command : $ jlink --module-path <modulepath> --add-modules <modules> --limit-modules <modules> --output <path>

HTTP/2 Client:
To send and receive HTTP request/responses we need use Browsers. 
To send HTTP request from Java application we need to use HTTP Client.
Problems with current HTTP class till Java 8:
1) To send HTTP request  we use HttpURLConnection, but this is released in 1997. 
As per the current standards we are still using legacy classes, it's not good for the performances.
2) At a time only one request we can send per TCP/IP connection, if we try to send more requests performance will automatically come down.
3) Supports only for text, not for Binary data.
4) Always works in Blocking mode, Synchronous mode, until we get response client will be in a blocking mode.
HTTP/2 client can fix all the above issues.
1) Can send multiple requests
2) Supports Binary/Text data
3) It supports Synchronous/Asynchronous modes

Process API Updates:
Until Java 8, it's difficult to communication with Processor is very difficult.
To get Processor level data, we need to write huge amount of code. 
With a single line of code we can get Process ID.
To get process id, System.out.println(ProcessHandle.current().pid()); ==> ProcessHandle is included in java.lang package itself. Not required of any imports.
From Java 9 onwards communication with Processor level as become very easy.

Private methods inside 'interfaces' :
This features has been introduced to make a reusable methods inside interface itself.
Until Java 8, we have - default and static methods in interface. 
If there is any common code with in multiple default methods, we can keep those common code inside private method.
Without effecting implementation classes if we want code re-usability we need to go for private methods. 
So that it's not going to impact the implemented classes.

'try' with resources :
Till Java 8, we can use syntax like : 
try(FileWriter fw = new FileWriter("writer.txt"); FileReader fr = new FileReader("reader.txt")){
}

From Java 9 ,we can declare them outside and use references with in try(...).
FileWriter fw = new FileWriter("writer.txt"); 
FileReader fr = new FileReader("reader.txt")
try(fw; fr){
}

Factory methods to create Unmodified collections:
Until Java 8:
List<String> li = new ArrayList<>();
li.add("A");li.add("B");li.add("C");
List<String> unmodif = Collections.unModifiableList(li);

Java 9:
List<String> li = List.of("A","B","C");

Stream  API Enhancements:
Stream we use to process the collection objects.
Few methods added in Java 9:
1) takeWhile() --> Object method
2) dropWhile() --> Object method
3) Stream.iterate() --> static method
4) Stream.ofNullable() --> To check the null values --> static method

<> operator:
Until Java 8, we can declare <> operator for all Classes.
From Java 9, we can use it for Anonymous inner classes: 
example:
List<String> al = new ArrayList<>(){
//This is an annonymous inner class.
};

SafeVarargs Annotation:
Until Java 8:
We can use this annotation for static methods/ final methods/ constructor
From Java 9: 
We can use for private methods also.

Until Java 8:
method(String... st);
methodL(List<String>... li){
// This method will generate "Heap Pollution" WARNINGS.


From Java 9:
If we are sure that Heap Pollution doesn't occur, we can suppress these warnings using an Annotation.
@SafeVarargs
methodL(List<String>... li){
//No warnings now.


G1GC(Garbage First Garbage Collector):
Different Types of Garbage Collectors:
Serial GC
Parallel GC
Concurrent Mark Sweep(CMS) GC
G1 GC -> is introduced in Java 6 itself.

Until Java 8 : 
Default Garbage Collector is 'Parallel GC'

From Java 9:
G1 GC has become the default GarbageCollector. 
Total Heap is divided into multiple regions and G1GC always chooses region which are having highest number of objects eligible for GC, then it clears that region.

Tuesday 24 July 2018

Architect Topics



Continuous Integration (http://java-wcs-development.blogspot.com/2018/07/continuous-integration-ci.html)

Continuous Delivery/Deployment (http://java-wcs-development.blogspot.com/2018/07/continuous-delivery-vs-continuous.html)

Blue Green Deployment (https://www.thoughtworks.com/insights/blog/implementing-blue-green-deployments-aws)

Finally, another way to perform the blue-green switch with Route53 is using Weighted Round-Robin. This works for both regular resource record sets as well as alias resource record sets. You have to associate multiple answers for the same domain/sub-domainand assign a weight between 0-255 to each entry. When processing a DNS query,Route53 will select one answer using a probability calculated based on those weights. To perform the blue-green switch you need to have an existing entry for the current “blue” environment with weight 255 and a new entry for the “green” environment with weight 0. Then, simply swap those weights to redirect traffic from blue to green.






Continuous Integration (CI)

Continuous Integration (CI) is a development practice that requires developers to integrate code into a shared repository several times a day. Each check-in is then verified by an automated build, allowing teams to detect problems early.

By integrating regularly, you can detect errors quickly, and locate them more easily.

Continuous Integration doesn’t get rid of bugs, but it does make them dramatically easier to find and remove. — Martin Fowler, Chief Scientist, ThoughtWorks

Team responsibilities:
==================
Check in frequently
Don’t check in broken code
Don’t check in untested code
Don’t check in when the build is broken
Don’t go home after checking in until the system builds

Continuous Delivery vs Continuous Deployment

Puppet/GoCD/Bamboo/Jenkins


While continuous deployment may not be right for every company, continuous delivery is an absolute requirement of DevOps practices. Only when you continuously deliver your code can you have true confidence that your changes will be serving value to your customers within minutes of pushing the "go" button, and that you can actually push that button any time the business is ready for it.

Thursday 19 July 2018

AWS - Scripts to execute while creating the EC2 instance


#!/bin/bash
yum install httpd -y
yum update -y
aws s3 cp s3://bucketname/index.html /var/www/html/
service httpd start
chkconfig httpd on

Monday 16 July 2018

java enum vs interface vs class constants

public enum EnumEx {

    MALE("male"),FEMALE("");
    private String value;

    EnumEx(String value){
        this.value = value;
    };

    public String value() {
        return value;
    }
}


public class TestEx {
    public static void main(String[] args) {
        System.out.println(EnumEx.MALE.value()); // custom method value()
        System.out.println(EnumEx.MALE.name()); // predefined method name()
    }
}

output:

male
MALE

=============================================

Can avoid using:

public interface Constants{
  String MALE = "male";
  .....
}

Enum by default is Singleton and it's threadsafe.

Thursday 5 July 2018

AWS - Generate signed url for S3 objects

package com;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.HttpMethod;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;

import java.io.IOException;
import java.net.URL;

public class GeneratePresignedURL {

    private static BasicSessionCredentials sessionCredentials;

    public static void main(String[] args) throws IOException {
        String clientRegion = "us-east-1";
        String bucketName = "bucketname";
        String objectKey = "CloudServices.png";

        String AccessKeyID = "ASIAIPORVHUBITG7";
        String SecretAccessKey = "WYOrynMz20B5ORHpu9f/m+ufH2t4+fkTjnb";
        String SessionToken = "XdzEIj//////////wEaDBx8AUTyX+PoMuFXUiLrARdFmJCz0ffbywapf9EPCzmy960mugqjVfy1uSoN4+iaSn/GjdE0x5kDGJ330foAe2XxvkbjVzpl4CN7ui6Z/oOiJC25H5/Y3vrV9qmDT/5MoT/CVnUz8vcxGtoDF/UyR5znoy5BlyqkagCaB/RmO6MdzTf9bqh4lPqU8ASn4yQ0YlVdQGvQvDZtsts0hits2pPdvwXKGOmX5zENFTscldFA1fCGK2fOmJp+UbaU+C7KzSNq4io48VdPEYBqSQZxl4W9ZIcdSkrVSzq7q17LCkpB6fVg6bSqmN/0Q3Ej0o0CfLhdIwiEMcfhH2wo4/b22QU=";

        try {

            sessionCredentials = new BasicSessionCredentials(AccessKeyID, SecretAccessKey, SessionToken);

            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(clientRegion)
                    .withCredentials(new AWSStaticCredentialsProvider(sessionCredentials))
                    .build();


            // Set the presigned URL to expire after one hour.            java.util.Date expiration = new java.util.Date();
            long expTimeMillis = expiration.getTime();
            expTimeMillis += 1000 * 60 * 60;
            expiration.setTime(expTimeMillis);

            // Generate the presigned URL.            System.out.println("Generating pre-signed URL.");
            GeneratePresignedUrlRequest generatePresignedUrlRequest =
                    new GeneratePresignedUrlRequest(bucketName, objectKey)
                            .withMethod(HttpMethod.GET)
                            .withExpiration(expiration);
            URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);

            System.out.println("Pre-Signed URL: " + url.toString());
        }
        catch(AmazonServiceException e) {
            // The call was transmitted successfully, but Amazon S3 couldn't process            // it, so it returned an error response.            e.printStackTrace();
        }
        catch(SdkClientException e) {
            // Amazon S3 couldn't be contacted for a response, or the client            // couldn't parse the response from Amazon S3.            e.printStackTrace();
        }
    }
}


output:

https://bucketname.s3.amazonaws.com/CloudServices.png?
X-Amz-Security-Token=XdzEIj//////////wEaDBx8AUTyX+PoMuFXUiLrARdFmJCz0ffbywapf9EPCzmy960mugqjVfy1uSoN4+iaSn/GjdE0x5kDGJ330foAe2XxvkbjVzpl4CN7ui6Z/oOiJC25H5/Y3vrV9qmDT/5MoT/CVnUz8vcxGtoDF/UyR5znoy5BlyqkagCaB/RmO6MdzTf9bqh4lPqU8ASn4yQ0YlVdQGvQvDZtsts0hits2pPdvwXKGOmX5zENFTscldFA1fCGK2fOmJp+UbaU+C7KzSNq4io48VdPEYBqSQZxl4W9ZIcdSkrVSzq7q17LCkpB6fVg6bSqmN/0Q3Ej0o0CfLhdIwiEMcfhH2wo4/b22QU=
&X-Amz-Algorithm=AWS4-HMAC-SHA256
&X-Amz-Date=20180705T064708Z
&X-Amz-SignedHeaders=host
&X-Amz-Expires=3599
&X-Amz-Credential=ASIAIPORVHUBITG7%2F20180705%2Fus-east-1%2Fs3%2Faws4_request
&X-Amz-Signature=39a033db5db1acd7ce579c7cf66adf3b14117676834345a0acdfa0fd1c42c364

Tuesday 3 July 2018

AWS - Public IP vs Elastic IP

When you launch an EC2 instance, you recieve a Public IP address by which that instance is reachable.
Once you stop that instance and restart the you get a new Public IP for the same instance's.

So, Public IP get's changed everytime for an instance after stop/start.

To overcome with this problem, we attach an Elastic IP to an Instance which doesn't change after you stop / start the instance as many times.

Advantage of Having Elastic IP
-> It is kind of static IP for your Instance.
-> Doesn't change after stop/start.

If you have Elastic IP in your account and  it's not in use, then you will be charged for it.

==================================================

The advantage of an Elastic IP (EIP) is that you can move an IP address from one host to another. This can help you to build a more resilient system.

Let's say your host is at 1.2.3.4 and that host becomes unhealthy. You can then start up a new, equivalent host and move 1.2.3.4 from the bad host to the good host. No DNS changes would be required, which is a win considering that many clients do not respect DNS TTL and would otherwise continue to try to talk to the old host. This is a simple (but fairly blunt) way to provide failover. You can also use it when upgrading a host, in which case you would run the new host side-by-side with the old, validate the new host, and then move the EIP to the new host.

PS some EC2 instances come with a public IP, not all EC2 instances. If you are launching into VPC and you do not request a public IP then the instance does not get one. This is how you build a private subnet.