for developer #1
ภูริเดช สุดสี (เอาะ)
โปรแกรมเมอรนอรแมด
Course Outline
Introduction
- Key concept
- Architecture
- Decoupling application
- Example use case
- Technical term e.g. Topic / Partition
/ Producer / Consumer
- Component
- Retention policy
- FAQ and Limitation
Setting up
- Setup Docker Compose for test bed
kafka cluster
- Use GUI to connect Kafa to familiar
with tool and concept
Hand on
- Implement Kafka producer and
consumer application
- Testing and debugging
Monitoring (If possible)
- Monitoring tool
2
Kafka’s origin story
- Initial project 2010, Initial release 2011
- Develop at Linkedin
- Written in Java/Scala
- Named after Franz Kafka, "a system
optimized for writing"
- Written in Java/Scala
- Page view, page event, aggregated logs to
Apache Hadoop.
- Need distributed architecture
Jay Kreps
CEO, Confluent
Neha Narkhede
CTO, Confluent
Jun Rao
Co-founder, Confluent
3
reference: Apache Kafka
4
references:
[1]Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java ,
Koutanov, Emil, eBook - Amazon.com
[2] Deep Learning Infrastructure for Extreme Scale with the Apache Kafka Open Source Ecosystem - DataScienceCentral.com
As of 2019 | 100+ cluster | 4,000 brokers | 100,000 topics |
> 4.5 Trillion message per day
As of 2015 | 20 Gbps
As of 2015 | 5 Billion session per day | Stream processing
use case (Apache Storm,Apache Hadoop and AWS
Elastic MapReduce)
As of 2016 | 700 Billion event | Stream processing with
Keystone | > 1 Trillion message per day
Tech giant use case
5
Use cases (1) Application activity tracking
- Kafka มีจุดเริ่มตนมาจาก Linkedin อยากรูการใช
งานของผูใช
- ทําเครื่องมือสําหรับเก็บการกด Click, เวลาที่ใชแตละ
Page, การลงทะเบียนและขอมูลตาง ๆ ของผูใช
- ทํา User personalize
- จัดขอมูลใหผูอานแตละคนโดยเฉพาะได
- การเก็บขอมูลจํานวนมาก ทําใหรูความตองการ หรือ
กระทั้งปลูกฝงความตองการ
- การเห็นขอมูลความตองการทําใหเตรียม Stock
สินคา หรือบริการได
6
Use cases (2) Messaging system
- ชวยสงผานขอมูลในรูปของ Message
- วิธีคิดแบบขอความชวยใหเราสามารถแบงการประมวลผลเปน Process ยอย ๆ ไดดี
- Message ทําใหการแบงการประมวลผลเปนแบบขนานทําไดดีขึ้น
- บางงานสามารถใชเปน Buffer คําขอได
7
Use cases (3) Decoupling service
- Microservice ทําให Context ของ Application อยูใน Domain ของตัวเอง
- Service ทําสําคัญจะตรวจสอบไดงาย ทําใหดูแลงาย
- หลังจากแยก Service ก็ยังตองการติดตอกัน
- สามารถติดตอผานทาง Kafka โดย Consumer จะทํางานตามความเร็วของตัว
เอง ทําให Service สามารถแยกกันได และไดความเสถียรมากขึ้น
8
Use cases (4) Application logs gathering and monitoring
- ใชเก็บ Log แบบ Near real-time
(AWS/GCP มี Service แตอาจจะชากวาและ
ตองจายเพิ่มในบางกรณี)
- บางกรณีตองการเอา Application log ไปเก็บ
ที่อื่น
- Reduce log message
- ตรวจความผิดปกติของ Application
- ชวยทํา Alert
- เก็บ Metric ตาง ๆ ของระบบ (Log-base
metric)
9
Use cases (5) Real-time data processing
- มี Internal tool สําหรับ Process data แบบ
Real-time (KStream)
- เชื่อมตอกับ External tool ได (Hadoop, Spark)
- ประมวลผลขอมูลเชน Recommend สินคาไดทัน
ความตองการ
- ทําพวก Fraud detection / Security Intrusion
Detection
- ลดการประมวลผลขอมูลทีละมาก ๆ (Resources
cost)
10
Use cases (5) Tracking log
- เก็บขอมูล IOT
- เก็บขอมูล Sensor
- GPS
- Application / User positioning
11
Messaging Queue vs Pub/Sub
reference: Pub-Sub vs. Message Queues | Baeldung
Exactly once approach At least 1 approach
12
Pub/Sub messaging pattern (1)
reference: Kafka: The Definitive Guide 2, Shapira, Gwen, Palino, Todd, Sivaram, Rajini, Petty, Krit, eBook - Amazon.com
13
Pub/Sub messaging pattern (2)
reference: Kafka: The Definitive Guide 2, Shapira, Gwen, Palino, Todd, Sivaram, Rajini, Petty, Krit, eBook - Amazon.com
14
Pub/Sub messaging pattern (3)
reference: Kafka: The Definitive Guide 2, Shapira, Gwen, Palino, Todd, Sivaram, Rajini, Petty, Krit, eBook - Amazon.com
15
Pub/Sub messaging pattern (4)
reference: Apache Kafka Core Concepts - Learning Journal
16
Pub/Sub messaging pattern (5)
reference: Kafka: The Definitive Guide 2, Shapira, Gwen, Palino, Todd, Sivaram, Rajini, Petty, Krit, eBook - Amazon.com
17
Kafka Pub/Sub messaging pattern
reference: Kafka: The Definitive Guide 2, Shapira, Gwen, Palino, Todd, Sivaram, Rajini, Petty, Krit, eBook - Amazon.com
18
Kafka key feature (1)
reference: Top 10 Kafka Features | Why Apache Kafka Is So Popular - DataFlair (data-flair.training)
a. Scalability
Apache Kafka can handle scalability in all the four
dimensions, i.e. event producers, event processors,
event consumers and event connectors. In other
words, Kafka scales easily without downtime.
b. High-Volume
Kafka can work with the huge volume of data streams,
easily.
c. Data Transformations
Kafka offers provision for deriving new data streams
using the data streams from producers.
d. Fault Tolerance
The Kafka cluster can handle
failures with the masters and
databases.
e. Reliability
Since Kafka is distributed,
partitioned, replicated and fault
tolerant, it is very Reliable.
19
Kafka key feature (2)
reference: Top 10 Kafka Features | Why Apache Kafka Is So Popular - DataFlair (data-flair.training)
f. Durability
It is durable because Kafka uses Distributed commit
log, that means messages persists on disk as fast as
possible.
g. Performance
For both publishing and subscribing messages, Kafka
has high throughput. Even if many TB of messages is
stored, it maintains stable performance.
h. Zero Downtime
Kafka is very fast and guarantees zero downtime and
zero data loss.
i. Extensibility
There are as many ways by which
applications can plug in and make
use of Kafka. In addition, offers
ways by which to write new
connectors as needed.
j. Replication
By using ingest pipelines, it can
replicate the events.
So, this was all about Apache Kafka
Features. Hope you like our
explanation.
20
Kafka architecture overview
reference: Kafka Enterprise Architecture - Learning Journal
21
reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java ,
Koutanov, Emil, eBook - Amazon.com
Kafka architecture overview
22
reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java ,
Koutanov, Emil, eBook - Amazon.com
Broker node overview
> Java process ☕
> Increase broker then IO,
Availability and Durability
🚀
> Append only log
> Consumer will be reading
from broker node (leader)
> Single node is elected as
cluster controller
23
reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java ,
Koutanov, Emil, eBook - Amazon.com
Zookeeper overview
> Elected controller broker
node
> Ensure at most one
controller broker node
> Store some configuration
> Maintain cluster metadata
> Housekeeping item
> Number of ZK node must
be odds!
> Security weak 🔐
24
Placement concern
> เลี่ยงการวาง Broker ไวในที่เดียวกัน ถาใช Cloud ใหกําหนด
Placement ใหกระจาย Zone หรือ Rack
> Zookeeper กับ Broker วางที่เดียวกันไดแตระวังเรื่อง Network
bandwidth
> Setup broker.rack=[rack-id]
25
reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java ,
Koutanov, Emil, eBook - Amazon.com
Producer overview
> Kafka client to produce
message
> TCP connect to Kafka
cluster
> Unable to modify
message log
26
reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java ,
Koutanov, Emil, eBook - Amazon.com
Consumer overview
> Kafka client to consume
message
> Parallel at most as
number of partition
> Lost consumer can cause
rebalance
27
Topic
reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java ,
Koutanov, Emil, eBook - Amazon.com
28
reference: Understanding Kafka Topic Partitions | by Dunith Dhanushka | Event-driven Utopia | Medium
Partition
29
reference: Why you should know Apache Kafka | b-nova
Partition
30
reference: Why you should know Apache Kafka | b-nova
Partition leader and follower (Replication factor)
RF <= No of broker
31
reference: Why you should know Apache Kafka | b-nova
Message, Record, Log
> ที่ Offset เดียวกัน แตคนละ Partition
ขอความเปนคนละขอความ
> Partial order in partition
> Default max = 1MB
Timestamp: เซ็ตจาก Producer หรือใชอัตโนมัติได
Header: Set ของ Key/Value สําหรับใชเปน Metadata
Partition number: 0-base index partition number
Offset: 64 signed integer ตําแหนงของ message ใน partition
Key: Byte array เปนอะไรก็ไดไมจําเปนตอง Unique
Value: Byte array
32
Partition awareness
> same key go to same partition (hash function)
> Number of partition can increase but cannot decrease
33
reference: Kafka Streams, co-partitioning requirements illustrated | by Loïc DIVAD | Publicis Sapient Engineering | Medium
Partition hash function
34
Partition and consumer group
reference: Kafka: The Definitive Guide 2, Shapira, Gwen, Palino, Todd, Sivaram, Rajini, Petty, Krit, eBook - Amazon.com
35
Advertise listener
reference: Apache Kafka Series - Kafka Cluster Setup & Administration | Udemy
./config/server.properties
36
Watermark
reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java ,
Koutanov, Emil, eBook - Amazon.com
37
Consumer processing
reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java ,
Koutanov, Emil, eBook - Amazon.com
38
> max.poll.interval.ms how much time
permit to complete processing by
consumer, increased time,increases the
rebalancing time
> session.timeout.ms is the timeout used
to identify if the consumer is still alive and
sending a heartbeat
Rebalance
reference: Kafka: The Definitive Guide 2, Shapira, Gwen, Palino, Todd, Sivaram, Rajini, Petty, Krit, eBook - Amazon.com
39
Component relation
reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java ,
Koutanov, Emil, eBook - Amazon.com
40
Architecture wrap-up
reference: Apache Kafka® Architecture: A Complete Guide - Instaclustr
41
reference: Why you should know Apache Kafka | b-nova
Retention policy
> Time-base (7 days) default: log.retention.hours=168
> Size-base (1 GiB) default: log.retention.bytes=1073741824
> per broker
> per topic
42
reference: A Deep Dive into Apache Kafka This is Event Streaming by Andrew Dunnings & Katherine Stanley - YouTube
Offset
43
reference: Kafka Topic Configuration: Min In-Sync Replicas | Learn Apache Kafka (conduktor.io)
Sync & Async Producer
Sync
- ACK=0 (fire and forgot)
- ACK=1 (only leader)
- ACK=all (all min.insync.replicas)
Async
- Support callback (specify
number of request
max.in.flight.requests.per.connec
tion,and retries flag)
44
Commit sync & async consumer
commitSync
Commit message แลวจะ
Block I/O รอผลจากการ
commit จะจัดการ Error ได
งายกวา
commitAsync
Commit message แลวจะไม
Block I/O ทําใหจัดการ Error
ไดยากกวา หากตองการจัดการ
Error ใชผาน callback
function ได
45
reference: Kafka Producer Batching | Learn Apache Kafka with Conduktor
Batch.size and linger.ms
⬆ batch.size
⬆ Thoughtput
⬆ Latency
46
KRaft, KIP-500 (1)
reference: The Apache Kafka Control Plane ­ ZooKeeper vs. KRaft - YouTube
47
reference: The Apache Kafka Control Plane ­ ZooKeeper vs. KRaft - YouTube
KRaft, KIP-500 (2)
48
Before Avro, CSV
+ อานงาย
+ Parse งาย
+ เขาใจงาย
===========
- ไมมี Data type กํากับ
- ถาขอมูลมี , อยูงานงอก
- name,,ID ตองหาวิธีแยก Empty กับ Null
49
Before Avro, Relational Database
+ มี Data type
+ อยูในรูป Table
===========
- Data type อาจจะไมเหมือนกันถา DB
engine ตางกัน
- ตองมองขอมูลในรูป Row ของ Table
50
Before Avro, JSON
+ ขอมูลอยูในรูป nested JSON ซอน
กันได
+ อยูในรูป Array ได
+ สามารถใชไดกับเกือบทุกภาษา
+ เปนที่นิยม
+ easily to share over network
===========
- ไมมี Schema ที่ชัดเจน (อาจทําให
ขอมูลเดิมพัง)
- ตองใช Key ทุกครั้ง ทําใหเสีย
Bandwidth / Storage เยอะ
51
+ กําหนด Schema ดวย JSON
+ มี Data type
+ Auto compression
+ อานขามภาษาได
+ Schema เปลี่ยนแปลงได (verify backward
compatibility ได)
+ เหมาะกับการเขียน (มากกวา Parquet)
+ Support Hive
===========
- อาจจะไมมี Support ในภาษาบางภาษา
- เนื่องจากมัน compressed อยู ตองใชเครื่องมืออาน
Avro
52
Avro compatibility
reference: Schema Evolution and
Compatibility | Confluent Documentation 53
Delivery semantic for consumer (1)
> At most once: offsets are committed as soon as the message is
received. If the processing goes wrong, the message will be lost
(it won’t be read again).
reference: Delivery Semantics for Kafka Consumers | Learn Apache Kafka (conduktor.io)
enable.auto.commit=true
auto.commit.interval.ms=5000 (5s)
54
Delivery semantic for consumer (2)
> At least once: offsets are committed after the message is processed. If the processing goes
wrong, the message will be read again. This can result in duplicate processing of messages. Make
sure your processing is idempotent (i.e. processing again the messages won’t impact your
systems)
reference: Delivery Semantics for Kafka Consumers | Learn Apache Kafka (conduktor.io)
enable.auto.commit=true
auto.commit.interval.ms=5000 (5s)
55
Delivery semantic for consumer (3)
> Exactly once: Every message is
guaranteed to be persisted in Kafka
exactly once without any duplicates and
data loss even where there is a broker
failure or producer retry.
reference: Exactly-Once Processing in Kafka explained | by sudan | Medium
Producer
enable.idempotence=true
transactional.id=<id> *
Consumer
enable.auto.commit=false
isolation.level=read_committed
(* is optional)
56
Topic design
> อยาใช Topic เดียวทําทุกอยาง
> Consumer อานขอมูลจาก Partition leader ถา leader
อยูเครื่องเดียวกันเยอะ อาจมีปญหา Performance
> ควรใช Data schema แบบเดิม / ไม break
backward-compat
> ตองการ Order ควรให Message ตก Partition เดียวกัน
> ออกแบบ Key ดีไดทั้ง Ordering และ Performance
> พิจารณาแยก Topic ในกรณีที่ตองการ Throughput สูงๆ
57
Software architecture design
reference: Disaster Recovery for Multi-Region Kafka at Uber - Uber Engineering Blog
58
Software architecture design
59
Software architecture design
60
Software architecture design (Chatting)
61
Segment
reference: Log Compacted Topics in Apache Kafka | by Seyed Morteza Mousavi | Towards Data Science
Segment Active
Segment
62
Broker config for production
> Recommended Java 8, 11
> Memory at least 8 GiB
> Change log.dirs (default is temp dir)
> At least 3 broker (Replication Factor 3)
> auto.create.topics.enable=false
> change num.partitions to expect consumer
63
Log compaction (changelog topic)
reference: Log Compacted Topics in Apache Kafka | by Seyed Morteza Mousavi | Towards Data Science
64
Performance test
65
Offset Explorer
66
Kafdrop
java --add-opens=java.base/sun.nio.ch=ALL-UNNAMED
-jar target/kafdrop-3.30.0.jar.jar
--kafka.brokerConnect=localhost:9092
67
KSteam overview
reference: Kafka Streams Overview | Confluent Documentation
68
KSteam benefit
reference: Kafka Streams Overview | Confluent Documentation
> Makes your applications highly scalable, elastic, distributed,
fault-tolerant
> Supports exactly-once processing semantics
> Stateful and stateless processing
> Event-time processing with windowing, joins, aggregations
> Supports Kafka Streams Interactive Queries to unify the worlds of
streams and databases
> Choose between a declarative, functional API and a lower-level
imperative API for maximum control and flexibility
69
KTable
reference: Streams Concepts | Confluent Documentation
> KSteam is a KTable duality
70
Kafka connect
> Data Centric Pipeline ­
Connect uses meaningful
data abstractions to pull
or push data.
> Flexibility
reference: Kafka Streams Overview | Confluent Documentation
> Scalability
> Reusability
> Extensibility
=> File sync
=> Database Sync
71
ksqlDB
reference: ksqlDB
72
Security
In-transit At rest
> GSSAPI
Kerberos authentication
> PLAIN
Username/password authentication
> SCRAM-SHA-256 and SCRAM-SHA-512
Username/password authentication
> OAUTHBEARER
Authentication using OAuth
No out of the box solution.
reference: Encrypting Kafka messages at rest to secure applications | Kafka Summit Europe 2021 - Confluent
73
SSL
reference: Apache Kafka Security 101 | Confluent
74
Monitoring
75
Monitoring
76

