Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

NATS Microservices

Commands subscribe through Core NATS queue groups. All replicas with the same service_name compete for each command. Events publish to JetStream and use a durable consumer per service, so every service group receives an event and one replica in each group handles it.

#![allow(unused)]
fn main() {
let options = NatsTransportOptions::new("nats://127.0.0.1:4222")
    .service_name("billing")
    .jetstream_stream("CAELIX_EVENTS")
    .dead_letter_subject("caelix.dead");
}

service_name is required when command handlers exist. jetstream_stream is required for event topology and names an existing stream whose subjects cover the event patterns.

Options

MethodDefaultMeaning
new(server)requiredNATS server URL
service_name(name)nonestable command queue group and event consumer identity
rpc_timeout(duration)5 scommand request deadline
jetstream_stream(name)nonedurable event stream
dead_letter_subject(subject)nonefinal-failure event destination
max_request_bytes(n)1 MiBencoded command envelope limit; minimum 1
max_response_bytes(n)1 MiBencoded response limit; minimum 256
max_event_bytes(n)1 MiBencoded event envelope limit; minimum 1
max_handler_concurrency(n)64simultaneous invocations per subscription; minimum 1
shutdown_timeout(duration)10 swait for transport tasks
max_event_deliveries(n)5attempt that dead-letters; minimum 1
event_retry_delay(duration)1 sdelayed NAK interval
event_ack_wait(duration)30 sunacknowledged redelivery wait; minimum 10 ms

Commands are ordinary NATS request/reply messages and require an active responder. Events are acknowledged only after a successful handler. Retryable failures are negatively acknowledged with the configured delay. At the delivery limit, a configured dead-letter subject receives the event and safe failure metadata; otherwise the transport terminates the retry cycle according to its consumer handling.

Keep the JetStream stream durable and size/age-limited according to broker operations. During shutdown, Caelix cancels intake, waits up to shutdown_timeout for tasks, then runs module shutdown hooks.