SlideShare a Scribd company logo
#JCConf
Apache Kafka

A high-throughput distributed messaging
system
陸振恩 (popcorny)
popcorny@cacafly.com
#JCConf
● What is Kafka
● Basic concept
● Why Kafka fast
● Programming Kafka
● Using scenarios
● Recap
Outline
2
#JCConf
What is Kafka
#JCConf
● More and more data and metrics need to be
collected
- Web activity tracking
- Operation metrics
- Application log aggregation
- Commit log
- …
● We need a message bus to collect and relay these
data
- Big Volume
- Fast and Scalable
Motivation
4
#JCConf
● Developed by Linkedin
● Message System
- Queue
- Pub / Sub
● Written in Scala
● Features
- Durability
- Scalability
- High Availability
- High Throughput
Kafka
5
#JCConf
BigData World
6
Traditional BigData
File System NFS HDFS, S3
Database RDBMS Cassandra, HBase
Batch
Processing
SQL
Hadoop MapReduce

Spark
Stream
Processing
In-App Processing
Strom, 

Spark Streaming
Message
Service
AMQP-compliant Kafka
#JCConf
● Durability
- All messages are persisted
- Sequential read & write (like log file)
- Consumers keep the message offset (like file
descriptor)
- The log files are rotated (like logrotate)
- Messages are only deleted on expired. (like
logrotate)
- Support Batch Load and Real Time Usage!
• cat access.log | grep ‘jcconf’
● tail -F access.log | grep ‘jcconf’
Features
7
#JCConf
Design like Message Queue
Implementation like Distributed Log File
8
#JCConf
● Scalability
- Horizontal scale out
- Topic is partitioned (sharded)
● High Availability
- Partition can be replicated
Features
9
#JCConf
● High Throughput
Features
10
source: http://www.infoq.com/articles/apache-kafka
#JCConf
Basic Concept
#JCConf
● Producer - The role to send message to broker
● Consumer -The role to receive message from broker
● Broker - One node of Kafka cluster.
● ZooKeeper - Coordinator of Kafka cluster and costumer
groups.
Kafka Cluster
Physical Components
12
Producer BroakerBroaker
Broaker
Zookeeper
Consumer Group
Consumer
#JCConf
● Topic!
- The named destination of
partition.
● Partition
- One Topic can have multiple
partition
- Unit of parallelism
● Message!
• Key/value pair
• Message offset
Logical Components
Topic
B
Partition 2
0 1
E
2
F
3
M
4
N
5
Q
6
R
7
S
8
Y
9
b
C
Partition 3
0 1
D
2
K
3
L
4
O
5
P
6
T
7
U
A
Partition 1
0 1
G
2
H
3
I
4
J
5
V
6
W
7
X
8
c
13
#JCConf
One Partition One Consumer 

(Queue)
P CA
Partition 1
0 1
B
2
C
3
D
4
E
5
F
6
G
7
H
8
I
9
J
offset = 8
14
Consumers keep the offset.

Broker has no idea about if message is proceeded
#JCConf
One Partition Multiple Consumer 

