AlgoViz

Apache Pulsar

medium 28 min

Apache Pulsar separates serving from storing: brokers talk to clients and keep no data of their own, Apache BookKeeper stores each topic as many small replicated segments.

Video lesson · Chapter 1 of 5

An order finds its broker and its journal

0:00 / 3:20

Brokers and bookies in 75 seconds

1:15Reel coming soon

Theory

Read or listen first. About 2 min.

What it is

Pulsar is an open-source system for messaging and event streaming. Producers publish to a topic, and each subscription on the topic gets every message. How a subscription shares messages among its consumers is a choice: one reader, one active reader with stand-bys, everybody in turn, or by key.

Its special idea is the split into layers. A broker accepts and delivers messages. A bookie, a storage server of Apache BookKeeper, keeps them. A topic is a chain of segments called ledgers, and every ledger is written to a few bookies. A metadata store remembers who owns what.

Pulsar was built for many teams on one cluster. Names have a tenant and a namespace, as in persistent://shop/orders/created, and policies such as retention, quotas and access hang on them.

Why it exists

In a log where the broker also owns the disk, moving a partition means copying its data. If serving and storing are separate, a topic can move to another broker in a moment, because there is nothing to copy. New storage servers take new segments at once.

A second reason is one system for two shapes of work: task queues with individual acknowledgements and ordered streams with replay. General ideas are on queues, log-based queues and pub-sub. This page is about the product.

The cost is a larger system. A production cluster has brokers, bookies and a metadata store, each with its own way to fail and to be upgraded. Many teams do not need that.

A short history

  1. 2015

    Yahoo deploys its first Pulsar instance in the second quarter, according to its case study on the project's site.

  2. June 2017

    Pulsar enters the Apache Incubator.

  3. September 2018

    Pulsar graduates to a top-level Apache project.

  4. May 2023

    Pulsar 3.0 is the first release of the long-term support model.

  5. October 2024

    Pulsar 4.0 (LTS) reworks Key_Shared delivery to keep order when consumers come and go.

  6. October 2026

    Pulsar 5.0 (LTS) brings scalable topics and a new client API, first in Java.

Words to know

Broker
Serves topics to clients. Keeps a cache but no durable data.
Bookie
A BookKeeper storage server. It stores parts of many ledgers.
Ledger
An append-only segment with one writer. Once closed it never changes.
Managed ledger
The chain of ledgers that makes one topic.
Cursor
The stored position of a subscription, with its single acknowledgements.
Subscription
A named reader group with a delivery type.
Bundle
A range of topic hashes in a namespace, the unit that moves between brokers.
Namespace
A group of topics inside a tenant that shares policies.

How it works

Step through the idea, or press Play.

Step 1 of 3

Publish. The owner broker appends the message to bookies

Understand it

Start here

The radio desk and the vault

Imagine a radio station. Announcers sit at desks and speak, but they keep nothing at the desk. Every sentence is recorded on two tapes in a separate vault before it counts as said. When an announcer leaves, another desk takes the channel. No tape has to move.

Speak here, store there
Channel AChannel BVault, channel A11Two tapes before on air

The desk can change in a moment, because the memory is in the vault.

What you get

Why teams choose it

  • Serve and store apart

    Add brokers for traffic and bookies for space, each on its own.

  • Queue and stream

    Four subscription types on the same topic.

  • Tenants

    Tenants and namespaces with quotas, policies and access rules.

  • Many topics

    The project claims over a million topics in a cluster.

  • Geo-replication

    Built in, between clusters, per namespace or topic.

  • Tiered storage

    Closed ledgers can move to object storage and stay readable.

Architecture

Brokers, bookies, metadata

A Pulsar cluster

A producer service publishes to broker 1 in the Pulsar cluster, which writes each entry to bookies 1 and 2. A consumer service receives from broker 2 and acknowledges there. Broker 2 reads from bookie 3 and asks the metadata store who owns a topic.
Every thick box is one machine. A topic has one owner broker at a time, and its ledgers are spread over bookies.

The parts

PartJobIf one dies
BrokerLookup, publish, dispatch, cacheIts bundles move to other brokers
BookieJournal and entries of ledgersWriters replace it, lost copies are rebuilt
Metadata storeOwners, ledger lists, policiesNeeds a quorum. Without it no new owner can be chosen
Proxy (optional)One address in front of all brokersUse several
Configuration storeTenants and namespaces across clustersPolicy changes wait
The 5.0 documentation recommends Oxia as the metadata store for new clusters. ZooKeeper is production-ready and ships with Pulsar. RocksDB is for standalone use.

One publish

A bookie writes a journal first. Use separate disks for the journal and for the entries, so reads do not slow down writes.

The key idea

A topic is a chain of ledgers

A partition in a broker-owned log lives on a fixed set of machines. A Pulsar topic is cut into ledgers, and each ledger may choose other bookies. Three numbers describe a ledger.

