NestFactory helpers
| Transport | Feature | Bootstrap |
|---|---|---|
| TCP | microservices | NestFactory::create_microservice |
| NATS | microservices-nats | create_microservice_nats |
| Redis | microservices-redis | create_microservice_redis |
| RabbitMQ | microservices-rabbitmq | create_microservice_rabbitmq |
| gRPC JSON | microservices-grpc | create_microservice_grpc |
| Kafka | microservices-kafka | KafkaMicroserviceServer (manual wiring) |
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 noNestFactory::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
| Pattern | Example | Use |
|---|---|---|
| Subject / routing key | prod.billing.events.v1.order.placed | Environment + domain + version + event—humans can grep logs. |
| RPC queue | prod.users.rpc (RabbitMQ work queue) | Single consumer group processing send workloads. |
| Redis prefix | myapp:rpc: + handler id | RedisMicroserviceOptions::with_prefix avoids collisions when several systems share one cluster. |
End-to-end example: checkout API + brokered backends
This is a realistic topology for production:checkout-apiis public HTTP.orders-rpchandles synchronous order creation.audit-workerreceives fire-and-forget events.notifications-workerfans out emails and webhooks.
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://ortls://URLs from secrets managers, not baked into images.
Redis (low-latency work queues + cache bus)
- Lists / streams back
create_microservice_redisworkloads; 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.rpcmessages land there for replay after fixing bugs. prefetchper consumer ≈ concurrent in-flight handlers; tune so workers stay busy without overflowing memory.
Kafka (streaming, replay, ordering)
- Put partition key =
order_id(ortenant_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/KafkaSaslOptionsto match MSK / Confluent Cloud.
TLS, secrets, and health
StoreNATS_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 TCP ↔ NATS without rewriting handler logic (wire).