(Pub/Sub)
P A
Partition 1
0 1
B
2
C
3
D
4
E
5
F
6
G
7
H
8
I
9
J
C1
C2
C3
offset = 8
offset = 7
offset = 9
15
Each Consumer keep its own offset.
#JCConf
broker2
Multiple Partitions
broker1
P
A
Partition 1
0 1
G
2
H
3
I
4
J
5
V
6
W
7
X
8
c
B
Partition 2
0 1
E
2
F
3
M
4
N
5
Q
6
R
7
S
8
Y
9
b
C
Partition 3
0 1
D
2
K
3
L
4
O
5
P
6
T
7
U
16
C1
p1.offset = 7
p2.offset = 9
p3.offset = 7
Dispatched by hashed key
#JCConf
broker2
Multiple Partitions
broker1
P
A
Partition 1
0 1
G
2
H
3
I
4
J
5
V
6
W
7
X
8
c
B
Partition 2
0 1
E
2
F
3
M
4
N
5
Q
6
R
7
S
8
Y
9
b
C
Partition 3
0 1
D
2
K
3
L
4
O
5
P
6
T
7
U
17
C2
offset = 9
offset = 7
C3
offset = 7
C1
#JCConf
Can we auto-rebalance the consumers
to partitions?
18
Yes, Consumer Group!!
#JCConf
● A group of workers
● Share the offsets
● Offsets are synced to ZooKeeper
● Auto Rebalancing
Consumer Group
19
#JCConf
Consumer Group
20
broker2
broker1
P
A
Partition 1
0 1
G
2
H
3
I
4
J
5
V
6
W
7
X
8
c
B
Partition 2
0 1
E
2
F
3
M
4
N
5
Q
6
R
7
S
8
Y
9
b
C
Partition 3
0 1
D
2
K
3
L
4
O
5
P
6
T
7
U
Consumer Group
‘group1’
C2
p1.offset = 7
p2.offset = 9
p3.offset = 7
C1
#JCConf
Consumer Group
21
broker2
broker1
P
A
Partition 1
0 1
G
2
H
3
I
4
J
5
V
6
W
7
X
8
c
B
Partition 2
0 1
E
2
F
3
M
4
N
5
Q
6
R
7
S
8
Y
9
b
C
Partition 3
0 1
D
2
K
3
L
4
O
5
P
6
T
7
U
’group1’
C2
C1
C1
’group2’
#JCConf
Consumer Group
P A
Partition 1
0 1
B
2
C
3
D
4
E
5
F
6
G
7
H
8
I
9
J
C1
C2
C3
offset = 9
Consumer Group
22
Partition to Consumer is Many to One relation (In One
Consumer Group)
#JCConf
● Messages from the same partition guarantee FIFO
semantic
● Traditional MQ can only guarantee message are
delivered in order
● Kafka can guarantee messages are handled in order (for
same partition)
Message Ordering
23
P B
C1
C2
P
P1 C1
C2P2
Traditional MQ Kafka
#JCConf
● At most once - Messages may be lost but are
never redelivered.
● At least once - Messages are never lost but may
be redelivered.
● Exactly once - each message is delivered once
and only once. (this is what people actually want)
- Two-Phase Commit
- At least once + Idempotence
Delivery Semantic
24
Apply multiple times without changing the final result
#JCConf
● Which part do we discuss?
Delivery Semantic
25
Producer Broker Consumer
Producer Broker Consumer
#JCConf
● At most once - Async send
● At least once - Sync send (with retry count)
" Exactly once!
- Idempotent delivery does not support until next
version (0.9)
Producer To Broker
26
Producer Broker Consumer
#JCConf
● At most once - Store the offset before handling the
message
● At least once - Store the offset after handling the
message
● Exactly once - At least once + Idempotent
operation
Broker to Consumer
27
Producer Broker Consumer
#JCConf
● The unit of replication is the partition!
● Each partition has a single leader and zero or more
followers
● All reads and writes go to the leader of the partition
Replication
28
source: http://www.infoq.com/articles/apache-kafka
Leader FollowerFollower
Producer Consumer
sync sync
write read
#JCConf29
#JCConf
● Many data system retain a latest state for data by
some key.
● Log compaction adds an alternative retention
mechanism, log compaction, to support retaining
messages by key instead of purely by time.
● This would describe both many common data
systems — a search index, a cache, etc
Log Compaction
30
#JCConf
Log Compaction
31
#JCConf
Log Compaction
32
#JCConf
Why Kafka Fast?
#JCConf34
Persistence and Fast?
#JCConf
● Don’t fear file system
● Six 7,200 RPM SATA RAID-5 array
- Sequential write: 600MB/sec
- Random write: 100K/sec
● Sequential read in disk faster than random access in memory?
Sequential vs Random
35
source: http://queue.acm.org/detail.cfm?id=1563874
#JCConf
If we persist data, should we cache
the data in memory?
36
#JCConf
● In-Process Cache
- Message as object
- Cache in JVM heap.
● Page Cache
- Disk cache by OS
In-Process Cache vs Page Cache
37
#JCConf
In-Process Cache vs Page Cache
38
In Process Cache Disk Page Cache
Memory
Usage
In-heap memory Free Memory
Overhead Object overhead No
Garbage
Collection
Yes No
Process
Restart
Lost Still Warm
Controled
by
App OS
#JCConf
● Fact
- All disk reads and writes will go through page
cache. This feature cannot easily be turned off
without using direct I/O, so even if a process
maintains an in-process cache of the data, this
data will likely be duplicated in OS pagecache,
effectively storing everything twice.
● Conclusion
- Relying on pagecache is superior to maintaining
an in-process cache or other structure
In-Process Cache vs Page Cache
39
#JCConf
How to transfer to consumers?
40
#JCConf
Application Copy vs Zero Copying
41
#JCConf
● Traditional Queue
- Broker keep the message state and metadata
- B-Tree O(log n)
- Random Access
● Kafka
- Consumers keep the offset
- Sequential Disk Read/Write O(1)
Constant Time
42
#JCConf
Programming Kafka
#JCConf
Producer
44
Sync Send
#JCConf
Producer
45
Async Send
#JCConf
High Level Consumer
46
Open The Consumer Connector
Open the stream for topic
#JCConf
High Level Consumer
47
Receive the message
#JCConf
Using Scenarios
#JCConf
● Realtime processing and analyzing
● Stream processing frameworks
- Strom
- Spark Streaming
- Samza
● Distributed stream source + Distributed stream
processing
● All these three frameworks support Kafka as stream
source.
Source of Stream Processing
49
Kafka
Cluster
Stream
Processing
#JCConf
● The most reliable source for stream processing
Source of Stream Processing
50
source: http://www.slideshare.net/ptgoetz/apache-storm-vs-spark-streaming
#JCConf
● Centralized Log Framework
● Distributed Log Collectors
- Logstash
- Fluentd
- Flume
Source and/or Sink of Distributed Log
Collectors
51
Kafka
Cluster
Distributed Log 

