Skip to main content
This recipe condenses Recipe F and overlaps Microservices—use both.

NestFactory helpers

All decode the same WireRequest JSON (wire).

NATS listener + Docker

Client caller (ClientProxy)

Redis listener + emit

RabbitMQ

ClientsModule (HTTP app calling brokers)

Merge ClientsModule::register(&[ ClientConfig::nats(...), ClientConfig::redis(...) ]) as a DynamicModule via NestFactory::create_with_modules—see dynamic_modules.rs.

Kafka advanced

There is no NestFactory::create_microservice_kafka yet—run KafkaMicroserviceServer::new with handler vec mirrored from NestFactory::create_microservice internals, or use ClientConfig::kafka for outbound send/emit.

Production broker architecture

Naming and isolation

End-to-end example: checkout API + brokered backends

This is a realistic topology for production:
  • checkout-api is public HTTP.
  • orders-rpc handles synchronous order creation.
  • audit-worker receives fire-and-forget events.
  • notifications-worker fans out emails and webhooks.
This split is useful when one call needs a reply (send("orders.create")) but side effects should stay asynchronous (emit("audit.order.created")).

NATS (cloud-native RPC and fan-out)

  • Core NATS is fast and ephemeral—if the subscriber is offline, messages are gone.
  • JetStream adds persistence, replay, and consumer groups—use it for integration events you must not lose (OrderPlaced, audit trail).
  • Run 3+ server clusters in prod; pin clients to nats:// or tls:// URLs from secrets managers, not baked into images.

Redis (low-latency work queues + cache bus)

  • Lists / streams back create_microservice_redis workloads; tune memory eviction so RPC metadata is not evicted under load.
  • Separate cache Redis from queue Redis when traffic mixes—noisy neighbors cause tail latency on send.

RabbitMQ (managed queues, DLQ)

Production checklist:
  • Quorum queues for HA (RabbitMQ 3.8+) instead of classic mirrored queues.
  • Dead-letter exchange (DLX) bound to a DLQ queue—failed users.rpc messages land there for replay after fixing bugs.
  • prefetch per consumer ≈ concurrent in-flight handlers; tune so workers stay busy without overflowing memory.
For a payment or KYC workflow, RabbitMQ is a strong fit when you need controlled retries and a visible queue of stuck jobs for operators.

Kafka (streaming, replay, ordering)

  • Put partition key = order_id (or tenant_id) so related events stay ordered per aggregate.
  • One consumer group per deploying service (billing-worker-v3); scale consumers ≤ partition count for strict ordering per key.
  • Enable TLS + SASL via KafkaConnectionOptions / KafkaSaslOptions to match MSK / Confluent Cloud.
For analytics, billing ledgers, and audit trails, Kafka is usually the better fit than Redis or plain NATS because you can replay history into a new consumer.

TLS, secrets, and health

Store NATS_URL, REDIS_URL, AMQP_URL, KAFKA_BOOTSTRAP in Vault / Kubernetes Secrets—rotate without redeploying app code when your platform supports hot reload. Combine broker-specific HealthIndicator stubs (NatsBrokerHealth, RedisBrokerHealth, kafka_cluster_reachable_with) with enable_readiness_check so orchestrators drain pods before broker outages take down user traffic.
WireRequest JSON is identical across transports—integration tests can swap TCPNATS without rewriting handler logic (wire).