Skip to content

About

Low-RSS Java REST framework with Rust Hyper I/O; optimized paths reach up to 11x RPS, ~50-95 MiB vs ~300-440 MiB Spring Boot

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Rust-Java REST

English | Türkçe

Release Java Native Engine License: MIT

A lightweight, reflection-free Java 21 REST framework powered by a Rust/Hyper native I/O plane.

Write clean, familiar Java handlers, services, and business logic while Rust handles TCP accept loops, HTTP parsing, file streaming, WebSockets, and memory-bounded backpressure over JNI.


Why Rust-Java REST?

  • Java owns business logic: Controllers, services, records, validation, JDBC, and RPC calls stay in standard Java 21.
  • Rust owns network I/O: Hyper/Tokio handles sockets, HTTP framing, multipart scanning, file streaming, and native buffer budgets.
  • Compile-time code generation: Routes, DI wiring, JSON codecs, and parameter binding are generated at build time — no classpath scanning or runtime reflection on hot paths.
  • Tiny footprint by default: Built-in runtime profiles (micro-rest, low-rss, balanced-dubbo, throughput) keep RSS predictable even in 1 CPU / 128 MiB containers.
  • Batteries included via Starters: Modular starters for REST, WebSocket, Security (JWT), OpenAPI, Outbound HTTP, Tracing, Scheduler, Redis Cache, and Dubbo.

Quick Start (5 Minutes)

1. Configure GitHub Packages

Add the GitHub Packages repository server to ~/.m2/settings.xml (requires a personal access token with read:packages):

<settings>
  <servers>
    <server>
      <id>github</id>
      <username>YOUR_GITHUB_USERNAME</username>
      <password>${env.GITHUB_PACKAGES_TOKEN}</password>
    </server>
  </servers>
</settings>

2. Create pom.xml

Use rust-java-platform-parent and add only the starters you need:

<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.reactor</groupId>
    <artifactId>rust-java-platform-parent</artifactId>
    <version>4.11.0</version>
  </parent>

  <groupId>com.example</groupId>
  <artifactId>hello-api</artifactId>
  <version>1.0.0</version>

  <repositories>
    <repository>
      <id>github</id>
      <url>https://maven.pkg.github.com/esasmer-dou/rust-java-rest</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>com.reactor</groupId>
      <artifactId>rust-java-starter-rest</artifactId>
    </dependency>
  </dependencies>
</project>

Note: Native binaries for Linux x64, Windows x64, Linux ARM64, and macOS ARM64 are bundled automatically. No manual DLL/SO setup is required.

3. Write a Handler and Entry Point

package com.example;

import com.dslplatform.json.CompiledJson;
import com.reactor.rust.annotations.GetMapping;
import com.reactor.rust.annotations.PathVariable;
import com.reactor.rust.annotations.RestController;
import com.reactor.rust.app.RestApplication;

@CompiledJson
public record Greeting(long id, String message) {}

@RestController
public final class HelloHandler {

    @GetMapping("/api/hello/{id}")
    public Greeting hello(@PathVariable("id") long id) {
        return new Greeting(id, "Hello from Rust-Java REST");
    }

    public static void main(String[] args) {
        RestApplication.run(HelloHandler.class);
    }
}

4. Build and Run

mvn clean package
java -jar target/hello-api-1.0.0.jar

Verify with curl:

curl http://127.0.0.1:8080/api/hello/42
# {"id":42,"message":"Hello from Rust-Java REST"}

Common Patterns

Pre-Serialized / Zero-Overhead JSON (RawResponse)

Use RawResponse.json(byte[]) when your handler already has UTF-8 JSON bytes (for example from a cache or static payload):

private static final byte[] OK = "{\"status\":\"UP\"}".getBytes(StandardCharsets.UTF_8);

@GetMapping(value = "/app/health", responseType = RawResponse.class)
public ResponseEntity<RawResponse> health() {
    return ResponseEntity.ok(RawResponse.json(OK));
}

Asynchronous Endpoints & Bounded Per-Key Admission

Protect downstream calls with declarative @KeyAdmission (no reflection, no blocking queue):

@Bean
public LongKeyAdmission customerAdmission() {
    return new LongKeyAdmission(true, 1, 256);
}

@PostMapping("/customers/{customerId}/refresh")
@KeyAdmission(bean = "customerAdmission", key = "customerId")
public CompletionStage<ResponseEntity<RawResponse>> refresh(
        @PathVariable("customerId") long customerId) {
    return customerService.refreshAsync(customerId);
}

Streaming Files & Packaged Static Assets

@GetMapping("/files/{name}")
public FileResponse download(@PathVariable("name") String name) {
    return FileResponse.of(Path.of("/data/files", name), "application/octet-stream");
}

@GetMapping("/assets/app.js")
public FileResponse asset() {
    return FileResponse.packagedAsset("static/app.js", "application/javascript");
}

Tuning Guide

