Finagle : Controlling the Future Of RPC systems


logo

In this blog we’ll use Finagle to build a very simple HTTP server that is also an HTTP client — an HTTP proxy.

So Let’s Begin With Some Introduction to the Finagle.

Finagle is an extensible Remote Procedure Call (RPC system) for the JVM, used to construct high-concurrency servers.

Twitter Finagle is a great framework to write distributed applications in Scala or Java. It is written in Scala, but provides both Scala and Java idiomatic APIs.
It implements uniform client and server APIs for several protocols, and is designed for high performance and concurrency.

 

Finagle uses a clean, simple, and safe concurrent programming model, based on Futures. This leads to safe and modular programs that are also simple to reason about.

Finagle provides a robust implementation of:

  • connection pools, with throttling to avoid TCP connection churn;
  • failure detectors, to identify slow or crashed hosts;
  • failover strategies, to direct traffic away from unhealthy hosts;
  • load-balancers, including “least-connections” and other strategies; and
  • back-pressure techniques, to defend servers against abusive clients and dogpiling.

Continue reading

Posted in Java, Scala | Tagged , , , , , , | 1 Comment

Lagom Circuit Breaker: What, Why and How?


When our service depends on a third party service’s end point for response, it is possible that due to various reasons, that may vary from bad connection to an internal server error from the third party, our own service fails. A circuit breaker is employed to prevent cascading failures in our services if such scenarios were to occur. And if you are here, you are probably looking forward to customise the circuit breaker within your own service. So, shall we?

First of, when working with lagom in java, the circuit breaker functionality is on even if you don’t do anything about it. But it is always better to customise things to suit our own needs, right? So let’s first get the concept of circuit breaker clear in our head. A circuit breaker can have one of the following three states at an instant:

  1. Closed,
  2. Open, and
  3. Half-Open

Also, there are three parameters that govern the state of our circuit breaker, which we can specify in the application.conf file in the /resources folder:

  1. max-failures
  2. reset-timeout
  3. call-timeout

Consider two services S1 (client service) and S2 (supplier service), and imagine a circuit between them. A circuit breaker is like a switch that exists between these two services. Initially, this switch is closed, and the flow of request/response between the two services goes smoothly. Also, assume that we have set the values of our three parameters as below:

  • max-failures = 10,
  • reset-timeout = 15 seconds, and
  • call-timeout = 10 seconds .

Continue reading

Posted in Scala | Tagged , | 3 Comments

Key points to build an Angular application


This Blog is about Angular and why developers should choose angular to build an application. And we will also discuss difference between its current version and previous versions.

What Angular is :

Angular is perfect choice for Single Page Application(SPAs). AngularJS takes another approach. AngularJS is a structural framework for dynamic web apps. It tries to make something as small as possible the impedance mismatch between document centric HTML and what an application needs by creating new HTML constructs. AngularJS teaches the browser new syntax through a construct we call directives.

Why Angular :

Image result for why angularjs

  • Integration: It is easy to unified third party features with AngularJS as Angular integration comes pre-built into frameworks such as Telerik’s Kendo UI, Ionic, Wijmo and others.

Continue reading

Posted in Scala | Tagged , , | Leave a comment

SQL made easy and secure with Slick


Slick stands for Scala Language-Integrated Connection Kit. It is Functional Relational Mapping (FRM) library for Scala that makes it easy to work with relational databases.

Slick can be considered as a replacement of writing SQL queries as Strings with a nicer API for handling connections, fetching results and using a query language, which is integrated more nicely into Scala. You can write your database queries in Scala instead of SQL, thus profiting from the static checking, compile-time safety and compositionality of Scala. Slick features an extensible query compiler which can generate code for different backends.

SQL queries are checked at run time only when they are actually executed, but with the help of libraries available in Slick, static checking occurs a compile time, thereby preventing the code to break at runtime i.e. when using Scala instead of raw SQL for your queries you benefit from compile-time safety and compositionality.

Slick’s key features are type-safe, composable queries. Slick comes with a Scala-to-SQL compiler, which allows a (purely functional) sub-set of the Scala language to be compiled to SQL queries. With the availability of standard libraries the Scala developers can write many queries against all supported relational databases with little learning required and without knowing SQL or remembering the particular dialect. Such Slick queries are composable, which means that you can write and re-use fragments and functions to avoid repetitive code like join conditions in a much more practical way than concatenating SQL strings. The fact that such queries are type-safe not only catches many mistakes early at compile time, but also eliminates the risk of SQL injection vulnerabilities.

Continue reading

Posted in database, Scala, Security, Slick, sql, Tutorial | Tagged , , , , , | Leave a comment

What to do for overriding the PureConfig behavior in Scala ?


PureConfig has its own predefined behavior for reading and writing to the configuration files, but sometimes we got the tricky requirement in which we need some specific behavior; for example to read the config.

It is possible to override the behavior of PureConfig for a certain type by implementing another instance of ConfigReader, ConfigWriter or ConfigConvert. So in this blog we will discuss all 3 override types.

1. ConfigReader:
The default behavior of PureConfig for String is to return the string itself in the configuration.
For example, when below configuration will be read by PureConfig, it will be read as it is :
application.conf:

company {
  full-name = "Knoldus Software LLP"
  started = 2012
  employees = "80-120"
  offices = ["India", "Singapore", "US", "Canada"]
  offices-in-india {
    head-office = "Delhi"
    development = "Noida"
  }
}

Reading configuration:

import pureconfig.error.ConfigReaderFailures
import pureconfig.loadConfig

class SimpleConfig {
  def getConfig: Either[ConfigReaderFailures, Company] = loadConfig[Company]
}

