Something about Kafka - Why Kafka is so fast

98
Something about Kafka Frank Yao @超杯摩卡星冰乐 2013-06-29 13年7月5星期五

description

This slide briefly introduced the reason why kafka is so fast in performance.

Transcript of Something about Kafka - Why Kafka is so fast

Page 1: Something about Kafka - Why Kafka is so fast

Something about KafkaFrank Yao

@超⼤大杯摩卡星冰乐2013-06-29

13年7月5⽇日星期五

Page 2: Something about Kafka - Why Kafka is so fast

Agenda

• WHAT is Kafka?

• HOW we use it in Vipshop?

• WHY Kafka is so ‘fast’?

13年7月5⽇日星期五

Page 3: Something about Kafka - Why Kafka is so fast

WHAT is Kafka?

13年7月5⽇日星期五

Page 4: Something about Kafka - Why Kafka is so fast

WHAT

• “Kafka is a messaging system that was originally developed at LinkedIn to serve as the foundation for LinkedIn's activity stream and operational data processing pipeline.”

13年7月5⽇日星期五

Page 5: Something about Kafka - Why Kafka is so fast

User cases

• Operational monitoring: real-time, heads-up monitoring

• Reporting and Batch processing: load data into a data warehouse or Hadoop system

13年7月5⽇日星期五

Page 6: Something about Kafka - Why Kafka is so fast

Performance(Below test is from Kafka website)

• Parameters:

• message size = 200 bytes

• batch size = 200 messages

• fetch size = 1MB

• flush interval = 600 messages

13年7月5⽇日星期五

Page 7: Something about Kafka - Why Kafka is so fast

Batch Size

13年7月5⽇日星期五

Page 8: Something about Kafka - Why Kafka is so fast

Consumer Throughput

13年7月5⽇日星期五

Page 9: Something about Kafka - Why Kafka is so fast

Data Size?

13年7月5⽇日星期五

Page 10: Something about Kafka - Why Kafka is so fast

Producers thread?

13年7月5⽇日星期五

Page 11: Something about Kafka - Why Kafka is so fast

Topic Number?

13年7月5⽇日星期五

Page 12: Something about Kafka - Why Kafka is so fast

Tradition Queue

• ActiveMQ, RabbitMQ...

13年7月5⽇日星期五

Page 13: Something about Kafka - Why Kafka is so fast

13年7月5⽇日星期五

Page 14: Something about Kafka - Why Kafka is so fast

My Test

• Use Flume:

• In/Out ~= 30w message per second

13年7月5⽇日星期五

Page 15: Something about Kafka - Why Kafka is so fast

Kafka in Vipshop

13年7月5⽇日星期五

Page 16: Something about Kafka - Why Kafka is so fast

Data ‘in’ Kafka

• Operational monitoring• Nginx access log• PHP error log, slow log

• Reporting and Batch processing:• Nginx access log• PHP error log, slow log• App log

• b2c• Recommend• Pay• Passport

13年7月5⽇日星期五

Page 17: Something about Kafka - Why Kafka is so fast

How many Data?

• Peak Time(10:00~10:30):

• IN : 15k-20k msg per second

• OUT : 30k-40k msg per second

13年7月5⽇日星期五

Page 18: Something about Kafka - Why Kafka is so fast

13年7月5⽇日星期五

Page 19: Something about Kafka - Why Kafka is so fast

Apps depends on Kakfa

13年7月5⽇日星期五

Page 20: Something about Kafka - Why Kafka is so fast

Kibana(Elasticsearch)

13年7月5⽇日星期五

Page 21: Something about Kafka - Why Kafka is so fast

real-time pv uv

13年7月5⽇日星期五

Page 22: Something about Kafka - Why Kafka is so fast

HDFS

13年7月5⽇日星期五

Page 23: Something about Kafka - Why Kafka is so fast

Load use Kafka

13年7月5⽇日星期五

Page 24: Something about Kafka - Why Kafka is so fast

Replace RabbitMQRabbitMQ Kafka

Servers

Load

Language

Deployment

Client

Management

RabbitMQ Kafka

6 1

>10 <2.5

Erlang Scala

Difficult Easy

A lot Not Many

Web-console JMX

13年7月5⽇日星期五

Page 25: Something about Kafka - Why Kafka is so fast

WHY Kafka ‘fast’

13年7月5⽇日星期五

Page 26: Something about Kafka - Why Kafka is so fast

