Finagle: Server and Client

people using laptops while sitting on chair
Reading Time: 2 minutes

Finagle is a powerful network framework that provides developers with the tools they need to build highly scalable and fault-tolerant distributed systems.

At the core of Finagle’s architecture are the server and client components, which work together to enable reliable communication between different services.

Finagle Server

The Finagle server component provides the infrastructure for building scalable, fault-tolerant network services.

A Finagle server is built using a “stack” of modules, each of which provides a specific functionality such as request handling, load balancing, or service discovery.

Developers can customize these stacks to meet the specific requirements of their service.

Finagle’s server architecture is designed to be highly concurrent, leveraging asynchronous programming models and non-blocking I/O to enable high throughput and low latency.

Finagle servers support multiple protocols such as HTTP, Thrift, and gRPC, making them versatile for building various types of services.

One of the key features of the Finagle server is its support for service-oriented architecture (SOA). SOA is a design pattern that emphasizes modularity and loose coupling of service components.

Finagle servers provide built-in support for service discovery and load balancing, making it easy to build scalable, distributed systems using this architecture.

import com.twitter.finagle.Http;

import com.twitter.finagle.Service;

import com.twitter.finagle.http.Request;

import com.twitter.finagle.http.Response;

import com.twitter.finagle.http.Status;

import com.twitter.util.Future;

public class MyServer {

    public static void main(String[] args) {

        Service<Request, Response> service = (Request request) -> {

            Response response = Response.apply();

            response.status(Status.Ok());

            response.setContentString("Hello, world!");

            return Future.value(response);
        };

        Http.serve(":8080", service);
    }

}

Finagle Client

The Finagle client component provides the infrastructure for building reliable and scalable network clients that can communicate with different services . It is built using a stack of modules that provide functionalities such as load balancing, retry logic, and request routing.

Finagle clients leverage the same asynchronous programming models and non-blocking I/O that the server component uses, enabling high throughput and low latency communication with different services.

The client component supports multiple protocols such as HTTP, Thrift, and gRPC. That makes it easy to build clients that communicate with different services.

One of the key features of the Finagle client is its support for load balancing. The client component can distribute requests across multiple instances of a service, providing fault tolerance and high availability.

The client component also supports circuit breaking, which can help prevent cascading failures in the system.

import com.twitter.finagle.Http;

import com.twitter.finagle.Service;

import com.twitter.finagle.http.Methods;

import com.twitter.finagle.http.Request;

import com.twitter.finagle.http.Response;

import com.twitter.finagle.http.Status;

import com.twitter.util.Future;


public class MyClient {

    public static void main(String[] args) {

        Service<Request, Response> client = 

Http.newService("localhost:8080");

        Request request = Request.apply(Methods.Get(), "/");

        Future<Response> responseFuture = client.apply(request);

        responseFuture.onSuccess(response -> {

            if (response.status().equals(Status.Ok())) {

                System.out.println("Response content: " + response.contentString());

            }
        });
    }

}

Conclusion

Finagle’s server and client components provide a powerful infrastructure for building reliable, scalable, and fault-tolerant distributed systems.

The server component enables developers to build scalable network services using a modular, customizable architecture.

The client component provides a reliable and scalable way to communicate with different services. Together, these components form a robust framework that enables developers to build highly scalable, fault-tolerant distributed systems with ease.

References

Twitter Finagle : https://twitter.github.io/finagle/

For a more technical blog, you can refer to the Knoldus bloghttps://blog.knoldus.com/

Written by 

Prakhar is a Software Consultant at Knoldus . He has completed his Masters of Computer Applications from Bharati Vidyapeeth Institute of Computer Applications and Management, Paschim Vihar . He likes problem solving and exploring new technologies .