Hierarchical Topics #663
shortishly
announced in
Announcements
Replies: 1 comment
|
Definitely a good idea/addition, I started to think, in vanilla Kafka segments could be keyed, but this might explode metadata on ram |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hierarchical Topics in Tansu
Kafka topics are great at fan-out: produce once, consume many times from any offset. But one thing they do not give you for free is per-key subscriptions. If you are streaming telemetry from a fleet of vehicles — each identified by its license plate — and a consumer only cares about one vehicle, it has to read the whole topic and discard everything that does not match. Hierarchical topics let you skip that work entirely.
The problem
Consider a
telemetrytopic where every message is keyed by the vehicle's license plate:{"key": "CC54 RYD", "value": {"latitude": 52.79954588398599, "longitude": -4.097797077141778, "altitude": 163.17052057678418}}{"key": "NN03 RYB", "value": {"latitude": 50.84287000432428, "longitude": -4.25542715891738, "altitude": 41.08461912439403}}A dashboard that tracks
CC54 RYDalone still has to page through everyNN03 RYB,AB12 XYZ, and every other plate in the fleet. For a large fleet with high message rates that is a lot of wasted I/O.Hierarchical Topics
Tansu introduces hierarchical topics — a way to address the subset of a topic that belongs to a single key using a derived topic name of the form
topic/key:telemetrytelemetry/CC54 RYDCC54 RYDtelemetry/NN03 RYBNN03 RYBThe hierarchical topic is not a real topic. No data is duplicated, no additional partitions are created. The SQL-based storage backends (PostgreSQL and libSQL) add a single
AND r.k = $7predicate to the existing fetch query, so the database does the filtering at the source.Enabling the feature
Hierarchical topics are opt-in per topic. Set the
tansu.virtualconfiguration key totruewhen creating the topic:Once the flag is set, any consumer can open a fetch against
telemetry/CC54 RYDusing the standard Kafka protocol — no client-side changes required.What happens without the flag
Topics that do not carry
tansu.virtual=trueare completely unaffected. A topic name that happens to contain a/is treated as a literal name; the slash carries no special meaning. This means existing topics are safe, and you can still create topics nameda/bif your naming convention requires it — they will simply stream all their records when fetched.How it works
On the PostgreSQL backend the fetch query is extended with an extra predicate when a virtual key is detected:
The libSQL backend uses an identical extension to its own fetch query.
At request time the broker:
/.describe_configon the base topic to check fortansu.virtual=true.as
$7and resolves offsets and watermarks against the base topic.unchanged — business as usual.
The
O(1)config lookup is cached at the storage layer, so the overhead on each fetch is negligible.Offset semantics
Offsets in a hierarchical topic are the same offsets as in the base topic. This is intentional: if you need to re-read from a known point you use the offset you last committed against the base topic. Compaction, retention, and watermarks are all governed by the base topic's configuration.
Example: vehicle telemetry dashboard
Produce telemetry as usual:
A consumer dashboard for a single vehicle subscribes to the hierarchical topic:
kafka-console-consumer.sh \ --topic telemetry/CC54\ RYD \ --from-beginning \ --bootstrap-server localhost:9092It receives only the
CC54 RYDrecords, with zero application-level filtering and no impact on consumers of the basetelemetrytopic.Availability
Hierarchical topics are supported on Tansu's SQL-backed storage engines:
postgres://)sqlite://)The in-memory and S3/object-store backends are unaffected; they continue to behave as standard Kafka-compatible topics.
The feature shipped in tansu-io/tansu#651, currently available on
main. Planned to be part of the0.7.0release.All reactions