LIGHT

  • News
  • Docs
  • Community
  • Reddit
  • GitHub

Spring Boot

Recently, one of the big customers asked if we can integrate light-4j middleware handlers to address cross-cutting concerns to Spring Boot applications. They want to leverage the light-4j middleware handlers as they are faster, smaller and easier to customize. Most importantly, these handlers work with Light infrastructure to form an ecosystem to support microservices architecture. In this way, they can migrate their thousands of Spring Boot applications to Light without changing the existing controllers and business logic. The different development teams welcome the idea as they can still use Spring Boot to develop or maintain their applications without knowing the light-4j middleware injections as the DevOps team handles it in the pipeline.

The customer realizes the performance difference between Light-4j and Spring Boot as they have compared both with their typical applications. While we are working on the integration in light-spring-boot project, we can easily inject Light-4j handlers to the Spring Boot application in the same process. So we did a simple performance test to figure out what is the difference between the Light-4j implementation vs. Spring Boot controller.

If you want to watch a video walk through.

Setup

We first generated a Spring Boot application with https://start.spring.io/ with Web dependency with Spring Boot 2.1.3 version. Once the project is downloaded to the local, we switch the default Tomcat embedded server to Undertow

We have updated the pom.xml to exclude the Tomcat server and added Undertow to the dependency.

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-tomcat</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-undertow</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

Spring Controller

We have added a RestController as following. This is a very simple controller that returns OK to the /spring-boot path.

package com.networknt.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {
    @RequestMapping("/spring-boot")
    public String greeting() {
        return "OK";
    }
}

Light-4j Handler

Now let’s inject a light-4j handler to return OK to the /light-4j path in the SpringBootApplication.

package com.networknt.demo;

import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.RoutingHandler;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.util.Headers;
import io.undertow.util.Methods;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.undertow.UndertowBuilderCustomizer;
import org.springframework.boot.web.embedded.undertow.UndertowDeploymentInfoCustomizer;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.context.annotation.Bean;

import java.util.Collections;

@SpringBootApplication
public class DemoApplication {

	@Bean
	UndertowServletWebServerFactory embeddedServletWebFactory() {
		UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
		factory.addDeploymentInfoCustomizers(new UndertowDeploymentInfoCustomizer() {
			@Override
			public void customize(DeploymentInfo deploymentInfo) {
				deploymentInfo.addInitialHandlerChainWrapper(handler -> {
							return Handlers.path()
									.addPrefixPath("/", handler)
									.addPrefixPath("/light-4j", getTestHandler());
						}
				);
			}
		});

		return factory;
	}

	static HttpHandler getTestHandler() {
		return new HttpHandler() {
			@Override
			public void handleRequest(HttpServerExchange httpServerExchange) throws Exception {
				httpServerExchange.getResponseSender().send("OK");
			}
		};
	}

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

As you can see, we have added a customizer to handle the /light-4j with an Undertow HttpHandler, and Spring Boot still handles other paths. Light-4j handler is the same with externalized configuration and other enterprise-level features. When you are using light-spring-boot, this injection will be done transparently with the pom.xml dependency and Spring @component scanning.

Start the Server

To build the server, run the following maven command.

mvn clean install

To start the server, run the following command.

java -jar target/performance-0.0.1-SNAPSHOT.jar

Now you can test the two paths by issue the following curl commands. The response will be the same OK.

Spring Boot

curl http://localhost:8080/spring-boot

Light-4j

curl http://localhost:8080/light-4j

Performance

We are using the wrk to test the perforamnce and a wrk pipeline.lua to generate as many requests as possible. To learn how to use wrk on Linux and Mac, please visit https://doc.networknt.com/tool/wrk-perf/. If you don’t want to create the pipeline.lua file manually, then you can checkout the microservices-framework-benchmark which has the file in the root folder.

Now, let’s test the performance of these two different paths.

Spring Boot

steve@freedom:~/networknt/microservices-framework-benchmark$ wrk -t8 -c128 -d30s http://localhost:8080 -s pipeline.lua --latency -- /spring-boot 64
Running 30s test @ http://localhost:8080
  8 threads and 128 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    19.89ms   13.15ms 103.76ms   64.24%
    Req/Sec    30.50k     2.27k   37.76k    69.58%
  Latency Distribution
     50%   18.79ms
     75%   29.92ms
     90%   41.53ms
     99%    0.00us
  7286784 requests in 30.02s, 0.96GB read
Requests/sec: 242744.25
Transfer/sec:     32.64MB

Light-4j

steve@freedom:~/networknt/microservices-framework-benchmark$ wrk -t8 -c128 -d30s http://localhost:8080 -s pipeline.lua --latency -- /light-4j 64
Running 30s test @ http://localhost:8080
  8 threads and 128 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     0.93ms  588.47us  15.83ms   62.78%
    Req/Sec   617.40k    49.68k    1.24M    88.89%
  Latency Distribution
     50%    0.98ms
     75%    1.54ms
     90%    2.23ms
     99%    0.00us
  147690816 requests in 30.10s, 13.89GB read
Requests/sec: 4906794.22
Transfer/sec:    472.63MB

In comparing the two results, we can see that the light-4j path achieves about 20 times the throughput of spring-boot path and the latency is 0.93ms vs. 19.89ms. Imagine if you wanted to reach the same throughput and same latency of light-4j path: how many Spring Boot instances would be needed? We have one customer who switched from Spring Boot to Light-4j for their service that collects data from IoT devices and they reduced from 250 medium size AWS VMs to only 4 small size AWS VMs with much better performance.

The test was done on my desktop computer with AMD 2700X and 32GB memory. The source code can be found at Github and you can run it locally.

