Fastify (Node.js) vs Webflux (Spring): comparación de rendimiento de Hello world
Una de mis series de artículos populares ha sido la comparación de rendimiento entre Node.js y Springboot. Comencé la serie comparando el servidor HTTP nativo de Node con Springboot (el artículo está aquí ). Como el artículo obtuvo un número récord de visitas, recibí comentarios que:
- Un servidor nativo no debe compararse con un marco como Spring
- Para la comparación de manzanas con manzanas, se debe usar webflux del marco reactivo de Spring en lugar del grupo de subprocesos de Spring
Con este artículo, sigo abordando el punto número 2. Esta vez, voy a comparar Spring Webflux con el marco fastify de Node.js. ¿Por qué Fastify esta vez? Porque Fastify es bastante rápido en comparación con Express. Los resultados son difíciles de predecir. Averigüemos quién gana esta vez.
Una nota sobre Spring Webflux
¡La programación reactiva es un estilo de programación que trata con flujos de datos asíncronos ! Un modelo de desarrollo que se centra en observar flujos de datos y reaccionar ante los cambios y propagarlos. Aprende programación reactiva para crear sistemas reactivos: ¡sistemas distribuidos altamente resistentes o microservicios! Spring Webflux es un ejemplo de programación reactiva.
La configuración de la prueba es la misma:
- MacBook M1 con 16G de RAM
- Node.js v19.8.1
- Primavera 3.0.4 sobre Java 17
Nodo.js
import Fastify from "fastify";
const fastify = Fastify({ logger: false });
fastify.get("/", () => {
return "Hello world!";
});
fastify.listen({ port: 3000 });
El código 'hello world' de Spring Webflux se ha tomado de su documentación oficial:
HolaWorldHandler.java
package hello;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Component
public class HelloWorldHandler {
public Mono<ServerResponse> hello(ServerRequest request) {
return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN)
.body(BodyInserters.fromValue("Hello World!"));
}
}
package hello;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
@Configuration(proxyBeanMethods = false)
public class HelloWorldRouter {
@Bean
public RouterFunction<ServerResponse> route(HelloWorldHandler helloWorldHandler) {
return RouterFunctions
.route(GET("/"), helloWorldHandler::hello);
}
}
package hello;
import reactor.core.publisher.Mono;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
@Component
public class HelloWorldClient {
private final WebClient client;
public HelloWorldClient(WebClient.Builder builder) {
this.client = builder.baseUrl("http://localhost:3000").build();
}
public Mono<ClientResponse> getMessage() {
return this.client.get()
.uri("/")
.exchange();
}
}
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
HelloWorldClient helloWorldClient = context.getBean(HelloWorldClient.class);
}
}
Vayamos directamente a los resultados.
A diferencia de Express, Fastify puede competir con Springboot con Webflux, pero pierde en RPS y otras métricas de latencia. Fastify puede puntuar en el uso de CPU y memoria.
GANADOR: Spring Webflux
¡Gracias por leer! Déjame saber cualquier comentario, sugerencia, etc.

![¿Qué es una lista vinculada, de todos modos? [Parte 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































