Nakadi Documentation

repository·master·Indexed 21 days ago

https://github.com/zalando/nakadi

Nakadi is a high-scalability distributed event bus broker providing a RESTful API abstraction over Kafka-like queues. It decouples microservices through reliable, schema-validated, and highly available data streams. Key features include a schema registry for data quality, a high-level subscription API for partition redistribution, OAuth2 security, and low-latency push-based consumption via streaming HTTP connections.

Tokens
16.7K
Snippets
40
Records
76
Agent score
75%

What's inside Nakadi

  1. What is Nakadi Event Broker?

    master

    Nakadi is a distributed event bus broker that provides a RESTful API abstraction over Kafka-like queues. It is designed to decouple microservices by allowing them to send, receive, and analyze streaming data in real time using reliable and highly available data streams.

    Key features include:

    • REST Abstraction: Decouples services from specific broker technologies (like Kafka).
    • Schema Registry: Ensures data quality by validating events against defined schemas.
    • High-Level Subscription API: Supports automatic partition redistribution and server-side cursor management via commits.
    • Security: Supports OAuth2 authentication and per-event type authorization.
    • Low Latency: Supports push-based consumption via streaming HTTP connections.
  2. Compare Nakadi to Apache Kafka

    master

    Nakadi leverages Apache Kafka as its internal broker but provides a high-level API designed for microservices integration.

    Key Differences:

    • Abstraction: Nakadi does not use 'Topics'. Instead, it uses Event Types (which define structure and ownership) and Streams.
    • Communication: Nakadi uses HTTP, allowing any service that speaks HTTP to participate without a shared technology dependency (like a specific Kafka client library).
    • Checkpointing: In Nakadi, consumers receive messages in batches, and each batch is checkpointed rather than individual messages.
    • Schema Validation: Nakadi includes an event type registry with JSON Schema validation. Unlike the Confluent ecosystem which is often optimized for Avro/Analytics, Nakadi is optimized for JSON-based microservices.
    • Inbuilt Event Types: Nakadi optionally supports predefined structures for business processes and data changes (providing primitives for identity, timestamps, and causality).
    • Operational Decoupling: Because Nakadi sits behind an API, upgrading the underlying Kafka infrastructure does not force a synchronized upgrade on all microservices.
  3. Compare Nakadi to Allegro Hermes

    master

    Both are API-based brokers built on Kafka, but they differ in delivery and schema support:

    • Delivery Mechanism: Hermes uses webhooks (callback URLs) to deliver messages. Nakadi maintains a streaming connection to push events.
    • Ordering: Nakadi guarantees delivery in arrival order for each partition (similar to Kafka). Hermes' ordering guarantees are not explicitly defined in its API.
    • Schema Support: Hermes supports both JSON Schema and Avro. Nakadi currently only supports JSON Schema.
    • Event Types: Nakadi provides inbuilt event types for business processes; Hermes does not.
    • Client Support: Hermes provides a Java client driver; Nakadi does not ship with a built-in client.
  4. Important caveats when repartitioning event types

    master

    When performing repartitioning in Nakadi, be aware of the following side effects and limitations:

    • Partition Mapping: If using a hash partition strategy, publishing events to the event type will change the partitions in which they previously appeared.
    • Ordering Guarantees: Nakadi guarantees ordering per partition per batch. Increasing the partition count from 1 to more will break the total order of events.
    • Directional Limit: Repartitioning only allows you to increase the number of partitions.
    • Subscription Impact: All consuming subscriptions are disconnected once the repartitioning process is finished.
  5. Configure Partitioning Strategies

    master

    Nakadi divides an event type's stream into one or more partitions. Each partition is a fully ordered log. There is no global ordering across different partitions.

    The partition_strategy determines how events are mapped to partitions. This is configured when creating the event type via the partition_strategy field:

    • random: (Default) The partition is selected randomly. This provides good load balancing for high-volume streams where order does not matter.
    • hash: The partition is selected by hashing the values of the fields defined in partition_key_fields. This ensures that events about the same logical entity (with the same key) are sent to the same partition. Use this when ordering matters.
    • user_defined: The producer sets the partition manually when sending an event. This is only available for business and data categories. Use this only if you have a specific requirement.

    Key Fields:

    • partition_strategy: The strategy to use (random, hash, or user_defined).
    • partition_key_fields: An array of field names used for the hash strategy.
  6. Compare Nakadi to Google Cloud Pub/Sub

    master

    Both Nakadi and Google Cloud Pub/Sub provide HTTP APIs suitable for microservices backplanes, but they differ in delivery and state management:

    • Acknowledgment Model: Pub/Sub allows acknowledging every message individually. Nakadi uses a logical log model where consumers checkpoint their position (typically per batch).
    • Subscription Management: Pub/Sub requires a subscription to be set up before consumption to manage delivery state. In Nakadi, consumers are currently expected to manage their own offsets.
    • Delivery Model: Pub/Sub uses a polling model (consumers request pages of messages). Nakadi maintains a streaming connection and pushes events to consumers as they arrive.
  7. Configure event ordering and partitioning

    master

    Nakadi publishes events in the order they appear in the posted array. It does not re-order events based on data properties.

    Partial Ordering (Per Entity)

    To ensure events for a specific entity (e.g., a specific user or order) are ordered, configure the event type with the hash partitioning strategy and specify the fields used to construct the key. This provides partial ordering for that key.

    Total Ordering

    Total ordering is generally not achievable unless the partition size is set to 1. However, setting partition size to 1 is discouraged as it limits scalability and can cause cluster hot spotting.

  8. How Nakadi Timelines and Zookeeper synchronization work

    master

    Nakadi uses Zookeeper to coordinate timeline creation and ensure all distributed Nakadi nodes are synchronized. This prevents race conditions when updating event type (ET) configurations and ensures that publishers are correctly blocked or released across the cluster.

    The Zookeeper Data Structure

    Nakadi maintains a specific hierarchy in Zookeeper to manage versioning and locking:

    • /nakadi/timelines/lock: A lock used to synchronize timeline version updates.
    • /nakadi/timelines/version: A monotonically incremented long value representing the current version of the timelines configuration.
    • /nakadi/timelines/locked_et/: Contains ephemeral nodes (e.g., et_1) used to lock specific event types during creation.
    • /nakadi/timelines/nodes/: Contains nodes for each Nakadi instance (e.g., node1, node2), where each node exposes its current local version.

    The Synchronization Lifecycle

    1. Locking: An instance acquires the /nakadi/timelines/lock and then creates an ephemeral node under /timelines/locked_et/{et_name} to lock the specific event type.
    2. Version Bump (Notification): The coordinator increments the /nakadi/timelines/version node. All Nakadi instances watch this node.
    3. Node Reaction (Barrier): Upon seeing the version change, every Nakadi instance updates its local state (e.g., blocking/releasing publishers) and then increments its own version in /nakadi/timelines/nodes/{node_id} to signal it has reacted.
    4. Execution: Once all nodes have signaled readiness, the coordinator performs database entries and storage snapshots.
    5. Cleanup: The coordinator removes the lock and bumps the global version again to notify nodes to finalize the change and clear local locks.
  9. Compare Nakadi to Amazon Kinesis

    master

    Nakadi and Kinesis are similar in using HTTP APIs and partition-based streams, but have distinct operational characteristics:

    • Checkpointing: Kinesis supports per-message checkpointing. Nakadi provides checkpointing information per batch of messages.
    • Partitioning: Kinesis allows setting a partition hash key directly. Nakadi computes the key based on the data.
    • Consumer Model: Kinesis uses a polling model via 'shard iterators', which is subject to rate limits (e.g., 5 transactions per second per shard). Nakadi uses a streaming connection.
    • Resumption: Kinesis offers various ways to resume from a position. Nakadi allows access only from the beginning or from a named offset.
    • Scalability: Kinesis allows resizing shards. In Nakadi, partition counts are fixed once set for an event type.
    • Retention: Kinesis messages are stored for a maximum of 7 days. Nakadi event expiration is configurable.
  10. Understand the Batch Response format

    master

    Nakadi groups events into batch responses. Each batch is emitted on a single line in the stream.

    A batch object contains:

    • cursor: An object describing the partition and the offset for this specific batch. This allows clients to checkpoint their position.
    • events: An array of event objects. These are ordered by arrival time. Note that Nakadi may regroup events from producers into different batches than those originally sent by the producer.

    Important: Individual events do not have cursors; cursors are properties of the batch.

    {
      "cursor": {
        "partition": "0",
        "offset": "4"
      },
      "events": [...]
    }
  11. Identify Nakadi Event Categories

    master

    Nakadi constrains all events on the platform to one of three specific categories via its HTTP API. When defining an Event Type, you must categorize it into one of the following:

    • Data Change Event: Represents a change to a record or other entity.
    • Business Event: An event that is part of, or drives, a business process.
    • Undefined Event: Used for other types of events where schema validation is still required but doesn't fit the other two categories.
  12. Understand event validation and effective schemas

    master

    Every event sent to a stream is validated against the effective schema for that event type's category.

    Validation behavior depends on the category:

    • Category-specific requirements: For example, events in the business category must include a metadata object containing eid and occurred_at fields, in addition to the fields defined in the event type's original schema.
    • Rejection: If an event does not conform to the effective schema, Nakadi will reject it. If it is valid, it is placed into a partition for consumers.