  • About Light
    • Overview
    • Testimonials
    • What is Light
    • Features
    • Principles
    • Benefits
    • Roadmap
    • Community
    • Articles
    • Videos
    • License
    • Why Light Platform
  • Getting Started
    • Get Started Overview
    • Environment
    • Light Codegen Tool
    • Light Rest 4j
    • Light Tram 4j
    • Light Graphql 4j
    • Light Hybrid 4j
    • Light Eventuate 4j
    • Light Oauth2
    • Light Portal Service
    • Light Proxy Server
    • Light Router Server
    • Light Config Server
    • Light Saga 4j
    • Light Session 4j
    • Webserver
    • Websocket
    • Spring Boot Servlet
  • Architecture
    • Architecture Overview
    • API Category
    • API Gateway
    • Architecture Patterns
    • CQRS
    • Eco System
    • Event Sourcing
    • Fail Fast vs Fail Slow
    • Integration Patterns
    • JavaEE declining
    • Key Distribution
    • Microservices Architecture
    • Microservices Monitoring
    • Microservices Security
    • Microservices Traceability
    • Modular Monolith
    • Platform Ecosystem
    • Plugin Architecture
    • Scalability and Performance
    • Serverless
    • Service Collaboration
    • Service Mesh
    • SOA
    • Spring is bloated
    • Stages of API Adoption
    • Transaction Management
    • Microservices Cross-cutting Concerns Options
    • Service Mesh Plus
    • Service Discovery
  • Design
    • Design Overview
    • Design First vs Code First
    • Desgin Pattern
    • Service Evolution
    • Consumer Contract and Consumer Driven Contract
    • Handling Partial Failure
    • Idempotency
    • Server Life Cycle
    • Environment Segregation
    • Database
    • Decomposition Patterns
    • Http2
    • Test Driven
    • Multi-Tenancy
    • Why check token expiration
    • WebServices to Microservices
  • Cross-Cutting Concerns
    • Concerns Overview
  • API Styles
    • Light-4j for absolute performance
    • Style Overview
    • Distributed session on IMDG
    • Hybrid Serverless Modularized Monolithic
    • Kafka - Event Sourcing and CQRS
    • REST - Representational state transfer
    • Web Server with Light
    • Websocket with Light
    • Spring Boot Integration
    • Single Page Application
    • GraphQL - A query language for your API
    • Light IBM MQ
    • Light AWS Lambda
    • Chaos Monkey
  • Infrastructure Services
    • Service Overview
    • Light Proxy
    • Light Mesh
    • Light Router
    • Light Portal
    • Messaging Infrastructure
    • Centralized Logging
    • COVID-19
    • Light OAuth2
    • Metrics and Alerts
    • Config Server
    • Tokenization
    • Light Controller
  • Tool Chain
    • Tool Chain Overview
  • Utility Library
  • Service Consumer
    • Service Consumer
  • Development
    • Development Overview
  • Deployment
    • Deployment Overview
    • Frontend Backend
    • Linux Service
    • Windows Service
    • Install Eventuate on Windows
    • Secure API
    • Client vs light-router
    • Memory Limit
    • Deploy to Kubernetes
  • Benchmark
    • Benchmark Overview
  • Tutorial
    • Tutorial Overview
  • Troubleshooting
    • Troubleshoot
  • FAQ
    • FAQ Overview
  • Milestones
  • Contribute
    • Contribute to Light
    • Development
    • Documentation
    • Example
    • Tutorial
“Spring Boot” was last updated: July 5, 2021: fixes #275 checked and corrected grammar/spelling for majority of pages (#276) (b3bbb7b)
Improve this page
  • News
  • Docs
  • Community
  • Reddit
  • GitHub
  • About Light
  • Getting Started
  • Architecture
  • Design
  • Cross-Cutting Concerns
  • API Styles
  • Infrastructure Services
  • Tool Chain
  • Utility Library
  • Service Consumer
  • Development
  • Deployment
  • Benchmark
  • Tutorial
  • Troubleshooting
  • FAQ
  • Milestones
  • Contribute