Class NioBackend
- All Implemented Interfaces:
TransportBackend, AutoCloseable
This backend uses traditional Java NIO (SocketChannel + Selector) and works on
all platforms. While not as fast as io_uring, it provides reliable baseline performance with
zero-allocation hot paths.
Performance Comparison with io_uring
| Feature | NIO | io_uring |
|---|---|---|
| Platform | All JVM platforms | Linux 5.1+ |
| Registered buffers | No | Yes (1.7x speedup) |
| Batch submission | No | Yes (100x syscall reduction) |
| SQPOLL mode | No | Yes (zero syscall I/O) |
| Syscalls per op | ~1-2 | Amortized 0.01 |
Design Principles
- Zero-allocation hot path: Pre-allocated completion ring buffer, cached ByteBuffer
views via FFM's
MemorySegment.asByteBuffer() - FFM-first: Works directly with MemorySegment, avoiding byte[] copies
- Thread-safe: Atomic state management, lock-free completion ring
- API consistent: Same interface as IoUringBackend for seamless fallback
When to Use NIO
- Cross-platform compatibility is required
- Running on non-Linux systems (macOS, Windows)
- io_uring is unavailable (older kernel or missing liburing)
- Development/testing on local machines
Architecture
┌─────────────────────────────────────────────────────────┐ │ NioBackend │ ├─────────────────────────────────────────────────────────┤ │ │ │ ┌─────────────┐ ┌─────────────┐ │ │ │ Completion │ │ Selector │ │ │ │ Ring Buffer │ │ (epoll/ │ │ │ │ (immediate) │ │ kqueue) │ │ │ └─────────────┘ └─────────────┘ │ │ │ │ │ │ ▼ ▼ │ │ ┌─────────────────────────────────────┐ │ │ │ poll() / waitFor() │ │ │ │ Delivers completions to handler │ │ │ └─────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────┘
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumConnection state for NIO backend. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaccept(long token) Accepts an incoming connection (server mode).voidbind(SocketAddress localAddress) Binds to a local address for accepting connections (server mode).voidclose()Closes the backend and releases all resources.voidconnect(SocketAddress remoteAddress, long token) Connects to a remote endpoint asynchronously.createFromAccepted(int handle) Creates a new backend instance from an accepted connection handle.Returns the backend type identifier.Gets the current connection state.getStats()Returns statistics about backend operations.voidinitialize(TransportConfig config) Initializes the backend with the given configuration.intpoll(CompletionHandler handler) Polls for completions without blocking.voidreceive(RegisteredBuffer buffer, long token) Receives data into a registered buffer (zero-copy).voidreceive(MemorySegment data, int maxLength, long token) Receives data into a raw memory segment.voidregisterBufferPool(RegisteredBufferPool bufferPool) Registers a buffer pool with this backend for zero-copy I/O.voidsend(RegisteredBuffer buffer, long token) Sends data using a registered buffer (zero-copy).voidsend(MemorySegment data, int length, long token) Sends data using a raw memory segment.intSubmits all queued operations in a batch.booleanReturns whether this backend supports batch submission.booleanReturns whether this backend supports registered buffers.booleanReturns whether this backend supports TLS/SSL.intwaitForCompletion(long timeoutMillis, CompletionHandler handler) Waits for at least one operation to complete.
-
Constructor Details
-
NioBackend
public NioBackend()
-
-
Method Details
-
initialize
Description copied from interface:TransportBackendInitializes the backend with the given configuration.This is called once during transport creation. Implementations should allocate resources, initialize the I/O subsystem, and prepare for operations.
- Specified by:
initializein interfaceTransportBackend- Parameters:
config- the transport configuration
-
registerBufferPool
Description copied from interface:TransportBackendRegisters a buffer pool with this backend for zero-copy I/O.For io_uring, this calls
io_uring_register_buffers()to pre-register memory regions with the kernel. For other backends, this may be a no-op.- Specified by:
registerBufferPoolin interfaceTransportBackend- Parameters:
bufferPool- the buffer pool to register
-
connect
Description copied from interface:TransportBackendConnects to a remote endpoint asynchronously.- Specified by:
connectin interfaceTransportBackend- Parameters:
remoteAddress- the remote address to connect totoken- the token identifying the operation
-
bind
Description copied from interface:TransportBackendBinds to a local address for accepting connections (server mode).- Specified by:
bindin interfaceTransportBackend- Parameters:
localAddress- the local address to bind to
-
accept
public void accept(long token) Description copied from interface:TransportBackendAccepts an incoming connection (server mode).- Specified by:
acceptin interfaceTransportBackend- Parameters:
token- the token identifying the operation
-
send
Description copied from interface:TransportBackendSends data using a registered buffer (zero-copy).For io_uring, this uses
io_uring_prep_send_fixed()with the buffer index. For other backends, this may fall back to a regular send.- Specified by:
sendin interfaceTransportBackend- Parameters:
buffer- the registered buffer containing data to sendtoken- the token identifying the operation
-
send
Description copied from interface:TransportBackendSends data using a raw memory segment.This is a fallback for non-registered buffers. Less efficient than
TransportBackend.send(RegisteredBuffer, long)but more flexible.- Specified by:
sendin interfaceTransportBackend- Parameters:
data- the memory segment containing data to sendlength- the number of bytes to sendtoken- the token identifying the operation
-
receive
Description copied from interface:TransportBackendReceives data into a registered buffer (zero-copy).For io_uring, this uses
io_uring_prep_recv_fixed()with the buffer index.- Specified by:
receivein interfaceTransportBackend- Parameters:
buffer- the registered buffer to receive intotoken- the token identifying the operation
-
receive
Description copied from interface:TransportBackendReceives data into a raw memory segment.- Specified by:
receivein interfaceTransportBackend- Parameters:
data- the memory segment to receive intomaxLength- the maximum number of bytes to receivetoken- the token identifying the operation
-
submitBatch
public int submitBatch()Description copied from interface:TransportBackendSubmits all queued operations in a batch.For io_uring, this calls
io_uring_submit()to submit all queued operations with a single syscall. This is the key to achieving 100x syscall reduction.For NIO, this is typically a no-op since NIO doesn't support batching.
- Specified by:
submitBatchin interfaceTransportBackend- Returns:
- the number of operations submitted
-
poll
Description copied from interface:TransportBackendPolls for completions without blocking.For io_uring, this calls
io_uring_peek_cqe()to check for completed operations. For NIO, this may use a Selector with 0 timeout.- Specified by:
pollin interfaceTransportBackend- Parameters:
handler- the handler to call for each completion- Returns:
- the number of operations completed
-
waitForCompletion
Description copied from interface:TransportBackendWaits for at least one operation to complete.This blocks until at least one queued operation completes.
- Specified by:
waitForCompletionin interfaceTransportBackend- Parameters:
timeoutMillis- the maximum time to wait in milliseconds (0 = forever)handler- the handler to call for each completion- Returns:
- the number of operations completed
-
createFromAccepted
Description copied from interface:TransportBackendCreates a new backend instance from an accepted connection handle.For io_uring, the handle is the file descriptor. For NIO, the handle is an index or reference to the accepted channel.
- Specified by:
createFromAcceptedin interfaceTransportBackend- Parameters:
handle- the handle returned by the accept completion- Returns:
- a new TransportBackend for the accepted connection
-
supportsRegisteredBuffers
public boolean supportsRegisteredBuffers()Description copied from interface:TransportBackendReturns whether this backend supports registered buffers.- Specified by:
supportsRegisteredBuffersin interfaceTransportBackend- Returns:
- true if registered buffers are supported
-
supportsBatchSubmission
public boolean supportsBatchSubmission()Description copied from interface:TransportBackendReturns whether this backend supports batch submission.- Specified by:
supportsBatchSubmissionin interfaceTransportBackend- Returns:
- true if batch submission is supported
-
supportsTLS
public boolean supportsTLS()Description copied from interface:TransportBackendReturns whether this backend supports TLS/SSL.- Specified by:
supportsTLSin interfaceTransportBackend- Returns:
- true if TLS is supported
-
getBackendType
Description copied from interface:TransportBackendReturns the backend type identifier.- Specified by:
getBackendTypein interfaceTransportBackend- Returns:
- the backend type (e.g., "io_uring", "nio", "xdp")
-
getStats
Description copied from interface:TransportBackendReturns statistics about backend operations.- Specified by:
getStatsin interfaceTransportBackend- Returns:
- backend statistics including operation counts, errors, etc.
-
close
public void close()Description copied from interface:TransportBackendCloses the backend and releases all resources.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceTransportBackend
-
getConnectionState
Gets the current connection state.- Returns:
- the connection state
-