Collector
Other
Sink
Kafka
Cluster
Distributed Log 

Collector
Other
Source
#JCConf
● Push vs Pull











● Distributed Log Collector provide Configurable
producer and consumer
● Kafka Cluster provide distributed, high availability,
reliable message system
Source and/or Sink of Distributed Log
Collectors (cont.)
52
Distributed Log 

Collector
Kafka Cluster
pull
pull
push
push
#JCConf
● What is lambda architecture?
- Stream for realtime data
- Batch for historical data
- Query by merged view.
Source of Lambda Architecture
53
source: http://lambda-architecture.net/
#JCConf
Lambda Architecture (cont.)
54
source: https://metamarkets.com/2014/building-a-data-pipeline-that-handles-billions-of-events-in-real-time/
#JCConf
● Features
- Durability
- Scalability
- High Availability
- High Throughput
● Basic Concept
- Producer, Broker, Consumer, Consumer Group
- Topic, Partition, Message
- Message Ordering
- Delivery Semantic
- Replication
● Why Kafka fast
● Using Scenarios
- Source of stream processing
- Source or sink of distributed log framework
- Source of lambda architecture
Recap
55
#JCConf
● Kafka Documentation

kafka.apache.org/documentation.html
● Kafka Wiki

https://cwiki.apache.org/confluence/display/KAFKA/Index
● The Log: What every software engineer should know about real-
time data's unifying abstraction

engineering.linkedin.com/distributed-systems/log-what-every-software-
engineer-should-know-about-real-time-datas-unifying
● Benchmarking Apache Kafka: 2 Million Writes Per Second (On
Three Cheap Machines)

engineering.linkedin.com/kafka/benchmarking-apache-kafka-2-million-
writes-second-three-cheap-machines
● Apache Kafka for Beginners

blog.cloudera.com/blog/2014/09/apache-kafka-for-beginners/
Reference
56
#JCConf57
producer.send(“thanks”);
#JCConf
// any question?

question = consumer.receive();
58

More Related Content

What's hot (20)

PPTX
kafka
Amikam Snir
 
PDF
Kafka 101 and Developer Best Practices
confluent
 
PDF
An Introduction to Apache Kafka
Amir Sedighi
 
PDF
The Patterns of Distributed Logging and Containers
SATOSHI TAGOMORI
 
PDF
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
The Hive
 
PDF
Cloud arch patterns
Corey Huinker
 
PDF
Introduction to Kafka Streams
Guozhang Wang
 
PDF
Introduction to apache kafka
Dimitris Kontokostas
 
PDF
Fundamentals of Apache Kafka
Chhavi Parasher
 
KEY
Introduction to memcached
Jurriaan Persyn
 
PDF
Best Practices For Workflow
Timothy Spann
 
PDF
ksqlDB: A Stream-Relational Database System
confluent
 
PPTX
Autoscaling Flink with Reactive Mode
Flink Forward
 
PPTX
HBase and HDFS: Understanding FileSystem Usage in HBase
enissoz
 
PPTX
One sink to rule them all: Introducing the new Async Sink
Flink Forward
 
PDF
Hello, kafka! (an introduction to apache kafka)
Timothy Spann
 
PPTX
HCL Domino V12 Key Security Features Overview
hemantnaik
 
PDF
Apache kafka
NexThoughts Technologies
 
PDF
Unlocking the Power of Apache Flink: An Introduction in 4 Acts
HostedbyConfluent
 
PDF
카프카(kafka) 성능 테스트 환경 구축 (JMeter, ELK)
Hyunmin Lee
 
Kafka 101 and Developer Best Practices
confluent
 
An Introduction to Apache Kafka
Amir Sedighi
 
The Patterns of Distributed Logging and Containers
SATOSHI TAGOMORI
 
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
The Hive
 
Cloud arch patterns
Corey Huinker
 
Introduction to Kafka Streams
Guozhang Wang
 
Introduction to apache kafka
Dimitris Kontokostas
 
Fundamentals of Apache Kafka
Chhavi Parasher
 
Introduction to memcached
Jurriaan Persyn
 
Best Practices For Workflow
Timothy Spann
 
ksqlDB: A Stream-Relational Database System
confluent
 
Autoscaling Flink with Reactive Mode
Flink Forward
 
HBase and HDFS: Understanding FileSystem Usage in HBase
enissoz
 
One sink to rule them all: Introducing the new Async Sink
Flink Forward
 
Hello, kafka! (an introduction to apache kafka)
Timothy Spann
 
HCL Domino V12 Key Security Features Overview
hemantnaik
 
Unlocking the Power of Apache Flink: An Introduction in 4 Acts
HostedbyConfluent
 
카프카(kafka) 성능 테스트 환경 구축 (JMeter, ELK)
Hyunmin Lee
 

Viewers also liked (20)

PDF
Gradle起步走: 以CLI Application為例 @ JCConf 2014
Chen-en Lu
 
PDF
Cassandra 2.1 簡介
Cloud Tu
 
PDF
java8-patterns
Justin Lin
 
PDF
淺談 Geb 網站自動化測試(JCConf 2014)
Kyle Lin
 
PPTX
Apache kafka
Viswanath J
 
PPTX
Apache kafka
Rahul Jain
 
PDF
Kafka as Message Broker
Haluan Irsad
 
PPTX
Introduction to Kafka and Zookeeper
Rahul Jain
 
PPTX
Building a robot with the .Net Micro Framework
Ducas Francis
 
PPT
Messaging
rbpasker
 
PDF
Event Driven Architectures with Apache Kafka on Heroku
Heroku
 
PPTX
Kafka overview and use cases
Indrajeet Kumar
 
PPTX
From Java Stream to Java DataFrame
Chen-en Lu
 
PDF
IBM MQ: Using Publish/Subscribe in an MQ Network
David Ware
 
PPT
Apache Kafka Reliability Guarantees StrataHadoop NYC 2015
Jeff Holoman
 
PPTX
Kafka Reliability Guarantees ATL Kafka User Group
Jeff Holoman
 
PPT
Advanced Pattern Authoring with WebSphere Message Broker
Ant Phillips
 
PPT
Effective Application Development with WebSphere Message Broker
Ant Phillips
 
PPT
Introduction to Patterns in WebSphere Message Broker
Ant Phillips
 
DOCX
Dm cv
dennis mangabat
 
Gradle起步走: 以CLI Application為例 @ JCConf 2014
Chen-en Lu
 
Cassandra 2.1 簡介
Cloud Tu
 
java8-patterns
Justin Lin
 
淺談 Geb 網站自動化測試(JCConf 2014)
Kyle Lin
 
Apache kafka
Viswanath J
 
Apache kafka
Rahul Jain
 
Kafka as Message Broker
Haluan Irsad
 
Introduction to Kafka and Zookeeper
Rahul Jain
 
Building a robot with the .Net Micro Framework
Ducas Francis
 
Messaging
rbpasker
 
Event Driven Architectures with Apache Kafka on Heroku
Heroku
 
Kafka overview and use cases
Indrajeet Kumar
 
From Java Stream to Java DataFrame
Chen-en Lu
 
IBM MQ: Using Publish/Subscribe in an MQ Network
David Ware
 
Apache Kafka Reliability Guarantees StrataHadoop NYC 2015
Jeff Holoman
 
Kafka Reliability Guarantees ATL Kafka User Group
Jeff Holoman
 
Advanced Pattern Authoring with WebSphere Message Broker
Ant Phillips
 
Effective Application Development with WebSphere Message Broker
Ant Phillips
 
Introduction to Patterns in WebSphere Message Broker
Ant Phillips
 
Ad

Similar to Apache Kafka: A high-throughput distributed messaging system @ JCConf 2014 (20)

PPT
Apache kafka- Onkar Kadam
Onkar Kadam
 
PDF
Apache Kafka Introduction
Amita Mirajkar
 
PPTX
Kafka and ibm event streams basics
Brian S. Paskin
 
PPTX
Kafka 0.8.0 Presentation to Atlanta Java User's Group March 2013
Christopher Curtin
 
PPTX
Apache kafka
Ramakrishna kapa
 
PPTX
Introduction to Kafka
Ducas Francis
 
PPTX
kafka_session_updated.pptx
Koiuyt1
 
PPTX
Kafka tutorial
Srikrishna k
 
PPTX
Apache kafka
Srikrishna k
 
PPTX
Distributed messaging through Kafka
Dileep Kalidindi
 
PPTX
Top Ten Kafka® Configs
confluent
 
PDF
RabbitMQ vs Apache Kafka - Part 1
Erlang Solutions
 
PDF
Kafka 10000 feet view
younessx01
 
PDF
Non-Kafkaesque Apache Kafka - Yottabyte 2018
Otávio Carvalho
 
PDF
apachekafka-160907180205.pdf
TarekHamdi8
 
PPTX
Apache kafka
Srikrishna k
 
KEY
Data Models and Consumer Idioms Using Apache Kafka for Continuous Data Stream...
Erik Onnen
 
PDF
Messaging queue - Kafka
Mayank Bansal
 
PDF
Deep dive into Apache Kafka consumption
Alexandre Tamborrino
 
PDF
Event driven-arch
Mohammed Shoaib
 
Apache kafka- Onkar Kadam
Onkar Kadam
 
Apache Kafka Introduction
Amita Mirajkar
 
Kafka and ibm event streams basics
Brian S. Paskin
 
Kafka 0.8.0 Presentation to Atlanta Java User's Group March 2013
Christopher Curtin
 
Apache kafka
Ramakrishna kapa
 
Introduction to Kafka
Ducas Francis
 
kafka_session_updated.pptx
Koiuyt1
 
Kafka tutorial
Srikrishna k
 
Apache kafka
Srikrishna k
 
Distributed messaging through Kafka
Dileep Kalidindi
 
Top Ten Kafka® Configs
confluent
 
RabbitMQ vs Apache Kafka - Part 1
Erlang Solutions
 
Kafka 10000 feet view
younessx01
 
Non-Kafkaesque Apache Kafka - Yottabyte 2018
Otávio Carvalho
 
apachekafka-160907180205.pdf
TarekHamdi8
 
Apache kafka
Srikrishna k
 
Data Models and Consumer Idioms Using Apache Kafka for Continuous Data Stream...
Erik Onnen
 
Messaging queue - Kafka
Mayank Bansal
 
Deep dive into Apache Kafka consumption
Alexandre Tamborrino
 
Event driven-arch
Mohammed Shoaib
 
Ad

Recently uploaded (20)

PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 

Apache Kafka: A high-throughput distributed messaging system @ JCConf 2014

  • 1. #JCConf Apache Kafka
 A high-throughput distributed messaging system 陸振恩 (popcorny) popcorny@cacafly.com
  • 2. #JCConf ● What is Kafka ● Basic concept ● Why Kafka fast ● Programming Kafka ● Using scenarios ● Recap Outline 2
  • 4. #JCConf ● More and more data and metrics need to be collected - Web activity tracking - Operation metrics - Application log aggregation - Commit log - … ● We need a message bus to collect and relay these data - Big Volume - Fast and Scalable Motivation 4
  • 5. #JCConf ● Developed by Linkedin ● Message System - Queue - Pub / Sub ● Written in Scala ● Features - Durability - Scalability - High Availability - High Throughput Kafka 5
  • 6. #JCConf BigData World 6 Traditional BigData File System NFS HDFS, S3 Database RDBMS Cassandra, HBase Batch Processing SQL Hadoop MapReduce
 Spark Stream Processing In-App Processing Strom, 
 Spark Streaming Message Service AMQP-compliant Kafka
  • 7. #JCConf ● Durability - All messages are persisted - Sequential read & write (like log file) - Consumers keep the message offset (like file descriptor) - The log files are rotated (like logrotate) - Messages are only deleted on expired. (like logrotate) - Support Batch Load and Real Time Usage! • cat access.log | grep ‘jcconf’ ● tail -F access.log | grep ‘jcconf’ Features 7
  • 8. #JCConf Design like Message Queue Implementation like Distributed Log File 8
  • 9. #JCConf ● Scalability - Horizontal scale out - Topic is partitioned (sharded) ● High Availability - Partition can be replicated Features 9
  • 10. #JCConf ● High Throughput Features 10 source: http://www.infoq.com/articles/apache-kafka
  • 12. #JCConf ● Producer - The role to send message to broker ● Consumer -The role to receive message from broker ● Broker - One node of Kafka cluster. ● ZooKeeper - Coordinator of Kafka cluster and costumer groups. Kafka Cluster Physical Components 12 Producer BroakerBroaker Broaker Zookeeper Consumer Group Consumer
  • 13. #JCConf ● Topic! - The named destination of partition. ● Partition - One Topic can have multiple partition - Unit of parallelism ● Message! • Key/value pair • Message offset Logical Components Topic B Partition 2 0 1 E 2 F 3 M 4 N 5 Q 6 R 7 S 8 Y 9 b C Partition 3 0 1 D 2 K 3 L 4 O 5 P 6 T 7 U A Partition 1 0 1 G 2 H 3 I 4 J 5 V 6 W 7 X 8 c 13
  • 14. #JCConf One Partition One Consumer 
 (Queue) P CA Partition 1 0 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J offset = 8 14 Consumers keep the offset.
 Broker has no idea about if message is proceeded
  • 15. #JCConf One Partition Multiple Consumer 
 (Pub/Sub) P A Partition 1 0 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J C1 C2 C3 offset = 8 offset = 7 offset = 9 15 Each Consumer keep its own offset.
  • 16. #JCConf broker2 Multiple Partitions broker1 P A Partition 1 0 1 G 2 H 3 I 4 J 5 V 6 W 7 X 8 c B Partition 2 0 1 E 2 F 3 M 4 N 5 Q 6 R 7 S 8 Y 9 b C Partition 3 0 1 D 2 K 3 L 4 O 5 P 6 T 7 U 16 C1 p1.offset = 7 p2.offset = 9 p3.offset = 7 Dispatched by hashed key
  • 17. #JCConf broker2 Multiple Partitions broker1 P A Partition 1 0 1 G 2 H 3 I 4 J 5 V 6 W 7 X 8 c B Partition 2 0 1 E 2 F 3 M 4 N 5 Q 6 R 7 S 8 Y 9 b C Partition 3 0 1 D 2 K 3 L 4 O 5 P 6 T 7 U 17 C2 offset = 9 offset = 7 C3 offset = 7 C1
  • 18. #JCConf Can we auto-rebalance the consumers to partitions? 18 Yes, Consumer Group!!
  • 19. #JCConf ● A group of workers ● Share the offsets ● Offsets are synced to ZooKeeper ● Auto Rebalancing Consumer Group 19
  • 20. #JCConf Consumer Group 20 broker2 broker1 P A Partition 1 0 1 G 2 H 3 I 4 J 5 V 6 W 7 X 8 c B Partition 2 0 1 E 2 F 3 M 4 N 5 Q 6 R 7 S 8 Y 9 b C Partition 3 0 1 D 2 K 3 L 4 O 5 P 6 T 7 U Consumer Group ‘group1’ C2 p1.offset = 7 p2.offset = 9 p3.offset = 7 C1
  • 21. #JCConf Consumer Group 21 broker2 broker1 P A Partition 1 0 1 G 2 H 3 I 4 J 5 V 6 W 7 X 8 c B Partition 2 0 1 E 2 F 3 M 4 N 5 Q 6 R 7 S 8 Y 9 b C Partition 3 0 1 D 2 K 3 L 4 O 5 P 6 T 7 U ’group1’ C2 C1 C1 ’group2’
  • 22. #JCConf Consumer Group P A Partition 1 0 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J C1 C2 C3 offset = 9 Consumer Group 22 Partition to Consumer is Many to One relation (In One Consumer Group)
  • 23. #JCConf ● Messages from the same partition guarantee FIFO semantic ● Traditional MQ can only guarantee message are delivered in order ● Kafka can guarantee messages are handled in order (for same partition) Message Ordering 23 P B C1 C2 P P1 C1 C2P2 Traditional MQ Kafka
  • 24. #JCConf ● At most once - Messages may be lost but are never redelivered. ● At least once - Messages are never lost but may be redelivered. ● Exactly once - each message is delivered once and only once. (this is what people actually want) - Two-Phase Commit - At least once + Idempotence Delivery Semantic 24 Apply multiple times without changing the final result
  • 25. #JCConf ● Which part do we discuss? Delivery Semantic 25 Producer Broker Consumer Producer Broker Consumer
  • 26. #JCConf ● At most once - Async send ● At least once - Sync send (with retry count) " Exactly once! - Idempotent delivery does not support until next version (0.9) Producer To Broker 26 Producer Broker Consumer
  • 27. #JCConf ● At most once - Store the offset before handling the message ● At least once - Store the offset after handling the message ● Exactly once - At least once + Idempotent operation Broker to Consumer 27 Producer Broker Consumer
  • 28. #JCConf ● The unit of replication is the partition! ● Each partition has a single leader and zero or more followers ● All reads and writes go to the leader of the partition Replication 28 source: http://www.infoq.com/articles/apache-kafka Leader FollowerFollower Producer Consumer sync sync write read
  • 30. #JCConf ● Many data system retain a latest state for data by some key. ● Log compaction adds an alternative retention mechanism, log compaction, to support retaining messages by key instead of purely by time. ● This would describe both many common data systems — a search index, a cache, etc Log Compaction 30
  • 35. #JCConf ● Don’t fear file system ● Six 7,200 RPM SATA RAID-5 array - Sequential write: 600MB/sec - Random write: 100K/sec ● Sequential read in disk faster than random access in memory? Sequential vs Random 35 source: http://queue.acm.org/detail.cfm?id=1563874
  • 36. #JCConf If we persist data, should we cache the data in memory? 36
  • 37. #JCConf ● In-Process Cache - Message as object - Cache in JVM heap. ● Page Cache - Disk cache by OS In-Process Cache vs Page Cache 37
  • 38. #JCConf In-Process Cache vs Page Cache 38 In Process Cache Disk Page Cache Memory Usage In-heap memory Free Memory Overhead Object overhead No Garbage Collection Yes No Process Restart Lost Still Warm Controled by App OS
  • 39. #JCConf ● Fact - All disk reads and writes will go through page cache. This feature cannot easily be turned off without using direct I/O, so even if a process maintains an in-process cache of the data, this data will likely be duplicated in OS pagecache, effectively storing everything twice. ● Conclusion - Relying on pagecache is superior to maintaining an in-process cache or other structure In-Process Cache vs Page Cache 39
  • 40. #JCConf How to transfer to consumers? 40
  • 41. #JCConf Application Copy vs Zero Copying 41
  • 42. #JCConf ● Traditional Queue - Broker keep the message state and metadata - B-Tree O(log n) - Random Access ● Kafka - Consumers keep the offset - Sequential Disk Read/Write O(1) Constant Time 42
  • 46. #JCConf High Level Consumer 46 Open The Consumer Connector Open the stream for topic
  • 49. #JCConf ● Realtime processing and analyzing ● Stream processing frameworks - Strom - Spark Streaming - Samza ● Distributed stream source + Distributed stream processing ● All these three frameworks support Kafka as stream source. Source of Stream Processing 49 Kafka Cluster Stream Processing
  • 50. #JCConf ● The most reliable source for stream processing Source of Stream Processing 50 source: http://www.slideshare.net/ptgoetz/apache-storm-vs-spark-streaming
  • 51. #JCConf ● Centralized Log Framework ● Distributed Log Collectors - Logstash - Fluentd - Flume Source and/or Sink of Distributed Log Collectors 51 Kafka Cluster Distributed Log 
 Collector Other Sink Kafka Cluster Distributed Log 
 Collector Other Source
  • 52. #JCConf ● Push vs Pull
 
 
 
 
 
 ● Distributed Log Collector provide Configurable producer and consumer ● Kafka Cluster provide distributed, high availability, reliable message system Source and/or Sink of Distributed Log Collectors (cont.) 52 Distributed Log 
 Collector Kafka Cluster pull pull push push
  • 53. #JCConf ● What is lambda architecture? - Stream for realtime data - Batch for historical data - Query by merged view. Source of Lambda Architecture 53 source: http://lambda-architecture.net/
  • 54. #JCConf Lambda Architecture (cont.) 54 source: https://metamarkets.com/2014/building-a-data-pipeline-that-handles-billions-of-events-in-real-time/
  • 55. #JCConf ● Features - Durability - Scalability - High Availability - High Throughput ● Basic Concept - Producer, Broker, Consumer, Consumer Group - Topic, Partition, Message - Message Ordering - Delivery Semantic - Replication ● Why Kafka fast ● Using Scenarios - Source of stream processing - Source or sink of distributed log framework - Source of lambda architecture Recap 55
  • 56. #JCConf ● Kafka Documentation
 kafka.apache.org/documentation.html ● Kafka Wiki
 https://cwiki.apache.org/confluence/display/KAFKA/Index ● The Log: What every software engineer should know about real- time data's unifying abstraction
 engineering.linkedin.com/distributed-systems/log-what-every-software- engineer-should-know-about-real-time-datas-unifying ● Benchmarking Apache Kafka: 2 Million Writes Per Second (On Three Cheap Machines)
 engineering.linkedin.com/kafka/benchmarking-apache-kafka-2-million- writes-second-three-cheap-machines ● Apache Kafka for Beginners
 blog.cloudera.com/blog/2014/09/apache-kafka-for-beginners/ Reference 56
  • 58. #JCConf // any question?
 question = consumer.receive(); 58