Show / Hide Table of Contents

    Tombstone Message

    A tombstone message is a message with a null body, used to indicate that the record has been deleted. This technique is for example used with Kafka topics compaction, to get rid of obsolete records.

    Consumer

    Silverback maps by default the messages with a null body to a Tombstone or Tombstone<TMessage>. This behavior can be changed using the SkipNullMessages or UseLegacyNullMessageHandling of the IConsumerEndpointBuilder<TBuilder>, or setting the NullMessageHandlingStrategy property of the ConsumerEndpoint).

    The Tombstone/Tombstone<TMessage> message exposes a single property containing the message identifier.

    • EndpointConfigurator
    • Subscriber
    public class MyEndpointsConfigurator : IEndpointsConfigurator
    {
        public void Configure(IEndpointsConfigurationBuilder builder) =>
            builder
                .AddKafkaEndpoints(endpoints => endpoints
                    .Configure(config => 
                        {
                            config.BootstrapServers = "PLAINTEXT://kafka:9092"; 
                        })
                    .AddInbound(endpoint => endpoint
                        .ConsumeFrom("catalog-events")
                        .Configure(config => 
                            {
                                config.GroupId = "my-consumer";
                            })
                        .DeserializeJson(serializer => serializer
                            .UseFixedType<Product>())));
    }
    
    public class MySubscriber
    {
        public async Task OnProductDeleted(Tombstone<Product> tombstone)
        {
            // TODO: use tombstone.MessageId to remove the product 
            // from the local database
        }
    }
    
    Important

    In order to create a typed Tombstone<TMessage> it is required that either the consumed message declares the x-message-type header or a fixed type deserializer is used (as shown in the example above). Otherwise the null message will be mapped to a simple Tombstone.

    Producer

    A Tombstone<TMessage> (or Tombstone) can also be used to produce a null message.

    • EndpointConfigurator
    • Publisher
    public class MyEndpointsConfigurator : IEndpointsConfigurator
    {
        public void Configure(IEndpointsConfigurationBuilder builder) =>
            builder
                .AddKafkaEndpoints(endpoints => endpoints
                    .Configure(config => 
                        {
                            config.BootstrapServers = "PLAINTEXT://kafka:9092"; 
                        })
                    .AddOutbound<Product>(endpoint => endpoint
                        .ProduceTo("catalog-events")));
    }
    
    public class MyService
    {
        private readonly IPublisher _publisher;
        
        public MyService(IPublisher publisher)
        {
            _publisher = publisher;
        }
        
        public async Task DeleteProduct(string productId)
        {
            ...
            
            await _publisher.PublishAsync(new Tombstone<Product>(productId));
        }
    }
    
    Note

    The Tombstone<TMessage> messages are routed according to the type parameter TMessage. This means that they will be published to the outbound endpoints papped to the same TMessage (Product in the above example), as well as to the outbound endpoints explicitly mapping Tombstone.

    • Improve this doc
    GitHub E-Mail
    ↑ Back to top © 2020 Sergio Aquilini