Kest

Search

Search the documentation

Microservices

Delivery and dead letters

What each transport promises, what a handler can see, and how failures are retried.

Every message reaches a handler through a transport, and a transport decides how much it promises. Kest makes the promise explicit in the code: a transport acknowledges a message after the handler has finished and its answer has been published, and a message whose handler failed for good is rejected, not dropped.

TransportDeliveryWhen a message is acknowledged
TCP, Redis, NATS, MQTTat most once, or at least once where the broker keeps the messagenothing to acknowledge in the protocol; a reply is the answer for requests
Kafkaat least oncethe offset is committed after the handler finished. A failed event goes to the dead-letter topic <topic>.dlq, then its offset is committed
RabbitMQat least oncebasicAck after the handler and the reply. A failed message is rejected: to the dead-letter exchange if the queue has one, else dropped

Requests are always answered: a handler that throws answers the caller with an error (status 500, or the status of an HttpException or RpcException). Only events are retried, because nobody is waiting for them.

val options = KafkaOptions(brokers = "kafka:9092", groupId = "billing",
    handlerRetry = HandlerRetry(attempts = 3, delay = Duration.ofSeconds(2)))   // an event is tried three times, two seconds apart
// RabbitMQ: failed messages are dead-lettered, not requeued (requeueing a poison message loops forever)
val options = RmqOptions("amqp://guest:guest@rabbit:5672", queue = "billing", deadLetterExchange = "billing.dlx")

What a handler can see

Add a parameter of a context type and the transport gives it to you. It is matched by type, so you do not name anything:

TransportContextWhat it tells you
KafkaKafkaContexttopic, partition, offset, key, headers
RabbitMQRmqContextqueue, delivery tag, whether it was redelivered (after a crash), message id
NATSNatsContextsubject, queue group
MQTTMqttContexttopic, QoS, retained
RedisRedisContextchannel, reply channel
anyMessageContextthe pattern and the transport name
@EventPattern("order.placed")
fun onPlaced(order: OrderPlaced, ctx: KafkaContext) {
    audit.record(order.id, source = "${ctx.topic}/${ctx.partition}@${ctx.offset}")
}
 
@MessagePattern("sum")
fun sum(@Payload("items") items: List<Int>, @Ctx ctx: MessageContext): Int = items.sum()     // @Payload takes one property of the payload

Object patterns

A pattern can be an object, as in NestJS: @MessagePattern("""{"cmd":"sum"}"""). It is matched by content, so {"cmd":"sum","v":1} and {"v":1,"cmd":"sum"} are the same pattern. Clients send proxy.send(mapOf("cmd" to "sum"), data).

Errors

RpcException(message, status, error) answers a caller with a status and an error object, which comes back as sent: RpcException("quantity must be positive", 422, mapOf("field" to "quantity")). A timeout on the caller's side is RpcException with status 408. A connection that drops during a request fails that request with 503, and the next call reconnects.

Reconnecting

TcpOptions, KafkaOptions, RmqOptions, NatsOptions and MqttOptions take retryAttempts and retryDelay for the first connection, so a service can start before its broker. The TCP client reconnects on the next call after a dropped connection. Redis and MQTT reconnect by themselves, and NATS does too.

Not yet

  • A message that arrives while no instance of a service is listening is lost on NATS and Redis, which do not store messages. Use RabbitMQ or Kafka for that.
  • Kafka and NATS are tested by the contract suite when their brokers are running; the RabbitMQ and MQTT delivery rules are tested against real brokers.

Read more