Basics

• producers

• consumers

• consumer groups

• brokers

13年7月5⽇日星期五

Page 27: Something about Kafka - Why Kafka is so fast

Kafka Arch

13年7月5⽇日星期五

Page 28: Something about Kafka - Why Kafka is so fast

Kafka Arch

13年7月5⽇日星期五

Page 29: Something about Kafka - Why Kafka is so fast

Kafka Deployment

13年7月5⽇日星期五

Page 30: Something about Kafka - Why Kafka is so fast

Major Design Elements

• Persistent messages

• Throughput >>> features

• Consumers hold states

• ALL is distributed

13年7月5⽇日星期五

Page 31: Something about Kafka - Why Kafka is so fast

Detail Agenda• Maximizing Performance

• Filesystem vs. Memory• BTree?• Zero-copy• End-to-end Batch Compression

• Consumer state• Message delivery semantics• Consumer state• Push vs. Pull

• Message• Message format• Disk structure

• Zookeeper• Directory Structure

13年7月5⽇日星期五

Page 32: Something about Kafka - Why Kafka is so fast

Maximize Performance

13年7月5⽇日星期五

Page 33: Something about Kafka - Why Kafka is so fast

Filesystem vs. MemoryMaximize Performance

13年7月5⽇日星期五

Page 34: Something about Kafka - Why Kafka is so fast

Who is fast?

13年7月5⽇日星期五

Page 35: Something about Kafka - Why Kafka is so fast

Memory

Filesystem

13年7月5⽇日星期五

Page 36: Something about Kafka - Why Kafka is so fast

Disk

hardware linear writes random writes

6*7200rpm SATA RAID-5 300MB/sec 50k/sec

13年7月5⽇日星期五

Page 37: Something about Kafka - Why Kafka is so fast

ACM Pieces

13年7月5⽇日星期五

Page 38: Something about Kafka - Why Kafka is so fast

Let’s see something REAL

13年7月5⽇日星期五

Page 39: Something about Kafka - Why Kafka is so fast

Server Stats

13年7月5⽇日星期五

Page 40: Something about Kafka - Why Kafka is so fast

page cache

• use free memory for disk caching to make random write fast

13年7月5⽇日星期五

Page 41: Something about Kafka - Why Kafka is so fast

13年7月5⽇日星期五

Page 42: Something about Kafka - Why Kafka is so fast

Drawbacks

• All disk reads and writes will go through this unified 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.

13年7月5⽇日星期五

Page 43: Something about Kafka - Why Kafka is so fast

If JVM...

13年7月5⽇日星期五

Page 44: Something about Kafka - Why Kafka is so fast

13年7月5⽇日星期五

Page 45: Something about Kafka - Why Kafka is so fast

If we use memory(JVM)

• The memory overhead of objects is very high, often doubling the size of the data stored (or worse).

• Java garbage collection becomes increasingly sketchy and expensive as the in-heap data increases.

13年7月5⽇日星期五

Page 46: Something about Kafka - Why Kafka is so fast

cache size

• at least double the available cache by having automatic access to all free memory, and likely double again by storing a compact byte structure rather than individual objects. Doing so will result in a cache of up to 28-30GB on a 32GB machine.

13年7月5⽇日星期五

Page 47: Something about Kafka - Why Kafka is so fast

comparison

in-disk in-memory

GC

Initialization

Logic

no GC stop the world

stay warm even if restarted

rebuilt slow(10min for 10GB) and cold cache

handle by OS handle by programs

13年7月5⽇日星期五

Page 48: Something about Kafka - Why Kafka is so fast

Conclusion

• using the filesystem and relying on pagecache is superior to maintaining an in-memory cache or other structure

13年7月5⽇日星期五

Page 49: Something about Kafka - Why Kafka is so fast

Go Extreme!

• Write to filesystem DIRECTLY!