More Related Content

DOCX
PPTX
IOT home appliance with cordova and firebase
PDF
Proxy fc4 web
PDF
Red5 streaming
PDF
Modern DevOps Day 4.pdfModern DevOps Day 4.pdf
PDF
The Future of JavaScript
PDF
Modern DevOps Day 5.pdfModern DevOps Day 5.pdf
IOT home appliance with cordova and firebase
Proxy fc4 web
Red5 streaming
Modern DevOps Day 4.pdfModern DevOps Day 4.pdf
The Future of JavaScript
Modern DevOps Day 5.pdfModern DevOps Day 5.pdf

Similar to Kafka for developer (20)

PDF
MySQL Multi-Master Replication Using Tungsten Replicator 2.0.5
PDF
Nest.js Microservices (1).pdf Nest.js Microservices (1).pdfNest.js Microservi...
PDF
IT Trends eMagazine Vol 4. No.12
PDF
Web Development
PPT
Centralized Log Server Thai version
PPTX
คุณสมบัติของข้อมูลกิจกรรมและองค์ประกอบอื่นๆ ที่เกี่ยวข้องกับความมั่นคงปลอดภัย...
PPTX
Computer systemarchitecture
PDF
Python Programming for Lecturer_RUS_Nonthaburi 17may2019
PDF
Web 2.0 mashup
DOC
ประว ต ของ Cloud computing
PDF
การสื่อสาร อ.พรทิพย์
PPT
PPT
What is Cloud Computing ?
PPT
Protocol
PPT
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 05
PDF
Database Tuning for e-Learning
PDF
01 ความรู้เกี่ยวกับการ web programming
PDF
30879960 geospatial-system-on-cloud-computing
PDF
Basic Kong API Gateway
MySQL Multi-Master Replication Using Tungsten Replicator 2.0.5
Nest.js Microservices (1).pdf Nest.js Microservices (1).pdfNest.js Microservi...
IT Trends eMagazine Vol 4. No.12
Web Development
Centralized Log Server Thai version
คุณสมบัติของข้อมูลกิจกรรมและองค์ประกอบอื่นๆ ที่เกี่ยวข้องกับความมั่นคงปลอดภัย...
Computer systemarchitecture
Python Programming for Lecturer_RUS_Nonthaburi 17may2019
Web 2.0 mashup
ประว ต ของ Cloud computing
การสื่อสาร อ.พรทิพย์
What is Cloud Computing ?
Protocol
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 05
Database Tuning for e-Learning
01 ความรู้เกี่ยวกับการ web programming
30879960 geospatial-system-on-cloud-computing
Basic Kong API Gateway
Ad

