Posts

Apache Kafka - V - Consumer Groups

Image
Spanish version / Versión en Español A single consumer, having to receive and process messages in a single thread, can soon become a bottleneck, especially as producers, topics and partitions grow in number. Kafka overcomes this limitation by allowing consumers to share their load as evenly as possible by setting the group.id option on creation, and thus belonging to a consumer group. When a bunch of consumers set the same group id, a broker inside the cluster is automatically assigned as the group coordinator: his goal will be to monitor the consumers and keep track of their membership. The group coordinator also communicates with Zookeeper and the cluster controller/leader node in order to assign partitions to each consumer in the group. The ideal situation is to have consumer and partitions evenly matched, i.e. each consumer will read only one partition from the topic. Each consumer regularly sends a heartbeat to the cluster (with a frequency defined by t...

Apache Kafka - V - Consumer Groups

Image
Versión en inglés / English version Un solo consumidor, teniendo que recibir y procesar mensajes en un solo thread , puede volverse rápidamente un cuello de botella. Este problema se acrecenta al aumentar la cantidad de topics y particiones. Kafka resuelve esta limitación permitiendo que varios consumidores repartan su carga de manera equitativa fijando el mismo valor en la propiedad group.id al momento de su creación. Haciendo esto, pasan a pertenecer al mismo consumer group . Cuando un conjunto de consumidores tiene el mismo id de grupo, un broker dentro del cluster es asignado automáticamente como el coordinador del grupo. Su objetivo será monitorear a los consumidores del grupo y llevar cuenta de su pertenencia al mismo. El coordinador del grupo también se comunica con Zookeeper para balancear consumidores y particiones: idealmente, cada consumidor leerá una partición distinta del topic . Cada consumidor envía periódicamente un heartbeat al cluster ...

Apache Kafka - IV - Consumers

Image
Spanish version / Versión en Español A basic Kafka consumer application, using the same library used for producers in part 3 , could be: public class KafkaConsumerApp{ public static void main([]String args) { Properties props = new Properties(); props.put("bootstrap.servers", "BROKER-1:9092, BROKER-2:9093"); props.put("key.deserializer", "org.apache.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.common.serialization.StringDeserializer"); KafkaConsumer myConsumer = new KafkaConsumer(props); myConsumer.subscribe(Arrays.asList("my-topic")); try{ while (true) { ConsumerRecords<String, String> records = myConsumer.poll(100); processRecords(records); } } catch (Exception ex) { ex.printStackTrace(); } finally { myCon...

Apache Kafka - IV - Consumidores

Image
Versión en inglés / English version Una aplicación consumidora básica, usando la misma biblioteca que en la parte 3 , podría ser: public class KafkaConsumerApp{ public static void main([]String args) { Properties props = new Properties(); props.put("bootstrap.servers", "BROKER-1:9092, BROKER-2:9093"); props.put("key.deserializer", "org.apache.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.common.serialization.StringDeserializer"); KafkaConsumer myConsumer = new KafkaConsumer(props); myConsumer.subscribe(Arrays.asList("my-topic")); try{ while (true) { ConsumerRecords<String, String> records = myConsumer.poll(100); processRecords(records); } } catch (Exception ex) { ex.printStackTrace(); } finally { myConsumer.c...

Apache Kafka - III - Producers

Spanish version / Versión en Español All code examples in this post will be in Java , for simplicity; however, keep in mind that Kafka offers client libraries in a myriad of languages. More specifically, in a Maven project, the Kafka dependency can be defined by: groupId: org.apache.kafka artifactId: kafka-clients version: 0.10.0.1 (or newer if available) So, to create a producer, some basic properties are required (although there are many, many more optional properties to tweak): Properties props = new Properties(); props.put("bootstrap.servers", "BROKER-1:9092, BROKER-2:9093"); props.put("key.serializer", "org.apache.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.common.serialization.StringSerializer"); The bootstrap.servers property defines a list of brokers in the cluster the producer can connect to. It doesn't need to be a full list of the cluster...

Apache Kafka - III - Productores

Versión en inglés / English version Por simplicidad, todos los ejemplos de código del post serán en Java . Sin embargo, es importante considerar que Kafka ofrece bibliotecas cliente en varios otros lenguajes. Volviendo a Java, en un proyecto gestionado por Maven , la dependencia con Kafka puede estar dada por: groupId: org.apache.kafka artifactId: kafka-clients version: 0.10.0.1 (o más reciente) Con la dependencia agregada, para crear un productor, será necesario agregar algunas propiedades básicas al proyecto. Hay muchas, muchas más; se verán las básicas por ahora: Properties props = new Properties(); props.put("bootstrap.servers", "BROKER-1:9092, BROKER-2:9093"); props.put("key.serializer", "org.apache.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.common.serialization.StringSerializer"); La propiedad bootstrap.servers define una lista de brokers del cluste...

Apache Kafka - II - Topics and partitions

Image
Topics In Kafka, a topic is a named feed, or a category of messages. Producers send messages to a specific topic, addressable by its name, and consumers can read messages from that topic, also addressing it by name (all of this in the context of a particular Kafka cluster). This is how a topic is understood as a logical entity: the way it's stored and handled inside the cluster doesn't matter to producers and consumers; they just want to send and read data. Kafka topics have the following essential characteristics: Order : Kafka topics are a time-ordered sequence of messages. Messages are saved in the topic in the order in which they are received. Immutability : Once a message has been saved to a topic, it cannot be modified, or deleted: topics are append-only structures (because of the time order). If a producer sends invalid data in a message, it will be up to him to notify the consumer and send the corrected data in a new message, and the consumer will have...