Kafka as Message Broker
-
Upload
haluan-irsad -
Category
Technology
-
view
112 -
download
1
Transcript of Kafka as Message Broker
WhentouseMB?
1. Decouplingmessagepublisherandconsumer(receiver)2. Storingthemessage3. Routingofmessage4. Monitoringandmanagementofmessages
Queuing
“ Apoolofconsumersmayreadfromaserverandeachrecordgoestooneofthem”(+)Allowtodivideupthedataprocessingovermultipleconsumerinstances,whichletsyouscaleuptheprocess.(-)Notmulti-subscriber—onceoneprocessreadsthedatait'sgone.
Publish-Subscribe
“Broadcastmessagestoallconsumersthatsubscribedtoaspecificchannel”(+)Allowgreaterscalabilityandamoredynamicnetworktopology.(-)Messagespecificationandparticipantrulesaddssomemorecomplexitytothesystem.(-)Hasnowayofscalingprocessingsinceeverymessagegoestoeverysubscriber
NotesnottoUseMB
ThesystemalreadyimplementObserverDesignPattern(readmore:https://sourcemaking.com/design_patterns/observer)
Kafkaisawesome(ehemmm..)
• Buildingreal-timestreamingdatapipelinesthatreliablygetdatabetweensystemsorapplications• Buildingreal-timestreamingapplicationsthattransformorreacttothestreamsofdata
Concepts
• Kafkaisrunasaclusterononeormoreservers.• TheKafkaclusterstores streamsof records incategoriescalled topics.• Eachrecordconsistsofakey,avalue,andatimestamp
CoreAPIs
• The ProducerAPI allowsanapplicationtopublish astreamrecordstooneormoreKafkatopics.• The ConsumerAPI allowsanapplicationtosubscribe tooneormoretopicsandprocessthestreamofrecordsproducedtothem.• The StreamsAPI allowsanapplicationtoactasa streamprocessor,consuming aninputstreamfromoneormoretopicsandproducing anoutputstreamtooneormoreoutputtopics,effectivelytransformingtheinputstreamstooutputstreams.• The ConnectorAPI allowsbuildingandrunningreusableproducers orconsumers thatconnect Kafkatopicstoexistingapplicationsordatasystems.Forexample,aconnectortoarelationaldatabasemightcaptureeverychangetoatable.
KafkaTopic
Atopic isacategory orfeednametowhichrecords arepublished.Topics inKafkaarealwaysmulti-subscriber;thatis,atopiccanhavezero,one,ormanyconsumersthatsubscribetothedatawrittentoit.
KafkaLogs
Eachpartition isanordered,immutable sequenceofrecordsthatiscontinuallyappendedto—astructuredcommitlog.
Distribution
• Eachpartitionhasoneserverwhichactsasthe"leader"andzeroormoreserverswhichactas"followers".Theleaderhandlesallreadandwriterequestsforthepartitionwhilethefollowers passivelyreplicate theleader• UseZooKeeper (morenextpart)
Guarantees
• Messagessentbyaproducertoaparticulartopicpartitionwillbeappendedintheordertheyaresent.Thatis,ifarecordM1issentbythesameproducerasarecordM2,andM1issentfirst,thenM1willhavealoweroffsetthanM2andappearearlierinthelog(FIFO).• Aconsumerinstanceseesrecordsintheordertheyarestoredinthelog.• ForatopicwithreplicationfactorN,KafkawilltolerateuptoN-1serverfailures withoutlosing anyrecordscommittedtothelog.
KafkaasaStorageSystem
• Datawritten toKafkaiswrittentodisk andreplicated forfault-tolerance.• Kafkaallowsproducerstowaitonacknowledgementsothatawriteisn'tconsideredcompleteuntilitisfullyreplicatedandguaranteed topersisteveniftheserverwrittentofails.
KafkaforStreamProcessing
• Handlingout-of-orderdata,reprocessing inputascodechanges,performingstateful computationsinrealtime.(Readmorehttps://kafka.apache.org/documentation.html#streams)
Howitworks?
• ZooKeeper ManagedKafkaclusterconfiguration.• ZooKeeper canelectKafkanodetobecomethemaster.• ProducersandConsumersarenotifiedbyZooKeeper serviceaboutthepresenceofnew brokerinKafkasystemorfailure ofthebrokerintheKafkasystem.• ZooKeeper SynchronizedKafkaacrossthecluster.
PuttingthePiecesTogether(DO)
• WriteaScalaserviceastheproducer• Writearubyserviceastheconsumer• Exchangedatainrealtime