Vert.xAsynchronous framework

The open source vert. x is not really a framework, but rather a toolset with an “asynchronous non-blocking” programming model at its core. The “asynchronous non-blocking” programming model provides better performance in IO intensive applications.

Vert.xAnd I

Vert.x was first introduced in 2016, and I found it interesting to fully asynchronous programming. At the time, efforts to eliminate waiting helped fuel the Internet boom.

  • dropsIs eliminating taxi waiting times;
  • Meituan,Hungry?Are eliminating waiting times for meals;
  • As identification,Intelligent recommendationAnd new technologies are eliminating all kinds of waiting times;
  • Hospitals canThe online registrationA;
  • Banks canOnline appointmentA;
  • You can go to the moviesOnline optionsA;
  • Online paymentEliminates the time people have to pay;

The idea of eliminating waiting is affecting society and the way people live. In programming circles, full asynchrony is also becoming more accepted. The direction of development of the front-end technology will affect the development of the back-end technology, by which time the front-end asynchrony is fully mature and there are a number of new asynchronous frameworks emerging on the back-end. From then on, I began to learn vert.x.

Vert.x was initially used for third-party call testing. At that time, because the services provided by the third party were not very stable, and sometimes we would not be notified when there was a change in the word end, so I wrote a service script to detect the availability of the third party service and whether there was any change, etc., and I felt good about using it at that time.

PS: I am looking forward to RSocket…

withSpring BootThe comparison of

See performance comparisons with SpringBoot

One day, a friend of another company called me and asked me: “Spring Boot queries the data in Redis, QPS is 1600 normal?” , the detailed server configuration is as follows:

Pressure test description: Call the Http interface of SpringBoot and obtain the Value corresponding to Key=aa from Redis. The Value is also aa. Server: 2 cores 4G framework: Spring Boot Data source: Redis Data source driver: Lettuce Pressure test Result Description Pressure QPS: 1600 CPU: 60%Copy the code

The specific pressure indicators are not carefully asked, but the first reaction is still not normal. After all, normal Redis can easily provide tens of thousands of QPS.

After that, I used vert. x to write a simple interface on the existing service for testing, and the QPS could basically reach about 27000, while the CPU usage was about 20%, far below the limit of pressure measurement.

Service code (simple)

private void queryRedis(RoutingContext routingContext) {
    @Nullable String key = routingContext.request().getParam("key");
    // @todo handles key empty...
    RedisFactory.api().get(key, ar -> {
        if (ar.failed()) {
            routingContext.fail(ar.cause());
            return;
        }
        routingContext.response().end(ar.result().toBuffer());
    });
}
Copy the code

Launch parameters

java -XX:MetaspaceSize=32m -XX:MaxMetaspaceSize=128m -Xmx256m -Xms256m -XX:NewSize=1 -Xss256k -XX:-UseBiasedLocking -XX:AutoBoxCacheMax=20000 -XX:+AlwaysPreTouch -XX:-UseCounterDecay -XX:-TieredCompilation -jar Luckin - resource - 1.0 - the SNAPSHOT - fat. The jar run com. Jiaomatech. Luckin. MainVerticleCopy the code

WRK pressure test command

WRK - t8 - c128 - d90s http://127.0.0.1:8081/app/redis\? key\=aa --latency -- / 16Copy the code

First Pressure survey (for fun)

Pressure test for the first time

First pressure test JVM monitoring

Second Manometry (for fun)

Second pressure test

Second pressure test JVM monitoring

Summary (for fun)

2 m Running test @ http://127.0.0.1:8081/app/redis? Avg Stdev Max +/- Stdev Max +/- Stdev Max +/- Stdev Max +/- Stdev Max +/- Stdev Max +/- Stdev Max +/- Stdev Max 3.44k 421.98 4.53k 69.32% Latency Distribution 50% 4.43ms 75% 5.43ms 90% 9.94ms requests in 1.5m, 93.92MB Read Requests/ SEC: 27341.00 Transfer/ SEC: 1.04MBCopy the code

No contrast, no harm, vert. x runs effortlessly on 256MB of ram. Spring Boot is much more difficult to Boot on a small amount of memory, and it consumes a lot of CPU.

Vert.xCommon Application Scenarios

It’s not that vert. x is only suitable for these applications, but I personally think we can make better profits in these applications.

Resource service

Vert.x is an IO intensive service, with data coming from Mysql, Redis, etc. Vert.x provides better performance with comparable hardware than Spring Boot services.

Simple services

Vert.x is a fast way to write an Http or WebSocket service, especially for short projects. For example, a Double 11 module can be added to the existing services to deal with the activities related to double 11.

Write fast & Die fast

Vert.x is very suitable for short-lived services because it is based on the way Verticle is deployed. We just need to deploy Verticle when it is appropriate and destroy it when it is not needed.

Provide services for APP

At the end of 2019, I introduced vert. x into the technical system of the company. In the first project, VERt. x was used to provide service for APP (a food fast food APP similar to Luckin Coffee), including modules of payment, order, coupons, activities and so on. On the PC side, because most of the operations are adding, deleting, checking and modifying, and the number of users is relatively small, Spring Boot is directly used to do simple coding.

other

Once you know vert. x, you can always find value in your application.