More from Bhuridech Sudsee (20)

PDF
Xss and sql injection
PPTX
Introduction to Quantum Computing
PDF
$ Spark start
PDF
เสี่ยวเอ้อสอน Spark
PDF
Jpa sa-60
PDF
18 ฝ่ามือพิชิต docker
PDF
Cloud computing
PDF
หนังสือภาษาไทย Spark Internal
PDF
VBoxManage tutorial
PDF
Git ฉบับอนุบาล 2
PDF
sample plot 3D form depth map using OpenCV
PDF
illustrator & design workshop
PDF
The bounded buffer
PDF
operating system
PDF
Producer and Consumer problem
PDF
OPD System with ZK Grails
PDF
Market management with ZK Grails
PDF
Websocket & HTML5
PDF
Networking section present
PDF
Breast Cancer data mining KDD
Xss and sql injection
Introduction to Quantum Computing
$ Spark start
เสี่ยวเอ้อสอน Spark
Jpa sa-60
18 ฝ่ามือพิชิต docker
Cloud computing
หนังสือภาษาไทย Spark Internal
VBoxManage tutorial
Git ฉบับอนุบาล 2
sample plot 3D form depth map using OpenCV
illustrator & design workshop
The bounded buffer
operating system
Producer and Consumer problem
OPD System with ZK Grails
Market management with ZK Grails
Websocket & HTML5
Networking section present
Breast Cancer data mining KDD
Ad