• (In effect this just means that it is transferred into the kernel's pagecache where the OS can flush it later.)

13年7月5⽇日星期五

Page 50: Something about Kafka - Why Kafka is so fast

Furthermore

• You can configure: every N messages or every M seconds. It is to put a bound on the amount of data "at risk" in the event of a hard crash.

• Varnish use pagecache-centric design as well.

13年7月5⽇日星期五

Page 51: Something about Kafka - Why Kafka is so fast

BTreeMaximize Performance

13年7月5⽇日星期五

Page 52: Something about Kafka - Why Kafka is so fast

Background

• Messaging system meta is often a BTree.

• BTree operations are O(logN).

13年7月5⽇日星期五

Page 53: Something about Kafka - Why Kafka is so fast

BTree• O(logN) ~= constant time

13年7月5⽇日星期五

Page 54: Something about Kafka - Why Kafka is so fast

BTree is slow on Disk!

13年7月5⽇日星期五

Page 55: Something about Kafka - Why Kafka is so fast

BTree for Disk

• Disk seeks come at 10 ms a pop

• each disk can do only one seek at a time

• parallelism is limited

• the observed performance of tree structures is often super-linear

13年7月5⽇日星期五

Page 56: Something about Kafka - Why Kafka is so fast

Lock

• Page or row locking to avoid lock the tree

13年7月5⽇日星期五

Page 57: Something about Kafka - Why Kafka is so fast

Two Facts

• no advantage of driver density because of the heavy reliance on disk seek

• need small (< 100GB) high RPM SAS drives to maintain a sane ratio of data to seek capacity

13年7月5⽇日星期五

Page 58: Something about Kafka - Why Kafka is so fast

Use Log file Structure!

13年7月5⽇日星期五

Page 59: Something about Kafka - Why Kafka is so fast

Feature

• One queue is one log file

• Operations is O(1)

• Reads do not block writes or each other

• Decouple with data size

• Retain messages after consumption

13年7月5⽇日星期五

Page 60: Something about Kafka - Why Kafka is so fast

zero-copyMaximize Performance

13年7月5⽇日星期五

Page 61: Something about Kafka - Why Kafka is so fast

1. The operating system reads data from the disk into pagecache in kernel space

2. The application reads the data from kernel space into a user-space buffer

3. The application writes the data back into kernel space into a socket buffer

4. The operating system copies the data from the socket buffer to the NIC buffer where it is sent over the network

13年7月5⽇日星期五

Page 62: Something about Kafka - Why Kafka is so fast

zerocopy

• data is copied into pagecache exactly once and reused on each consumption instead of being stored in memory and copied out to kernel space every time it is read

13年7月5⽇日星期五

Page 63: Something about Kafka - Why Kafka is so fast

13年7月5⽇日星期五

Page 64: Something about Kafka - Why Kafka is so fast

zerocopy performance

13年7月5⽇日星期五

Page 65: Something about Kafka - Why Kafka is so fast

End-to-end Batch CompressionMaximizing Performance

13年7月5⽇日星期五

Page 66: Something about Kafka - Why Kafka is so fast

Consider that

C1

C2

C3

P1

P2

2*compression+3*de-compression

M=num(P)N=num(C)M*compression+N*de-compression

13年7月5⽇日星期五

Page 67: Something about Kafka - Why Kafka is so fast

Key point

• End-to-end: compress by producers and de-compress by consumers

• Batch: compression aims to compress a ‘message set’

• Kafka supports GZIP and Snappy protocols

13年7月5⽇日星期五

Page 68: Something about Kafka - Why Kafka is so fast

Consumer State

13年7月5⽇日星期五

Page 69: Something about Kafka - Why Kafka is so fast

Facts

• No ACK

• Consumers maintain the message state

13年7月5⽇日星期五

Page 70: Something about Kafka - Why Kafka is so fast

Features

• Message is in a partition

• Stored and given out in the order they arrive

• ‘ watermark’ - ‘offset’ in Kafka

13年7月5⽇日星期五

Page 71: Something about Kafka - Why Kafka is so fast

track state

• write msg state in zookeeper

• in one transaction with writing data

• side benefit: ‘rewind’ msg

13年7月5⽇日星期五

Page 72: Something about Kafka - Why Kafka is so fast

Screenshot

13年7月5⽇日星期五

Page 73: Something about Kafka - Why Kafka is so fast

push vs. pullConsumer State

13年7月5⽇日星期五

Page 74: Something about Kafka - Why Kafka is so fast

push system

• if a consumer is <defunct>?

13年7月5⽇日星期五

Page 75: Something about Kafka - Why Kafka is so fast

Kafka use pull model

13年7月5⽇日星期五

Page 76: Something about Kafka - Why Kafka is so fast

MessageFormat & Data structure

13年7月5⽇日星期五

Page 77: Something about Kafka - Why Kafka is so fast

Msg Format• N byte message:

• If magic byte is 0

1. 1 byte "magic" identifier to allow format changes

2. 4 byte CRC32 of the payload

3. N - 5 byte payload

• If magic byte is 1

1. 1 byte "magic" identifier to allow format changes

2. 1 byte "attributes" identifier to allow annotations on the message independent of the version (e.g. compression enabled, type of codec used)

3. 4 byte CRC32 of the payload

4. N - 6 byte payload

13年7月5⽇日星期五

Page 78: Something about Kafka - Why Kafka is so fast

Log format on-disk

• On-disk format of a message• message length : 4 bytes (value: 1+4+n) • ‘magic’ value : 1 byte• crc : 4 bytes• payload : n bytes

• partition id and node id to uniquely identify a message

13年7月5⽇日星期五

Page 79: Something about Kafka - Why Kafka is so fast

Kafka Log Implementation

13年7月5⽇日星期五

Page 80: Something about Kafka - Why Kafka is so fast

Screenshot

13年7月5⽇日星期五

Page 81: Something about Kafka - Why Kafka is so fast

Screenshot

13年7月5⽇日星期五

Page 82: Something about Kafka - Why Kafka is so fast

WritesMessage

13年7月5⽇日星期五

Page 83: Something about Kafka - Why Kafka is so fast

Writes• Append-write

• When rotate:

• M : M messages in a log file

• S : S seconds after last flush

• Durability guarantee: losing at most M messages or S seconds of data in the event of a system crash

13年7月5⽇日星期五

Page 84: Something about Kafka - Why Kafka is so fast

ReadsMessage

13年7月5⽇日星期五

Page 85: Something about Kafka - Why Kafka is so fast

Buffer Reads

• auto double buffer size

• you can specify the max buffer size

13年7月5⽇日星期五

Page 86: Something about Kafka - Why Kafka is so fast

Offset Search

• Search steps:

1. locating the log segment file in which the data is stored

2. calculating the file-specific offset from the global offset value

3. reading from that file offset

• Simple binary in memory

13年7月5⽇日星期五

Page 87: Something about Kafka - Why Kafka is so fast

Features

• Reset the offset

• OutOfRangeException(problem we met)

13年7月5⽇日星期五

Page 88: Something about Kafka - Why Kafka is so fast

DeletesMessage

13年7月5⽇日星期五

Page 89: Something about Kafka - Why Kafka is so fast

Deletes• Policy: N days ago or N GB

• Deleting while reading?

• a copy-on-write style segment list implementation that provides consistent views to allow a binary search to proceed on an immutable static snapshot view of the log segments

13年7月5⽇日星期五

Page 90: Something about Kafka - Why Kafka is so fast

Zookeeper

13年7月5⽇日星期五

Page 91: Something about Kafka - Why Kafka is so fast

Directory StructureZookeeper

13年7月5⽇日星期五

Page 92: Something about Kafka - Why Kafka is so fast

Broker Node

• /brokers/ids/[0...N] --> host:port (ephemeral node)

13年7月5⽇日星期五

Page 93: Something about Kafka - Why Kafka is so fast

Broker Topic

• /brokers/topics/[topic]/[0...N] --> nPartions (ephemeral node)

13年7月5⽇日星期五

Page 94: Something about Kafka - Why Kafka is so fast

Consumer Id

• /consumers/[group_id]/ids/[consumer_id] --> {"topic1": #streams, ..., "topicN": #streams} (ephemeral node)

13年7月5⽇日星期五

Page 95: Something about Kafka - Why Kafka is so fast

Consumer Offset Tracking

• /consumers/[group_id]/offsets/[topic]/[broker_id-partition_id] --> offset_counter_value ((persistent node)

13年7月5⽇日星期五

Page 96: Something about Kafka - Why Kafka is so fast

Partition Owner

• /consumers/[group_id]/owners/[topic]/[broker_id-partition_id] --> consumer_node_id (ephemeral node)

13年7月5⽇日星期五

Page 97: Something about Kafka - Why Kafka is so fast

Why Kafka fast?• Maximizing Performance

• Filesystem vs. Memory• BTree?• Zero-copy• End-to-end Batch Compression

• Consumer state• Message delivery semantics• Consumer state• Push vs. Pull

• Message• Message format• Disk structure

• Zookeeper• Directory Structure

13年7月5⽇日星期五

Page 98: Something about Kafka - Why Kafka is so fast

Thank You!

13年7月5⽇日星期五