NumberMeaningDefault in broker.conf
Ensemble, EBookies that share the ledger2
Write quorum, QwCopies of each entry2
Ack quorum, QaCopies on disk before the confirmation2
E must be at least Qw, and Qw at least Qa. A common production choice is 3, 3, 2: three copies, and one slow bookie does not delay the writer.

Where entry i is stored

bookies={(i+j) mod E;:;0≤j<Qw}\text{bookies} = \{(i + j) \bmod E ;:; 0 \le j < Q_w\}
With E=4E = 4 and Qw=2Q_w = 2, entry 0 goes to bookies 0 and 1, entry 1 to 1 and 2, entry 3 to 3 and 0. This is striping. A confirmed entry survives the loss of Qa−1Q_a - 1 bookies.

How much disk?

30,000 msgs×1 KB×86,400 s×2≈5.2 TB per day30{,}000\ \tfrac{msg}{s} \times 1\,\text{KB} \times 86{,}400\ \text{s} \times 2 \approx 5.2\ \text{TB per day}
Assume 10,000 messages per second on average, so 30,000 at peak, 1 KB each, and two copies. At the average rate it is 1.7 TB per day. Three copies cost half as much again.

A broker fails

Fencing stops an old owner that is only slow: bookies refuse its writes, so it can no longer reach its ack quorum. No data is copied.

What the split makes easy

  • Moving a topic is a change of owner
  • New bookies take new ledgers at once
  • A topic is not limited by one disk
  • Closed ledgers can be offloaded

What it costs

  • An extra network hop on a write
  • Three kinds of servers
  • More metadata to keep
  • Reads of old data go broker to bookie

Reading

Four subscription types

TypeWho gets a messageOrderCumulative ack
Exclusive (default)The only consumerYesYes
FailoverThe active consumer, per partitionYes, duplicates possible at a switchYes
SharedAny consumer, in turnNoNo
Key_SharedThe consumer that owns the keyPer keyNo
Several subscriptions on one topic are independent. Each one gets all messages, which is pub-sub.

Queue and stream on one topic

Mail needs speed, the ledger needs order. Both read the same messages at their own pace.

Acknowledge, refuse, retry

  1. 1

    Ack one by one

    In Shared and Key_Shared each message is acknowledged alone. The cursor stores the holes.

  2. 2

    Negative ack

    Asks for redelivery, after 1 minute by default in the Java client. The redelivery counter lives in memory and can be reset, for example by a broker restart.

  3. 3

    Ack timeout

    Off by default. Without it a message comes back only when the consumer disconnects or refuses it.

  4. 4

    Retry topic

    reconsumeLater with enableRetry stores the count with the message. The documentation calls it the only way that guarantees your maximum is honoured and the dead letter topic is reached.

  5. 5

    Dead letter topic

    For Shared and Key_Shared. Create a subscription on it, or the dead letters may be lost.

Keeping data

Retention, TTL, quota: three different things

SettingQuestion it answersDefault
RetentionHow long do acknowledged messages stay?0: not kept
Message TTLWhen is an unacknowledged message given up?0: never
Backlog quotaHow large may the backlog be?No limit
Quota policyWhat happens at the limit?Producers are held
Maximum message sizeHow large may one message be?5 MB, chunking above that
Set them per namespace or per topic. Retention must be set for both size and time.

When may a ledger go?

Space is freed per ledger, not per message. One unacknowledged message holds a ledger. After an offload the local copy stays 4 hours by default.

Compaction is a separate tool. It keeps the latest message per key in a compacted copy, for readers that want a current table instead of the history.

Scaling

Partitions, bundles and scalable topics

  1. 1

    Bundles

    Topics of a namespace are hashed into bundles, 32 by default since 5.0. Bundles are assigned to brokers and can be split and moved.

  2. 2

    Partitioned topics

    A topic with a fixed number of parts, for more throughput than one broker gives. The number can grow but never shrink, and growing can send a key to another partition and break its order.

  3. 3

    Scalable topics (5.0)

    Addressed as topic://. The key space is cut into segments that the broker splits when hot and merges when cold. Ordered consumers read the old segment to its end before the new one.

Partitioned or scalable?

Partitioned topicScalable topic
Available inAll versions5.0 and later, on by default
CapacityYou choose and raise itSplits and merges by itself
ShrinkNot possibleYes
Key order when resizedCan breakKept for stream consumers
ClientsAllv5 API, currently the Java client
Geo-replicationYesNot supported
ConsumersFour subscription typesStream, queue, checkpoint
The documentation recommends scalable topics for new applications that fit these limits and fully supports the older kind. One hot key cannot be split by either.

Guarantees

Duplicates, transactions, regions

  1. 1

    At least once

    The normal case for persistent topics with acknowledgements. Make consumers idempotent.

  2. 2

    Producer deduplication

    Off by default. The broker drops a repeated sequence id of a producer, per topic partition.

  3. 3

    Transactions

    Consume, process and produce as one unit across topics. The coordinator is off by default (transactionCoordinatorEnabled). The documentation lists Java, Go and .NET clients.

  4. 4

    Non-persistent topics

    Messages are not stored. Fast, and lost when nobody is listening.

  5. 5

    Delayed messages

    A message can be delivered later. Only Shared and Key_Shared subscriptions respect the delay.