Kafka for developer

  • 1. for developer #1 ภูริเดช สุดสี (เอาะ) โปรแกรมเมอรนอรแมด
  • 2. Course Outline Introduction - Key concept - Architecture - Decoupling application - Example use case - Technical term e.g. Topic / Partition / Producer / Consumer - Component - Retention policy - FAQ and Limitation Setting up - Setup Docker Compose for test bed kafka cluster - Use GUI to connect Kafa to familiar with tool and concept Hand on - Implement Kafka producer and consumer application - Testing and debugging Monitoring (If possible) - Monitoring tool 2
  • 3. Kafka’s origin story - Initial project 2010, Initial release 2011 - Develop at Linkedin - Written in Java/Scala - Named after Franz Kafka, "a system optimized for writing" - Written in Java/Scala - Page view, page event, aggregated logs to Apache Hadoop. - Need distributed architecture Jay Kreps CEO, Confluent Neha Narkhede CTO, Confluent Jun Rao Co-founder, Confluent 3
  • 5. references: [1]Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java , Koutanov, Emil, eBook - Amazon.com [2] Deep Learning Infrastructure for Extreme Scale with the Apache Kafka Open Source Ecosystem - DataScienceCentral.com As of 2019 | 100+ cluster | 4,000 brokers | 100,000 topics | > 4.5 Trillion message per day As of 2015 | 20 Gbps As of 2015 | 5 Billion session per day | Stream processing use case (Apache Storm,Apache Hadoop and AWS Elastic MapReduce) As of 2016 | 700 Billion event | Stream processing with Keystone | > 1 Trillion message per day Tech giant use case 5
  • 6. Use cases (1) Application activity tracking - Kafka มีจุดเริ่มตนมาจาก Linkedin อยากรูการใช งานของผูใช - ทําเครื่องมือสําหรับเก็บการกด Click, เวลาที่ใชแตละ Page, การลงทะเบียนและขอมูลตาง ๆ ของผูใช - ทํา User personalize - จัดขอมูลใหผูอานแตละคนโดยเฉพาะได - การเก็บขอมูลจํานวนมาก ทําใหรูความตองการ หรือ กระทั้งปลูกฝงความตองการ - การเห็นขอมูลความตองการทําใหเตรียม Stock สินคา หรือบริการได 6
  • 7. Use cases (2) Messaging system - ชวยสงผานขอมูลในรูปของ Message - วิธีคิดแบบขอความชวยใหเราสามารถแบงการประมวลผลเปน Process ยอย ๆ ไดดี - Message ทําใหการแบงการประมวลผลเปนแบบขนานทําไดดีขึ้น - บางงานสามารถใชเปน Buffer คําขอได 7
  • 8. Use cases (3) Decoupling service - Microservice ทําให Context ของ Application อยูใน Domain ของตัวเอง - Service ทําสําคัญจะตรวจสอบไดงาย ทําใหดูแลงาย - หลังจากแยก Service ก็ยังตองการติดตอกัน - สามารถติดตอผานทาง Kafka โดย Consumer จะทํางานตามความเร็วของตัว เอง ทําให Service สามารถแยกกันได และไดความเสถียรมากขึ้น 8
  • 9. Use cases (4) Application logs gathering and monitoring - ใชเก็บ Log แบบ Near real-time (AWS/GCP มี Service แตอาจจะชากวาและ ตองจายเพิ่มในบางกรณี) - บางกรณีตองการเอา Application log ไปเก็บ ที่อื่น - Reduce log message - ตรวจความผิดปกติของ Application - ชวยทํา Alert - เก็บ Metric ตาง ๆ ของระบบ (Log-base metric) 9
  • 10. Use cases (5) Real-time data processing - มี Internal tool สําหรับ Process data แบบ Real-time (KStream) - เชื่อมตอกับ External tool ได (Hadoop, Spark) - ประมวลผลขอมูลเชน Recommend สินคาไดทัน ความตองการ - ทําพวก Fraud detection / Security Intrusion Detection - ลดการประมวลผลขอมูลทีละมาก ๆ (Resources cost) 10
  • 11. Use cases (5) Tracking log - เก็บขอมูล IOT - เก็บขอมูล Sensor - GPS - Application / User positioning 11
  • 12. Messaging Queue vs Pub/Sub reference: Pub-Sub vs. Message Queues | Baeldung Exactly once approach At least 1 approach 12
  • 13. Pub/Sub messaging pattern (1) reference: Kafka: The Definitive Guide 2, Shapira, Gwen, Palino, Todd, Sivaram, Rajini, Petty, Krit, eBook - Amazon.com 13
  • 14. Pub/Sub messaging pattern (2) reference: Kafka: The Definitive Guide 2, Shapira, Gwen, Palino, Todd, Sivaram, Rajini, Petty, Krit, eBook - Amazon.com 14
  • 15. Pub/Sub messaging pattern (3) reference: Kafka: The Definitive Guide 2, Shapira, Gwen, Palino, Todd, Sivaram, Rajini, Petty, Krit, eBook - Amazon.com 15
  • 16. Pub/Sub messaging pattern (4) reference: Apache Kafka Core Concepts - Learning Journal 16
  • 17. Pub/Sub messaging pattern (5) reference: Kafka: The Definitive Guide 2, Shapira, Gwen, Palino, Todd, Sivaram, Rajini, Petty, Krit, eBook - Amazon.com 17
  • 18. Kafka Pub/Sub messaging pattern reference: Kafka: The Definitive Guide 2, Shapira, Gwen, Palino, Todd, Sivaram, Rajini, Petty, Krit, eBook - Amazon.com 18
  • 19. Kafka key feature (1) reference: Top 10 Kafka Features | Why Apache Kafka Is So Popular - DataFlair (data-flair.training) a. Scalability Apache Kafka can handle scalability in all the four dimensions, i.e. event producers, event processors, event consumers and event connectors. In other words, Kafka scales easily without downtime. b. High-Volume Kafka can work with the huge volume of data streams, easily. c. Data Transformations Kafka offers provision for deriving new data streams using the data streams from producers. d. Fault Tolerance The Kafka cluster can handle failures with the masters and databases. e. Reliability Since Kafka is distributed, partitioned, replicated and fault tolerant, it is very Reliable. 19
  • 20. Kafka key feature (2) reference: Top 10 Kafka Features | Why Apache Kafka Is So Popular - DataFlair (data-flair.training) f. Durability It is durable because Kafka uses Distributed commit log, that means messages persists on disk as fast as possible. g. Performance For both publishing and subscribing messages, Kafka has high throughput. Even if many TB of messages is stored, it maintains stable performance. h. Zero Downtime Kafka is very fast and guarantees zero downtime and zero data loss. i. Extensibility There are as many ways by which applications can plug in and make use of Kafka. In addition, offers ways by which to write new connectors as needed. j. Replication By using ingest pipelines, it can replicate the events. So, this was all about Apache Kafka Features. Hope you like our explanation. 20
  • 21. Kafka architecture overview reference: Kafka Enterprise Architecture - Learning Journal 21
  • 22. reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java , Koutanov, Emil, eBook - Amazon.com Kafka architecture overview 22
  • 23. reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java , Koutanov, Emil, eBook - Amazon.com Broker node overview > Java process ☕ > Increase broker then IO, Availability and Durability 🚀 > Append only log > Consumer will be reading from broker node (leader) > Single node is elected as cluster controller 23
  • 24. reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java , Koutanov, Emil, eBook - Amazon.com Zookeeper overview > Elected controller broker node > Ensure at most one controller broker node > Store some configuration > Maintain cluster metadata > Housekeeping item > Number of ZK node must be odds! > Security weak 🔐 24
  • 25. Placement concern > เลี่ยงการวาง Broker ไวในที่เดียวกัน ถาใช Cloud ใหกําหนด Placement ใหกระจาย Zone หรือ Rack > Zookeeper กับ Broker วางที่เดียวกันไดแตระวังเรื่อง Network bandwidth > Setup broker.rack=[rack-id] 25
  • 26. reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java , Koutanov, Emil, eBook - Amazon.com Producer overview > Kafka client to produce message > TCP connect to Kafka cluster > Unable to modify message log 26
  • 27. reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java , Koutanov, Emil, eBook - Amazon.com Consumer overview > Kafka client to consume message > Parallel at most as number of partition > Lost consumer can cause rebalance 27
  • 28. Topic reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java , Koutanov, Emil, eBook - Amazon.com 28
  • 29. reference: Understanding Kafka Topic Partitions | by Dunith Dhanushka | Event-driven Utopia | Medium Partition 29
  • 30. reference: Why you should know Apache Kafka | b-nova Partition 30
  • 31. reference: Why you should know Apache Kafka | b-nova Partition leader and follower (Replication factor) RF <= No of broker 31
  • 32. reference: Why you should know Apache Kafka | b-nova Message, Record, Log > ที่ Offset เดียวกัน แตคนละ Partition ขอความเปนคนละขอความ > Partial order in partition > Default max = 1MB Timestamp: เซ็ตจาก Producer หรือใชอัตโนมัติได Header: Set ของ Key/Value สําหรับใชเปน Metadata Partition number: 0-base index partition number Offset: 64 signed integer ตําแหนงของ message ใน partition Key: Byte array เปนอะไรก็ไดไมจําเปนตอง Unique Value: Byte array 32
  • 33. Partition awareness > same key go to same partition (hash function) > Number of partition can increase but cannot decrease 33
  • 34. reference: Kafka Streams, co-partitioning requirements illustrated | by Loïc DIVAD | Publicis Sapient Engineering | Medium Partition hash function 34
  • 35. Partition and consumer group reference: Kafka: The Definitive Guide 2, Shapira, Gwen, Palino, Todd, Sivaram, Rajini, Petty, Krit, eBook - Amazon.com 35
  • 36. Advertise listener reference: Apache Kafka Series - Kafka Cluster Setup & Administration | Udemy ./config/server.properties 36
  • 37. Watermark reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java , Koutanov, Emil, eBook - Amazon.com 37
  • 38. Consumer processing reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java , Koutanov, Emil, eBook - Amazon.com 38 > max.poll.interval.ms how much time permit to complete processing by consumer, increased time,increases the rebalancing time > session.timeout.ms is the timeout used to identify if the consumer is still alive and sending a heartbeat
  • 39. Rebalance reference: Kafka: The Definitive Guide 2, Shapira, Gwen, Palino, Todd, Sivaram, Rajini, Petty, Krit, eBook - Amazon.com 39
  • 40. Component relation reference: Effective Kafka: A Hands-On Guide to Building Robust and Scalable Event-Driven Applications with Code Examples in Java , Koutanov, Emil, eBook - Amazon.com 40
  • 41. Architecture wrap-up reference: Apache Kafka® Architecture: A Complete Guide - Instaclustr 41
  • 42. reference: Why you should know Apache Kafka | b-nova Retention policy > Time-base (7 days) default: log.retention.hours=168 > Size-base (1 GiB) default: log.retention.bytes=1073741824 > per broker > per topic 42
  • 43. reference: A Deep Dive into Apache Kafka This is Event Streaming by Andrew Dunnings & Katherine Stanley - YouTube Offset 43
  • 44. reference: Kafka Topic Configuration: Min In-Sync Replicas | Learn Apache Kafka (conduktor.io) Sync & Async Producer Sync - ACK=0 (fire and forgot) - ACK=1 (only leader) - ACK=all (all min.insync.replicas) Async - Support callback (specify number of request max.in.flight.requests.per.connec tion,and retries flag) 44
  • 45. Commit sync & async consumer commitSync Commit message แลวจะ Block I/O รอผลจากการ commit จะจัดการ Error ได งายกวา commitAsync Commit message แลวจะไม Block I/O ทําใหจัดการ Error ไดยากกวา หากตองการจัดการ Error ใชผาน callback function ได 45
  • 46. reference: Kafka Producer Batching | Learn Apache Kafka with Conduktor Batch.size and linger.ms ⬆ batch.size ⬆ Thoughtput ⬆ Latency 46
  • 47. KRaft, KIP-500 (1) reference: The Apache Kafka Control Plane ­ ZooKeeper vs. KRaft - YouTube 47
  • 48. reference: The Apache Kafka Control Plane ­ ZooKeeper vs. KRaft - YouTube KRaft, KIP-500 (2) 48
  • 49. Before Avro, CSV + อานงาย + Parse งาย + เขาใจงาย =========== - ไมมี Data type กํากับ - ถาขอมูลมี , อยูงานงอก - name,,ID ตองหาวิธีแยก Empty กับ Null 49
  • 50. Before Avro, Relational Database + มี Data type + อยูในรูป Table =========== - Data type อาจจะไมเหมือนกันถา DB engine ตางกัน - ตองมองขอมูลในรูป Row ของ Table 50
  • 51. Before Avro, JSON + ขอมูลอยูในรูป nested JSON ซอน กันได + อยูในรูป Array ได + สามารถใชไดกับเกือบทุกภาษา + เปนที่นิยม + easily to share over network =========== - ไมมี Schema ที่ชัดเจน (อาจทําให ขอมูลเดิมพัง) - ตองใช Key ทุกครั้ง ทําใหเสีย Bandwidth / Storage เยอะ 51
  • 52. + กําหนด Schema ดวย JSON + มี Data type + Auto compression + อานขามภาษาได + Schema เปลี่ยนแปลงได (verify backward compatibility ได) + เหมาะกับการเขียน (มากกวา Parquet) + Support Hive =========== - อาจจะไมมี Support ในภาษาบางภาษา - เนื่องจากมัน compressed อยู ตองใชเครื่องมืออาน Avro 52
  • 53. Avro compatibility reference: Schema Evolution and Compatibility | Confluent Documentation 53
  • 54. Delivery semantic for consumer (1) > At most once: offsets are committed as soon as the message is received. If the processing goes wrong, the message will be lost (it won’t be read again). reference: Delivery Semantics for Kafka Consumers | Learn Apache Kafka (conduktor.io) enable.auto.commit=true auto.commit.interval.ms=5000 (5s) 54
  • 55. Delivery semantic for consumer (2) > At least once: offsets are committed after the message is processed. If the processing goes wrong, the message will be read again. This can result in duplicate processing of messages. Make sure your processing is idempotent (i.e. processing again the messages won’t impact your systems) reference: Delivery Semantics for Kafka Consumers | Learn Apache Kafka (conduktor.io) enable.auto.commit=true auto.commit.interval.ms=5000 (5s) 55
  • 56. Delivery semantic for consumer (3) > Exactly once: Every message is guaranteed to be persisted in Kafka exactly once without any duplicates and data loss even where there is a broker failure or producer retry. reference: Exactly-Once Processing in Kafka explained | by sudan | Medium Producer enable.idempotence=true transactional.id=<id> * Consumer enable.auto.commit=false isolation.level=read_committed (* is optional) 56
  • 57. Topic design > อยาใช Topic เดียวทําทุกอยาง > Consumer อานขอมูลจาก Partition leader ถา leader อยูเครื่องเดียวกันเยอะ อาจมีปญหา Performance > ควรใช Data schema แบบเดิม / ไม break backward-compat > ตองการ Order ควรให Message ตก Partition เดียวกัน > ออกแบบ Key ดีไดทั้ง Ordering และ Performance > พิจารณาแยก Topic ในกรณีที่ตองการ Throughput สูงๆ 57
  • 58. Software architecture design reference: Disaster Recovery for Multi-Region Kafka at Uber - Uber Engineering Blog 58
  • 62. Segment reference: Log Compacted Topics in Apache Kafka | by Seyed Morteza Mousavi | Towards Data Science Segment Active Segment 62
  • 63. Broker config for production > Recommended Java 8, 11 > Memory at least 8 GiB > Change log.dirs (default is temp dir) > At least 3 broker (Replication Factor 3) > auto.create.topics.enable=false > change num.partitions to expect consumer 63
  • 64. Log compaction (changelog topic) reference: Log Compacted Topics in Apache Kafka | by Seyed Morteza Mousavi | Towards Data Science 64
  • 68. KSteam overview reference: Kafka Streams Overview | Confluent Documentation 68
  • 69. KSteam benefit reference: Kafka Streams Overview | Confluent Documentation > Makes your applications highly scalable, elastic, distributed, fault-tolerant > Supports exactly-once processing semantics > Stateful and stateless processing > Event-time processing with windowing, joins, aggregations > Supports Kafka Streams Interactive Queries to unify the worlds of streams and databases > Choose between a declarative, functional API and a lower-level imperative API for maximum control and flexibility 69
  • 70. KTable reference: Streams Concepts | Confluent Documentation > KSteam is a KTable duality 70
  • 71. Kafka connect > Data Centric Pipeline ­ Connect uses meaningful data abstractions to pull or push data. > Flexibility reference: Kafka Streams Overview | Confluent Documentation > Scalability > Reusability > Extensibility => File sync => Database Sync 71
  • 73. Security In-transit At rest > GSSAPI Kerberos authentication > PLAIN Username/password authentication > SCRAM-SHA-256 and SCRAM-SHA-512 Username/password authentication > OAUTHBEARER Authentication using OAuth No out of the box solution. reference: Encrypting Kafka messages at rest to secure applications | Kafka Summit Europe 2021 - Confluent 73
  • 74. SSL reference: Apache Kafka Security 101 | Confluent 74