Reader small image

You're reading from  Apache Kafka 1.0 Cookbook

Product typeBook
Published inDec 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781787286849
Edition1st Edition
Languages
Tools
Right arrow
Authors (2):
Raúl Estrada
Raúl Estrada
author image
Raúl Estrada

Raúl Estrada has been a programmer since 1996 and a Java developer since 2001. He loves all topics related to computer science. With more than 15 years of experience in high-availability and enterprise software, he has been designing and implementing architectures since 2003. His specialization is in systems integration, and he mainly participates in projects related to the financial sector. He has been an enterprise architect for BEA Systems and Oracle Inc., but he also enjoys web, mobile, and game programming. Raúl is a supporter of free software and enjoys experimenting with new technologies, frameworks, languages, and methods. Raúl is the author of other Packt Publishing titles, such as Fast Data Processing Systems with SMACK and Apache Kafka Cookbook.
Read more about Raúl Estrada

View More author details
Right arrow

Chapter 2. Kafka Clusters

In this chapter, we will cover the following topics:

  • Configuring a single-node single-broker cluster – SNSB
  • SNSB – creating a topic, producer, and consumer
  • Configuring a single-node multiple-broker cluster – SNMB
  • SNMB – creating a topic, producer, and consumer
  • Configuring a multiple-node multiple-broker cluster – MNMB

Introduction


In the previous chapter, we explained how to program with the Apache Kafka publisher-subscriber messaging system. In Apache Kafka there are three types of clusters:

  • Single-node single-broker
  • Single-node multiple-broker
  • Multiple-node multiple-broker cluster

The following four recipes show how to run Apache Kafka in these clusters.

Configuring a single-node single-broker cluster – SNSB


The first cluster configuration is single-node single-broker (SNSB). This cluster is very useful when a single point of entry is needed. Yes, its architecture resembles the singleton design pattern. A SNSB cluster usually satisfies three requirements:

  • Controls concurrent access to a unique shared broker
  • Access to the broker is requested from multiple, disparate producers
  • There can be only one broker

If the proposed design has only one or two of these requirements, a redesign is almost always the correct option.

Sometimes, the single broker could become a bottleneck or a single point of failure. But it is useful when a single point of communication is needed.

Getting ready

Go to the Kafka installation directory (/usr/local/kafka/ for macOS users and /opt/kafka/ for Linux users):

> cd /usr/local/kafka

How to do it...

The diagram shows an example of an SNSB cluster:

Starting ZooKeeper

  1. Kafka provides a simple ZooKeeper configuration file to launch...

SNSB – creating a topic, producer, and consumer


The SNSB Kafka cluster is running; now let's create topics, producer, and consumer.

Getting ready

We need the previous recipe executed:

  • Kafka already installed
  • ZooKeeper up and running
  • A Kafka server up and running
  • Now, go to the Kafka installation directory (/usr/local/kafka/ for macOS users and /opt/kafka/ for Linux users):
> cd /usr/local/kafka

How to do it...

The following steps will show you how to create an SNSB topic, producer, and consumer.

Creating a topic

  1. As we know, Kafka has a command to create topics. Here we create a topic called SNSBTopic with one partition and one replica:
> bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic SNSBTopic

We obtain the following output:

Created topic "SNSBTopic".

The command parameters are:

    • --replication-factor 1: This indicates just one replica
    • --partition 1: This indicates just one partition
    • --zookeeper localhost:2181: This indicates the ZooKeeper URL
  1. As...

Configuring a single-node multiple-broker cluster – SNMB


The second cluster configuration is single-node multiple-broker (SNMB). This cluster is used when there is just one node but inner redundancy is needed.

When a topic is created in Kafka, the system determines how each replica of a partition is mapped to each broker. In general, Kafka tries to spread the replicas across all available brokers.

The messages are first sent to the first replica of a partition (to the current broker leader of that partition) before they are replicated to the remaining brokers.

The producers may choose from different strategies for sending messages (synchronous or asynchronous mode). Producers discover the available brokers in a cluster and the partitions on each (all this by registering watchers in ZooKeeper).

In practice, some of the high volume topics are configured with more than one partition per broker. Remember that having more partitions increases the I/O parallelism for writes and this increases the...

SNMB – creating a topic, producer, and consumer


The SNMB Kafka cluster is running; now let's create topics, producer, and consumer.

Getting ready

We need the previous recipe executed:

  • Kafka already installed
  • ZooKeeper up and running
  • A Kafka server up and running
  • Now, go to the Kafka installation directory (/usr/local/kafka/ for macOS users and /opt/kafka/ for Linux users):
> cd /usr/local/kafka

How to do it...

The following steps will show you how to create an SNMB topic, producer, and consumer

Creating a topic

  1. Using the command to create topics, let's create a topic called SNMBTopic with two partitions and two replicas:
> bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 2 --partitions 3 --topic SNMBTopic

The following output is displayed:

Created topic "SNMBTopic".

This command has the following effects:

    • Kafka will create three logical partitions for the topic.
    • Kafka will create two replicas (copies) per partition. This means, for each partition it will pick two brokers...

Configuring a multiple-node multiple-broker cluster – MNMB


Finally, the third cluster configuration is multiple-node multiple-broker (MNMB). This cluster is used when there are several nodes and one or many brokers per node.

Getting ready

Go to the Kafka installation directory (/usr/local/kafka/ for macOS users and /opt/kafka/ for Linux users):

> cd /usr/local/kafka

How to do it...

The following diagram shows an example MNMB cluster:

Here we are presented with the real power of the cluster. In this cluster, Kafka should be installed on every machine in the cluster. Here, every physical server could have one or many brokers; all the nodes on the same cluster should connect to the same ZooKeeper.

How it works...

The good news is that all the commands in the previous recipes remain the same. The commands for ZooKeeper, the broker, producer, and consumer, don't change.

See also

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Apache Kafka 1.0 Cookbook
Published in: Dec 2017Publisher: PacktISBN-13: 9781787286849
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Raúl Estrada

Raúl Estrada has been a programmer since 1996 and a Java developer since 2001. He loves all topics related to computer science. With more than 15 years of experience in high-availability and enterprise software, he has been designing and implementing architectures since 2003. His specialization is in systems integration, and he mainly participates in projects related to the financial sector. He has been an enterprise architect for BEA Systems and Oracle Inc., but he also enjoys web, mobile, and game programming. Raúl is a supporter of free software and enjoys experimenting with new technologies, frameworks, languages, and methods. Raúl is the author of other Packt Publishing titles, such as Fast Data Processing Systems with SMACK and Apache Kafka Cookbook.
Read more about Raúl Estrada