case class Company(company: CompanyDetails)

case class CompanyDetails(fullName: String,
                          started: Int,
                          employees: String,
                          offices: List[String],
                          officesInIndia: Map[String, String],
                          extraActivity: Option[String])


// Calling and displaying the configuration
val simpleConfig = new SimpleConfig().getConfig

  simpleConfig match {
    case Left(ex) => ex.toList.foreach(println)

    case Right(config) => 
      println(s"Company's Name ${config.company.fullName}")
      println(s"Company started at ${config.company.started}")
      println(s"Company's strength is ${config.company.employees}")
      println(s"Company's presence are in  ${config.company.offices}")
      println(s"Company's office in India are  ${config.company.officesInIndia}")
      println(s"Company's extra activity is  ${config.company.extraActivity}")
  }

Output :

Knoldus Software LLP
2012
80-120
List(India, Singapore, US, Canada)
Map(development -> Noida, head-office -> Delhi)

This is the default behavior of ConfigReader in which the output is same as it is defined in configuration file.

Continue reading

Posted in Agile, Best Practices, big data, knoldus, Reactive, Scala | 1 Comment

Simple Java program to Append to a file in Hdfs


In this blog, I will present you with a java program to append to a file in HDFS.

I will be using Maven as the build tool.

Now to start with-

First, we need to add maven dependencies in pom.xml.

Now we need to import the following classes-

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.*;

Continue reading

Posted in big data, HDFS, Java | Tagged , , , | 1 Comment

Lagom: Getting to Know


JUST THE RIGHT AMOUNT

Lagom- [lah-gome], a swedish word meaning ‘Sufficient’ or ‘Just the right amount’.

The meaning of the name ‘Microservice’ is misunderstood by a majority of people. Taking it litrelly Micro = very small i.e. very small service is not the underlying meaning or agenda of creating Microservices. The real theme here does not revolves around the ‘size’ criteria. What really matters here is the ‘architecture‘. Lagom is created to help clear this misunderstanding and it provides the structure applications need to embrace asynchronous messaging, distributed persistence, and eventual consistency. This is all wrapped up in a productive development environment that makes it easy to launch your services and data stores, with automatic reloading when the code in one of your services changes.

According to Lightbend the focus should not be on how small the services are, but instead they should be just the right size, “Lagom” size services.

Lagom is an open source framework built with the shifting from monoliths to microservices-driven application architecture in mind. It abstracts the complexity of building, running and monitoring microservices driven applications.

UNDER THE HOOD

Behind the scenes, Lagom framework uses the Play Framework, an Akka message-driven runtime, Kafka for decoupling services, Event Sourcing, and CQRS patterns, and ConductR support for monitoring and scaling microservices in the container environment.

Lagom gives you an opiniated view on how to build Microservices that adhere to reactive stack. Lagom is built with Akka and play to make it fully asynchronous and unblocking at its core. To make it easier with this pattern to build systems that are truely reactive, it is advised to use Event Sourcing and CQRS.

Continue reading

Posted in Scala | Tagged , | Leave a comment

Remove Scala warts with WartRemover


Each time we write code, we are told to ‘maintain the coding standards’, yet many a time we fail to do so. Though there are tools which we use to help us in linting the code like scalastyle, scapegoat, scalafmt, scalariform etc. lack a bit, seems not enough to meet the standard. Basically though we use all these tools still there is wart and therefore there comes WartRemover to rescue us from these warts.

Now the question comes, What’s WartRemover ?

Well, WartRemover is a flexible Scala code linting tool. It takes the pain out of writing scala by removing some of the language’s nastier features. It works with the type system to improve the reliability of your code.

Different ways of using WartRemover

  • Using the sbt plugin
  • As a command-line tool
  • As a compiler plugin with manually provided scalac options
  • To derive macros

It catches the following issues:

Continue reading

Posted in Scala | Tagged , , | Leave a comment

Docker : linking and working with multiple containers


Prerequisites

  1. Docker installation from here.
  2. Create an account on Docker Hub

In my previous blog we had a quick overview of Docker technology. I suggest you to have a look at my previous blog for better understanding. Our application can have multiple services running in different containers. So In this blog we will know how to link multiple containers. Here we will take an example of mysql and wordpress. Here wordpress is our front-end and mysql is our back-end.

Please follow these steps carefully-

Steps

1.  Download the images of mysql and wordpress from dockerhub with your account by running these simple commands-

  • $ docker pull mysql
  • $ docker pull wordpress

2. After downloading both images successfully we will run our back-end mysql image by running this command-

Continue reading

Posted in Devops, Scala | Tagged , , , , , | Leave a comment

Magic Of Dependency Injection in Angular 2


When i was a kid Injection word was my biggest nightmare, but since I have become a programmer and jovial by doing clean, crisp and precise applications the “injection” word seems the most wonderful word to me, as this word brings so much ease, reduces so much coding effort and helps us to build a project quickly. When I took Angular 2 to build my application then as most people do, I also had a look  into Angular 2’s documentation and there i find this definition of Dependency Injection :-

Dependency injection is an important application design pattern. Angular has its own dependency injection framework, and you really can’t build an Angular application without it. It’s used so widely that almost everyone just calls it DI

They have clearly mentioned that “you really can’t build an Angular application without it so we can say that DI is the heart of AngularJS.

Pain of Maintaining As well As Testing Code Without  Dependency Injection

Let’s understand what DI really is. Consider the following code:

class College {
  constructor() {
    this.student = new Student();
    this.professor = Professor.getInstance();
    this.dean = app.get('dean');
  }
}

Continue reading

Posted in Scala | Tagged , , , , , , , | Leave a comment