Asynchronous geo-replication

A producer publishes to the broker in Europe, which stores the message on bookies in Europe and later copies it to the broker in America, which stores it on bookies there.
Each region confirms on its own. If Europe is lost, messages that were not copied yet are missing in America.

Operations

Running it

Shell
# a tenant, a namespace and its policiesbin/pulsar-admin tenants create shop --allowed-clusters eubin/pulsar-admin namespaces create shop/orders --bundles 64bin/pulsar-admin namespaces set-retention shop/orders --size 100G --time 7dbin/pulsar-admin namespaces set-message-ttl shop/orders --messageTTL 1209600bin/pulsar-admin namespaces set-backlog-quota shop/orders \  --limit 50G --policy producer_request_hold# a partitioned topic, then look at itbin/pulsar-admin topics create-partitioned-topic \  persistent://shop/orders/created --partitions 8bin/pulsar-admin topics partitioned-stats persistent://shop/orders/createdbin/pulsar-admin topics subscriptions persistent://shop/orders/created
Time to live is in seconds: 1,209,600 is 14 days.
properties
# broker.conf, a common choice for three or more bookiesmanagedLedgerDefaultEnsembleSize=3managedLedgerDefaultWriteQuorum=3managedLedgerDefaultAckQuorum=2# on by default in 5.0, off means: no topic:// at allscalableTopicsEnabled=true# off by default, needed for transactionstransactionCoordinatorEnabled=true
The first three are per namespace settings too, through a persistence policy.

Red flags

You seeCauseRemedy
Disk grows, nobody readsA forgotten subscriptionDelete it, set TTL and a quota
Producers hangBacklog quota with holdFix the consumer, raise or change the policy
A consumer of a Shared subscription gets nothing moreToo many unacknowledged messagesAcknowledge or refuse each message
Failing messages never reach the dead letter topicCounter reset with negative acksreconsumeLater with enableRetry
One broker is hotA large bundle or a hot topicSplit the bundle, partition the topic
Write latency jumpsSlow journal disk on a bookieSeparate disks, ack quorum below write quorum

Before production

  1. 1

    Choose a version with a future

    5.0 and 4.0 are long-term support releases. 5.0 has active support until October 2028. Active support of 4.0 ended in October 2026, with security fixes until October 2027.

  2. 2

    Spread bookies

    Put them in three zones and make placement aware of zones.

  3. 3

    Create subscriptions first

    Before the first message, or set retention.

  4. 4

    Watch

    Backlog per subscription, publish latency, bookie disk and journal, ledger under-replication.

  5. 5

    Check the ecosystem

    The open-source Kafka-on-Pulsar repository was archived in November 2026. Do not plan a migration on it.

Choose

When Pulsar, and when not

Choose Pulsar

  • Many teams or customers on one platform
  • Work queues and ordered streams of the same events
  • Very many topics
  • Long retention with cheap storage
  • Replication between regions out of the box

Choose something else

  • A small team with one queue: SQS or RabbitMQ
  • The Kafka ecosystem is the reason: Kafka
  • No one to run three layers: a managed service
  • Only routing inside one data center: RabbitMQ
  • Heavy stream computation: Flink, with any log

Pulsar and Kafka

PulsarKafka
Who storesBookies, per segmentBrokers, per partition
Adding storageNew ledgers use it at oncePartitions are reassigned
Queue styleShared, single acksConsumer groups, share groups in newer versions
CoordinationOxia or ZooKeeperKRaft inside Kafka
TenantsBuilt into names and policiesBy convention, quotas and ACLs
EcosystemSmallerMuch larger
Both have tiered storage. Details of the other side: Apache Kafka.

Summary

The whole topic in one minute.

A broker owns a topic for a while, appends each message to several bookies and answers when enough of them have it on disk. If the broker dies, another one takes the topic, closes the last ledger safely and opens a new one. No message is copied for that. Adding a bookie helps at once, because new ledgers use it.

Subscriptions decide the shape: Exclusive and Failover keep order, Shared spreads work, Key_Shared keeps order per key. Messages are kept until every subscription has acknowledged them, and then deleted unless you set retention. Choose Pulsar for many tenants, queue plus stream, or geo-replication, when you can operate three kinds of servers.

  • Brokers serve, bookies store, the metadata store remembers owners.
  • A topic is a chain of ledgers, each on its own set of bookies.
  • A broker failure changes the owner. No data moves.
  • Exclusive and Failover keep order, Shared spreads, Key_Shared keeps order per key.
  • No subscription keeps nothing. A forgotten one keeps everything.
  • Scalable topics (5.0) split and merge by themselves, with limits.

Sources