Configuration precedence is strictly layered:

  1. JVM -D system properties & uppercase ENV_VARS (highest)
  2. External overlay file (-Dreactor.config.file=/path/prod.properties)
  3. Classpath src/main/resources/rust-spring.properties
  4. Built-in runtime profile defaults (lowest)

Built-in Runtime Profiles

Select a baseline with reactor.runtime.profile=<name> and override only what you measure:

Profile Target Workload JNI Workers Rust I/O Threads Max Connections Response Pool (S / M / L)
micro-rest 1 CPU / 128 MiB REST services 1 1 512 8 / 2 / 0
micro-rest-plus micro-rest + heavy direct/producer JSON routes 1 1 512 8 / 2 / 0
micro-dubbo Low-RSS REST + native Dubbo consumer 1 1 512 8 / 2 / 0
low-rss Small multi-core (2 CPU) services 2 2 1024 64 / 64 / 2
fast-start low-rss optimized for fastest cold start 2 2 1024 64 / 64 / 2
ready-low-latency low-rss with JSON prewarm at startup 2 2 1024 64 / 64 / 2
balanced-dubbo Multi-core REST + high-concurrency Dubbo (tokio-demux) 16 2 1200 192 / 192 / 8
throughput Maximum RPS on multi-core hosts (HTTP/1 + HTTP/2) auto (0) auto (0) 4096 512 / 512 / 32

Recipe 1: Ultra-Low Memory Container (1 CPU / 128 MiB)

src/main/resources/rust-spring.properties:

server.port=8080
reactor.runtime.profile=micro-rest
reactor.rust.http.max-inflight-body-bytes=4194304
reactor.rust.http.max-inflight-response-bytes=8388608
reactor.rust.async.max-inflight=64
reactor.rust.async.response-timeout-ms=1500

Recommended OpenJ9 21 JVM flags:

java -XX:-UseJITServer -XX:ActiveProcessorCount=1 -Xms16m -Xmx64m \
     -Xtune:virtualized -jar app.jar

Recipe 2: Balanced Production API (2–4 CPU / 256–512 MiB)

server.port=8080
reactor.runtime.profile=low-rss
reactor.rust.jni.workers=4
reactor.rust.runtime.worker-threads=2
reactor.rust.http.max-connections=2048
reactor.rust.async.max-inflight=256
reactor.rust.async.response-timeout-ms=2000

Recipe 3: High-Throughput / Large JSON Payloads (4+ CPU)

server.port=8080
reactor.runtime.profile=throughput
reactor.rust.response-pool.small-capacity=256
reactor.rust.response-pool.medium-capacity=256
reactor.rust.response-pool.large-capacity=16
reactor.rust.json.writer-retain-max-bytes=131072

Production Startup Gates (Optional)

Enable these fail-fast checks in production overlays to ensure zero reflection fallbacks at startup:

reactor.optimizer.fail-on-reflection-route-metadata=true
reactor.optimizer.fail-on-fallback=true
reactor.optimizer.fail-on-heavy-json-object-graph=true

Selective request metadata transfer (reactor.rust.request-metadata.selective-enabled) is enabled by default in 4.10.1, passing only the query and header fields each annotated handler declares.


Ecosystem & Starters (4.11.0)

Starter / Library Version Purpose
rust-java-starter-rest 4.11.0 Core HTTP server, routing, JSON, DI
rust-java-starter-security 4.11.0 Native HS256 JWT & JCA RS256/ES256/Ed25519
rust-java-starter-http-client 4.11.0 Bounded native outbound HTTP/1.1 client & circuit breakers
rust-java-starter-websocket 4.11.0 Bounded WebSocket transport & binary forwarding
rust-java-starter-openapi 4.11.0 Build-time OpenAPI generation
rust-java-starter-tracing 4.11.0 W3C trace propagation
rust-java-starter-scheduler 4.11.0 Bounded background scheduler
rust-java-starter-cache-reader / writer 4.11.0 (cache 0.11.0) Native Redis CQRS reader/writer (java-rust-cache)
rust-java-starter-dubbo 4.11.0 (dubbo 0.11.0) Native Dubbo consumer (java-rust-dubbo)
java-rust-dubbo-spring-boot-starter 0.10.0 Reflection-free Dubbo for Spring Boot 3 (repo)
java-rust-glowroot-spring-boot-starter 0.7.1 Low-RSS Glowroot telemetry (repo)

Requirements

  • Java: 21 LTS (Eclipse OpenJ9 / IBM Semeru or HotSpot)
  • Maven: 3.9+
  • Platforms: Linux x64 (GLIBC 2.17+), Linux ARM64, Windows x64, macOS Apple Silicon (ARM64)

Documentation & Samples

About

Low-RSS Java REST framework with Rust Hyper I/O; optimized paths reach up to 11x RPS, ~50-95 MiB vs ~300-440 MiB Spring Boot

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages