47
2016

Javaeeconf 2016 how to cook apache kafka with camel and spring boot

Embed Size (px)

Citation preview

Page 1: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

2016

Page 2: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

2

HOW TO COOK APACHE KAFKAWITH CAMEL AND SPRING BOOT

Java EE conference 2016

Ivan VasylievPlaytika Core Services Team

Page 3: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

3Java EE conference 2016

AGENDA

Basics of Apache Kafka Apache Camel Spring Boot Demo Q&A

CODE SLIDES

Page 4: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

4Java EE conference 2016

WHY APACHE KAFKA?

http://research.microsoft.com/en-us/um/people/srikanth/netdb11/netdb11papers/netdb11-final12.pdf

Page 5: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

5Java EE conference 2016

WHY APACHE KAFKA?

Designed for large scale Widely adopted by top tech companies Hardened production quality product Data replication out of the box

Page 6: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

6Java EE conference 2016

FEATURES

At most once, at least once guarantees Batching for high throughput cases Efficient with DEFAULT settings

Page 7: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

7Java EE conference 2016

EVEN MORE FEATURES

Mirroring between datacenters Connectors to various DWH Complex event processing integrations

Page 8: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

8Java EE conference 2016

HIGH LEVEL VIEW

http://kafka.apache.org/documentation.html#introduction

Page 9: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

9Java EE conference 2016

HIGH LEVEL VIEW

Publisher/subscriber and point-to-point models Client which sends message – producer Client which receives messages - consumer

Page 10: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

10Java EE conference 2016

WHAT IS NOT INCLUDED - JMS

Page 11: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

11Java EE conference 2016

WHAT IS NOT INCLUDED - JMS

Not a JMS compliant server No message headers

Can employ message key Send in payload Wait for it, on roadmap

No transactions/JTA support

Page 12: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

12Java EE conference 2016

WHAT IS NOT INCLUDED - EXACTLY ONCE GUARANTEE

Page 13: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

13Java EE conference 2016

WHAT IS NOT INCLUDED - EXACTLY ONCE GUARANTEE

No exactly once guarantee Duplicates because of failures De-duplication is on roadmap

De-duplication on consumer With camel EIP, by message ID/body Consumer can tolerate duplicates

Page 14: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

14Java EE conference 2016

APACHE KAFKA LANGUAGE

http://research.microsoft.com/en-us/um/people/srikanth/netdb11/netdb11papers/netdb11-final12.pdf

Page 15: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

15Java EE conference 2016

APACHE KAFKA LANGUAGE

Topic - represents stream of messages Contains set of partitions

Partition - subset of messages in stream Partitioning is done by message key on producer

No “queue” in dictionary

Page 16: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

16Java EE conference 2016

TOPICS AND PARTITIONS

http://kafka.apache.org/documentation.html#intro_topics

Page 17: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

17Java EE conference 2016

TOPICS AND PARTITIONS

Partition is smallest unit of storage in kafka Partition is data file with messages

Producer always append to end of file Consumers scroll/seek over file

Consumer offset is persisted (zk or kafka) Strong ordering guarantees for consumer

Page 18: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

18Java EE conference 2016

QUEUE SEMANTIC IS DONE ON CLIENT

http://kafka.apache.org/documentation.html#intro_consumers

Page 19: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

19Java EE conference 2016

QUEUE

Consumer offset is persisted by group id/per partition Queue semantic inside of consumer group Topic semantic between consumer groups

Page 20: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

20Java EE conference 2016

CONSUMPTION IS ALL ABOUT OFFSETS

https://hadoopabcd.wordpress.com/2015/04/11/kafka-building-a-real-time-data-pipeline/

Page 21: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

21Java EE conference 2016

CONSUMPTION IS ALL ABOUT OFFSETS

Consumer polls data from broker Consumer offset is send (committed) to server Auto offset commit enabled

By separate thread, periodically

Auto offset commit disabled By your code, when batch of messages processed

Page 22: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

22Java EE conference 2016

CONSUMER OFFSET AND AUTO-COMMIT

Page 23: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

23Java EE conference 2016

CONSUMER OFFSET AND AUTO-COMMIT

With “auto-commit” enabled you can loose messages Step1: One thread did not finish processing and failed Step 2: Auto-commit thread does not care

Auto-commit is OK for status heartbeats Auto-commit is NOT OK if you need “at least once” guarantee, e.g. payment processing

