Sunday, August 16, 2020

Java Interview @ Rocketalk: Part-2

Hi Friends,

In this post, I am sharing interview questions asked in Rocketalk: Round - 2. 



Question 1:

What is Docker swarm?

Answer:

A docker swarm is a group of either physical or virtual machines that are running the docker application and that have been configured to join together in a cluster.

Docker swarm is a container orchestration tool, meaning that it allows the user to manage multiple containers deployed across multiple host machines.

The cluster management and orchestration features embedded in the docker engine are built using swarmkit.

Swarmkit is a separate project which implements docker's orchestration layer and is used directly within docker.

A swarm consists of multiple docker hosts  which run in swarm mode and act as managers[to manage membership and delegation] and workers[which run swarm services].

A given docker host can be a manager, a worker or perform both roles.

When we create a service, we define it's optimal state(number of replicas, network and storage resources available to it, ports the service exposes to the outside world and more).

Docker works to maintain that desired state. For instance, if a worker node becomes unavailable, docker schedules that node's tasks on other nodes. A task is a running container which is part of a swarm service and managed by a swarm manager, as opposed to a standalone container.


One of the key advantages of swarm services over standalone containers is that we can modify a service's configuration including the networks and volumes it is connected to, without the need to manually restart the service. Docker will update the configuration, stop the service tasks with the out of date configuration and create new ones matching the desired configuration.


When docker is running in swarm mode, we can still run standalone containers on any of the docker hosts participating in the swarm, as well as swarm services. 

A key difference between swarm services and standalone containers is that only swarm managers can manage a swarm, while standalone containers can be started on any daemon. Docker daemons can participate in a swarm as a managers, workers or both. 

In the same way that we can use docker compose to define and run containers, we can define and run swarm services stacks. 


Question 2:

What is the difference between Kubernetes and Docker Swarm?

Answer:

Kubernetes: Kubernetes is an open source platform created by google for container deployment operations, scaling up and down and automation across the clusters of hosts. This production-ready, enterprise grade, self healing(auto-scaling, auto-replication, auto-restart, auto-replacement) platform is modular and so it can be utilized for any architecture deployment.


Kubernetes also distributes the load amongst containers. It aims to relieve the tools and components from the problem faced due to running applications in private and public clouds by placing the containers into groups and naming them as logical units. Their power lies in easy scaling, environment agnostic portability and flexible growth.


Docker Swarm:

As a platform, docker has revolutionized the manner software was packaged. Docker swarm or simply swarm is an open-source container orchestration platform and is the native clustering engine for and by docker. Any software, services or tools that run with docker containers run equally well in swarm. Also swarm utilizes the same command line from docker.


Docker Swarm tuns a pool of docker hosts into a virtual, single host. Swarm is especially useful for people who are trying to get comfortable with an orchestrated environment or who need to adhere to a simple deployment technique but also have more just one cloud environment or one particular platform to run this on.

Kubernetes versus Docker Swarm:


Application definition:

Kubernetes:  An application can be deployed in Kubernetes utilizing a combination of services (or microservices), deployments and pods.


Docker Swarm: The applications can be deployed as micro-services or services in a swarm cluster in Docker swarm. YAML files can be utilized to identify multi-container. Moreover, Docker compose can install the application.



Networking:

Kubernetes: The networking model is a flat network, allowing all pods to interact with one another. The network policies specifies how the pods interact with each other. The flat network is implemented typically as an overlay. The model needs to CIDRs one for the services and another from which pods acquire an IP address.


Docker Swarm:

The node joining a swarm cluster generates an overlay network for services that span every host in the docker swarm and a host-only docker bridge network for containers. The users have a choice to encrypt container data traffic while creating of an overlay network by on their own in docker swarm. 


Scalability:

Kubernetes: For distributed systems, kubernetes is more of an all-in-one framework. It is a complex system because it provides strong guarantees about the cluster state and a unified set of APIs. This slows down container scaling and deployment.

Docker Swarm: Docker Swarm, when compared to Kubernetes, can deploy container much faster and this allows faster reaction times to scale on demand.



Question 3:

What is the difference between execute(), executeQuery() and executeUpdate() methods?

Answer:

Definition of execute() is boolean execute(String sql) throws SQLException.

This method can be used for all types of SQL statements. If you do not know which method to use for SQL statements, this method can be the best option. This method returns a boolean value. TRUE indicates that statement has returned a ResultSet object and FALSE indicates that statement has returned an int value or returned nothing.


Definition of executeQuery() is :

ResultSet executeQuery(String sql) throws SQLException.

This method is used for sql statements which retrieve some data from the database. For example is Select statement. This method is meant to be used for select queries which fetch some data from the database. This method returns one java.sql.ResultSet object which contains the data returned by the query.


Definition of executeUpdate() is :

int executeUpdate(String sql) throws SQLException

This method is used for SQL statements which update the database in some way. For example UPDATE and DELETE statements.

This method returns an int value which represents the number of rows affected by the query. This value will be 0 for the statements which return nothing.



Question 4:

What is the use of spring-boot-starter-parent?

Answer:

It provides default configurations for our application and a complete dependency tree to quickly build our spring boot project.

It also provides default configurations for maven plugins such as maven-failsafe-plugin, maven-jar-plugin, maven-surefire-plugin, maven-war-plugin.

Beyond that it also inherits dependency management from spring-boot-dependencies which is the parent to the spring-boot-starter-parent. 


We can start using it in our project by adding this as a parent in our project's pom.xml file.

<parent>

    <groupId>org.psringframework.boot<.groupId>

    <artifactId> spring-boot-starter-parent</artifactId>

    <version>2.2.6.RELEASE</version>

</parent>


Once we have declared the starter parent in our project, we can pull any dependency from the parent by just declaring it in our dependencies tag.

Also, we do not need to define versions of the dependencies, maven will download jar files based on the version defined for starter parent in the parent tag.



Question 5:

Where does spring store the cached data?

Answer:

Spring stores the data in internal tables.

If any value is cached for some input parameter [function argument], then that method is not called and the cached value is returned for that request.

@Cacheable(cacheNames = "cacheName") : This name is very important. It is the name of the cache.

When we want to delete all the entries in any cache , use @CacheEvict(cacheName = "cacheName", allEntries = true).

We can also tell spring that only cache values for input which is greater than some value. e.g.:

@Cacheable(cacheNames = "cacheName", condition = "#id > 1").


Question 6:

What are java microservices frameworks?

Answer:

There are many java microservices frameworks available:

  • Spring and Spring Boot
  • DropWizard
  • Eclipse MicroProfiler


Spring and Spring Boot:

The framework propagates building the application into a jar and running it on the embedded tomcat server, making it a perfect combination with Docker to manage a virtualized deployment environment.


DropWizard:

Like spring boot, dropwizard applications are packaged into fat jar files with the jetty application server embedded.

DropWizard integrates the tried-and-tested java libraries into a fully functional platform: Jersey for REST, Jackson for JSON.

For Templates, it uses Freemarker and Mustache is available for java-based UIs.

DropWizard ships with no built-in dependency injection solution, but integration exists for Guice and Dagger.


That's all for this post.

Thanks for reading!!

No comments:

Post a Comment

CAP Theorem and external configuration in microservices

 Hi friends, In this post, I will explain about CAP Theorem and setting external configurations in microservices. Question 1: What is CAP Th...