Posts

Showing posts from June, 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