Pulsar Academy

Pulsar Message Retention Tutorial

Learn how message retention works in Apache Pulsar.

Introduction

Message retention allows you to keep messages in Pulsar around even after they have been acknowledged. By default, messages are only kept as long as they are in at least one backlog. To store messages that are not in any backlog, or have already been acknowledged by all active subscriptions, you can use retention policies. Retention policies can be set based on time and size. Before you run the commands below, make sure you have started Pulsar.

How to set namespace message retention using the Pulsar CLI?

To set a message retention policy for all topics in a Pulsar namespace, we need to provide the mandatory parameters:

  • Tenant name & Namespace name
  • Size limit
  • Time limit
./bin/pulsar-admin namespaces set-retention my-tenant/my-namespace --size 500MB --time 12h

How to set topic message retention using the Pulsar CLI?

To set a message retention policy for a Pulsar topic, we need to provide the mandatory parameters:

  • Persistence & Tenant name & Namespace name & Topic name
  • Size limit
  • Time limit
./bin/pulsar-admin topicPolicies set-retention persistent://my-tenant/my-namespace/my-topic --size 500MB --time 12h

How to get namespace message retention using the Pulsar CLI?

To get the message retention policy for all topics in a Pulsar namespace, we need to provide the mandatory parameters:

  • Tenant name & Namespace name
./bin/pulsar-admin namespaces get-retention my-tenant/my-namespace

How to get topic message retention using the Pulsar CLI?

To get the message retention policy for a Pulsar topic, we need to provide the mandatory parameters:

  • Persistence & Tenant name & Namespace name & Topic name
./bin/pulsar-admin topicPolicies get-retention persistent://my-tenant/my-namespace/my-topic --applied

How to disable message retention in Pulsar?

By default, message retention is already disabled. If you changed your configuration, you can reset it by setting both the time and size limit to zero:

# For namespace-level retention
./bin/pulsar-admin namespaces set-retention my-tenant/my-namespace --size 0 --time 0

# For topic-level retention
./bin/pulsar-admin topicPolicies set-retention persistent://my-tenant/my-namespace/my-topic --size 0 --time 0

Table of Contents

Let's stay in touch

Get notified of new developments or blogposts.
Checkmark icon
You've joined the mailing list!
Oops! Something went wrong while submitting the form.