Posts

Showing posts from 2020

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

Apache Kafka - II - Topics y particiones

Image
Versión en inglés / English version Topics En Kafka, un topic es una colección ordenada de mensajes, con un nombre que lo identifica dentro de un cluster . Los productores envían mensajes a un topic específico, utilizando el nombre del topic para indicarlo. Por su parte, cualquier consumidor de ese mismo cluster puede leer mensajes del topic , indicando también su nombre. Ésta es la visión de un topic como una entidad lógica; la forma en que se almacena y maneja dentro del cluster es irrelevante para productores y consumidores. A ellos sólo les interesa enviar y recibir datos. Los topics de Kafka tienen las siguientes propiedades esenciales: Orden : Los topics en Kafka son una secuencia ordenada de mensajes. Dicho orden está dado por el orden en que los mensajes ingresan al topic , o sea el orden en que son recibidos por el cluster . Inmutabilidad : Una vez que un mensaje ha sido guardado en un topic , no puede ser modificado, ni eliminado: los

Apache Kafka - I - High level architecture and concepts

Image
Spanish version / Versión en Español First of all, what is Apache Kafka ? An initial definition for it is "a high-throughput distributed messaging system". Breaking this down a bit: High throughput: One of the main goals of Kafka is to process as much messages per unit of time as possible, in a scalable and fault-tolerant way. For example, LinkedIn , which is where Kafka originated, needs to achieve throughputs in the order of 20 million messages per second and 3 GB of data per second. Distributed messaging system: In a generic distributed system , networked computers work together for a common goal. In Kafka's case, that goal is to send data from points A 1 , A 2 , ... A N , to B 1 , B 2 , ... B M . A common benefit of this kind of system, which is crucial to Kafka's success, is horizontal scalability : the ability to handle higher and higher volumes of data by adding nodes to the system (not to be confused with  vertical scalability , whic

Apache Kafka - I - Arquitectura y conceptos de alto nivel

Image
Versión en inglés / English version En primer lugar, ¿qué es Apache Kafka ? Una definición inicial sería "un sistema distribuido de mensajería de alto throughput ". Descomponiendo: Alto throughput : Una traducción aproximada de throughput, en el contexto de sistemas informáticos, sería "rendimiento". Sin embargo, es necesario aclarar que cuando se habla de throughput , se está aludiendo concretamente a la cantidad de elementos procesados por unidad de tiempo. Uno de los objetivos principales de Kafka es precisamente procesar la mayor cantidad posible de mensajes por unidades de tiempo, de forma escalable y tolerante a fallas. Por ejemplo, LinkedIn , que es donde se originó Kafka, necesita lograr throughputs del orden de 20 millones de mensajes por segundo, y 3 GB de datos por segundo.. Sistema distribuido de mensajería: En un sistema distribuido genérico, múltiples computadoras en red colaboran con un objetivo en común. En el caso particular de Ka