Page 24: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

24Java EE conference 2016

DATA REPLICATION

Page 25: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

25Java EE conference 2016

DATA REPLICATION

Leader receives all reads and writes Decides when to commit message

Follower syncs messages from leader Take over if leader is down

Replication controller maintains leader Zookeper used for coordination

Leader election Consensus protocol

Page 26: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

26Java EE conference 2016

APACHE KAFKA PRODUCER

Page 27: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

27Java EE conference 2016

APACHE KAFKA PRODUCER

Performs load balancing Uses message key to select partition Finds appropriate kafka broker leader for partition Has few configurable acknowledge modes Can do batching in async mode

Page 28: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

28Java EE conference 2016

DELIVERY GUARANTEED

Page 29: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

29Java EE conference 2016

DELIVERY GUARANTEED

Durability with ack levels on producer side Data replication between brokers No in-memory state, efficient persistence Manually committing offset on consumer side

Page 30: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

30Java EE conference 2016

ISSUES - OPS

Ops is not free There is Zookeeper on board Easy to setup with Docker/Rancher

Need to learn basics to setup and monitor

Page 31: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

31Java EE conference 2016

ISSUES – DATA

Can’t auto-scale existing data Option 1: Add new partitions, they will go to new nodes Option 2: Do it manually, move partitions around Option 3: Wait for it, on roadmap

Mirroring seems to work into one direction Can’t handle very large number of topics

Page 32: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

32Java EE conference 2016

WHY APACHE CAMEL?

Page 33: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

33Java EE conference 2016

WHY APACHE CAMEL?

Message routing DSL (java/scala/grooovy) Enterprise Integration Patterns

Idempotent consumer (de-duplication) Aggregator …

Abstractions for testing MockEndpoint Route Advice

Page 34: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

34Java EE conference 2016

APACHE CAMEL

http://camel.apache.org/java-dsl.html

Page 35: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

35Java EE conference 2016

APACHE CAMEL

Lightweight and embeddable Spring boot integration Connectors to various message and data sources

Page 36: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

36Java EE conference 2016

SPRING BOOT

Page 37: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

37Java EE conference 2016

SPRING BOOT

Fat jar/jee containerless deployment Autoconfiguration and conditionals Сodeless usage of spring cloud/netflix projects

Page 38: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

38Java EE conference 2016

Page 39: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

39Java EE conference 2016

GOTCHA’S – PRODUCER FASTER THAN CONSUMER, PRECONDITIONS

Its not recommended to have lots of partitions Each partition is consumed by one consumer thread Producer X times faster than consumer

Page 40: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

40Java EE conference 2016

GOTCHA’S – PRODUCER FASTER THAN CONSUMERS, ACTIONS

Monitor kafka lag Messages not consumed by group

Add intermediate multiplexing queue See camel “seda” component Think carefully since in-memory state can lead to data loss

Consider adding more partitions Will allow more consumption threads

Page 41: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

41Java EE conference 2016

GOTCHA’S – PRODUCER FASTER THAN CONSUMERS, TOOLS

https://github.com/quantifind/KafkaOffsetMonitor

Page 42: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

42Java EE conference 2016

GOTCHA’S – AUTO OFFSET RESET

When you start test you do not receive any messages Producer sends message before consumer is UP Check auto.offset.reset setting in unit test

Latest (or largest in old api) can lead to consumption of only new messages Earliest (or smallest in old api) will mean “from beginning”

Page 43: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

43Java EE conference 2016

GOTCHA’S – CLIENT VERSION MIGHT NEED TO MATCH SERVER

Clients supposed to be “backward compatible”, but … If you see weird things – you should check classpath

Page 44: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

44Java EE conference 2016

GOTCHA’S – WATCH THE CLASSPATH

Multiple versions of kafka client Multiple versions of kafka client dependencies Multiple versions of zookeper client

Page 45: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

45Java EE conference 2016

DEPENDENCY MANAGEMENT

Use dependency management to force versions and exclusions Use “Maven helper” Intellij plugin to check issues

https://github.com/krasa/MavenHelperhttps://plugins.jetbrains.com/plugin/7179

Page 46: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

46Java EE conference 2016

Page 47: Javaeeconf 2016 how to cook apache kafka with camel and spring boot

Thank [email protected]

Join us:http://goo.gl/LuWMo3

47Java EE conference 2016