Home
%3CLINGO-SUB%20id%3D%22lingo-sub-822926%22%20slang%3D%22en-US%22%3EProcessing%20ordered%20IoT%20streams%20in%20Azure%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-822926%22%20slang%3D%22en-US%22%3E%3CH1%20id%3D%22toc-hId-2016597408%22%20id%3D%22toc-hId-2016597408%22%3EWhy%20are%20Ordered%20Events%20Important%3F%3C%2FH1%3E%0A%3CP%3EWhile%20many%20IoT%20data%20streams%20can%2C%20and%20should%2C%20be%20treated%20as%20Discrete%20events%2C%20some%20need%20to%20be%20treated%20as%20Series%20events%2C%20where%20events%20must%20be%20processed%20in%20order%20throughout%20the%20solution%20pipeline.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhat%20are%20Series%20events%3F%26nbsp%3B%3CEM%3E%3CSTRONG%3ESeries%20events%3C%2FSTRONG%3E%3C%2FEM%3E%26nbsp%3Bare%20ordered%20and%20interrelated.%20The%20consumer%20needs%20the%20sequenced%20series%20of%20events%20to%20analyze%20what%20happened.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAn%20example%20of%20Series%20events%20is%20streaming%20temperature%20values%20or%20GPS%20coordinates%20from%20a%20moving%20vehicle.%20These%20are%20time-ordered%20and%20often%20must%20be%20processed%20in%20order.%20For%20example%2C%20if%20the%20average%20temperature%20over%20the%20last%205%20minutes%20is%20trending%20up%2C%20trigger%20an%20alert.%20Another%20example%2C%20if%20a%20vehicle's%20GPS%20coordinates%20are%20streamed%20to%20a%20geofence%20solution%2C%20and%20you%20process%20those%20events%20out-of-order%2C%20you%20could%20have%20a%20situation%20where%20solution%20detects%20the%20vehicle%20is%20leaving%20the%20geofence%20before%20it%20had%20entered%20the%20geofence.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOther%20examples%20include%20real-time%20fraud%20detection%20and%20systems%20that%20utilize%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Farchitecture%2Fpatterns%2Fevent-sourcing%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EEvent%20Sourcing%3C%2FA%3E.%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId--535559553%22%20id%3D%22toc-hId--535559553%22%3EWays%20to%20Handle%20Ordered%20Events%3C%2FH1%3E%0A%3CP%3EBefore%20we%20delve%20into%20the%20complexities%20let's%20discuss%20other%20ways%20of%20handling%20ordered%20events%2C%20such%20as%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EDo%20not%20order%20events%3CUL%3E%0A%3CLI%3ESounds%20obvious%20but%20often%20developers%20assume%20all%20events%20must%20be%20ordered.%20Not%20so.%20Many%20IoT%20data%20streams%20can%20be%20treated%20as%20Discrete.%26nbsp%3B%3C%2FLI%3E%0A%3CLI%3EAnalyze%20your%20streaming%20IoT%20data%20and%20determine%20which%20are%20Discrete%20events%20vs%20Series%20Events.%20Try%20to%20make%20most%20events%20discrete%2C%20as%20scale%20is%20easier%20to%20achieve%20with%20Discrete%20events.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3CLI%3EStore%20events%20out%20of%20order%20but%20return%20queries%20in%20ordered%20form%3CUL%3E%0A%3CLI%3EThe%20challenge%20is%20that%20you%20need%20to%20land%20your%20data%20first%20(e.g.%20in%20Cosmos%20DB%2C%20Azure%20Data%20Explorer%2C%20etc.)%2C%20then%20have%20your%20microservices%20query%20(poll)%20for%20this%20data%20in%20ordered%20form.%3C%2FLI%3E%0A%3CLI%3EThis%20introduces%20latency%20in%20the%20solution%20as%20the%20data%20needs%20to%20land%20first%20before%20processing%20it.%3C%2FLI%3E%0A%3CLI%3EIf%20your%20solution%20can%20tolerate%20this%20latency%20then%20this%20is%20a%20perfectly%20viable%20approach.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EFailing%20these%20options%2C%20you%20will%20need%20to%20process%20events%20in%20order%20as%20they%20arrive.%20Herein%20lies%20the%20challenge.%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId-1207250782%22%20id%3D%22toc-hId-1207250782%22%3EOrdered%20Events%20and%20CAP%20Theorem%3C%2FH1%3E%0A%3CP%3EProcessing%20ordered%20events%2C%20at%20scale%2C%20is%20difficult.%20Why%3F%20Blame%20%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FCAP_theorem%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CEM%3ECAP%20Theorem%3C%2FEM%3E%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F128604i7C94CEF263DB02FB%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_0.png%22%20title%3D%22clipboard_image_0.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FConsistency_(database_systems)%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CEM%3EConsistency%3C%2FEM%3E%3C%2FA%3E%3A%20Every%20read%20receives%20the%20most%20recent%20write%20or%20an%20error.%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FAvailability%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CEM%3EAvailability%3C%2FEM%3E%3C%2FA%3E%3A%20Every%20request%20receives%20a%20(non-error)%20response%20%E2%80%93%20without%20the%20guarantee%20that%20it%20contains%20the%20most%20recent%20write.%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FNetwork_partitioning%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CEM%3EPartition%20tolerance%3C%2FEM%3E%3C%2FA%3E%3A%20The%20system%20continues%20to%20operate%20despite%20an%20arbitrary%20number%20of%20messages%20being%20dropped%20(or%20delayed)%20by%20the%20network%20between%20nodes.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EMuch%20has%20been%20written%20about%20CAP%20Theorem%20and%20databases%2C%20with%20some%20database%20systems%20allowing%20you%20to%20choose%20between%20Consistency%20and%20Availability.%20Azure%20Cosmos%20DB%20is%20a%20great%20example%20of%20such%20as%20system.%20It%20supports%20%3CA%20href%3D%22https%3A%2F%2Fblogs.technet.microsoft.com%2Fuktechnet%2F2019%2F03%2F12%2Fazure-cosmos-db-tunable-consistency%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Etunable%20consistency%20levels%3C%2FA%3E.%20However%2C%20CAP%20Theorem%20can%20also%20be%20applied%20to%20IoT%20data%20streams%20and%20the%20trade-offs%20necessary%20to%20process%20ordered%20events.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20with%20databases%2C%20event%20ingestion%20services%20such%20as%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fiot-hub%2Fiot-hub-scaling%23partitions%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20IoT%20Hub%3C%2FA%3E%2C%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fevent-hubs%2Fevent-hubs-features%23partitions%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Event%20Hub%3C%2FA%3E%2C%20%3CA%20href%3D%22https%3A%2F%2Fkafka.apache.org%2F23%2Fdocumentation%2Fstreams%2Farchitecture%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EKafka%20%3C%2FA%3Eetc.%20utilize%20partitions%20in%20order%20to%20scale%20out.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECAP%20theorem%20implies%20that%20in%20the%20presence%20of%20a%20network%20partition%2C%20one%20has%20to%20choose%20between%20consistency%20and%20availability.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%2C%20think%20of%20%3CEM%3EPartition%20%3C%2FEM%3Eas%20a%20constant.%20With%20Partition%20as%20a%20constant%2C%20the%20choice%20becomes%20%3CEM%3EConsistency%3C%2FEM%3Eor%20%3CEM%3EAvailability.%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20what%20exactly%20is%20a%20partition%3F%20According%20to%20the%20Event%20Hub%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fevent-hubs%2Fevent-hubs-features%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Edocumentation%3C%2FA%3E%2C%20a%20partition%20is%20an%20ordered%20sequence%20of%20events.%20As%20newer%20events%20arrive%2C%20they%20are%20added%20to%20the%20end%20of%20this%20sequence.%20A%20partition%20can%20be%20thought%20of%20as%20a%20%22commit%20log.%22%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorclipboard_image_1%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3EPartitions%20are%20a%20data%20organization%20mechanism%20that%20relates%20to%20the%20downstream%20parallelism%20required%20in%20consuming%20applications.%26nbsp%3BThe%20more%20partitions%2C%20the%20more%20parallelism.%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId--1344906179%22%20id%3D%22toc-hId--1344906179%22%3EThe%20Tradeoff%3C%2FH1%3E%0A%3CP%3EHere's%20where%20the%20tradeoff%20comes%20into%20play.%20If%20you%20require%20consistency%20(where%20streaming%20events%20must%20be%20processed%20in%20order)%20you%20need%20to%20ensure%20that%20all%20events%20from%20that%20device%20arrive%20in%20the%20same%20partition.%20In%20doing%20so%20you%20are%20trading%20off%20availability%2C%20as%20you%20now%20have%20a%20reliance%20on%20individual%20partitions.%20You%20are%20choosing%20Consistency%20over%20Availability%20in%20terms%20of%20CAP%20Theorem.%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId-397904156%22%20id%3D%22toc-hId-397904156%22%3EAchieving%20Consistency%20with%20Event%20Hub%20and%20IoT%20Hub%3C%2FH1%3E%0A%3CP%3EIn%20order%20to%20guarantee%20consistency%2C%20%3CSTRONG%3E%3CEM%3Eall%20events%20coming%20from%20the%20same%20device%20must%20go%20to%20the%20same%20partition%3C%2FEM%3E%3C%2FSTRONG%3E.%20Azure%20IoT%20Hub%20already%20does%20this%20for%20us%20behind%20the%20scenes.%20All%20messages%20from%20a%20specific%20IoT%20Hub%20%3CSTRONG%3EDevice%20Id%3C%2FSTRONG%3Ealways%20end%20up%20on%20the%20same%20partition.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorclipboard_image_2%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F128607iFCD902C1B645A9D2%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22IotHub.jpg%22%20title%3D%22IotHub.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20with%20Event%20Hub%2C%20you%20need%20to%20do%20this%20in%20your%20publisher%20code%2C%20as%20Event%20Hub%20has%20no%20concept%20of%20a%20device.%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId-2140714491%22%20id%3D%22toc-hId-2140714491%22%3ESending%20events%20to%20an%20Event%20Hub%20Partition%3C%2FH1%3E%0A%3CP%3EThere%20are%20two%20ways%20to%20guarantee%20consistency%20when%20sending%20events%20to%20Event%20Hub%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3ESpecify%20a%20partition%20key%20when%20sending%20an%20event%2C%20or%3C%2FLI%3E%0A%3CLI%3ESpecify%20a%26nbsp%3Bpartition%20ID%26nbsp%3Bto%20send%20events%20to%20a%20specific%20partition.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EThe%20first%20way%20is%20to%20specify%20a%20partition%20key%20when%20sending%20using%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.eventhubclient.sendasync%3Fview%3Dazure-dotnet%23Microsoft_Azure_EventHubs_EventHubClient_SendAsync_Microsoft_Azure_EventHubs_EventData_System_String_%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESendAsync%3C%2FA%3Emethod%20of%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.eventhubclient.sendasync%3Fview%3Dazure-dotnet%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EEventHubClient%3C%2FA%3E.%20Normally%20this%20would%20be%20a%20unique%20device%20ID%20or%20device%20name.%20A%20hash%20algorithm%20is%20run%20over%20the%20partition%20key%20to%20ensure%20all%20events%20with%20the%20same%20partition%20key%20land%20on%20the%20same%20partition.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorclipboard_image_3%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F128608i75DD16D25C5A3D2E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22eventhub-sending.jpg%22%20title%3D%22eventhub-sending.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20following%20code%20example%20shows%20100%20events%20landing%20on%20the%20same%20partition%20by%20specifying%20a%20partition%20key%20(e.g.%20%E2%80%9CDevice1%E2%80%9D).%20Note%3A%20The%20Event%20Hub%20was%20created%20with%204%20partitions%2C%20numbered%200%20to%203.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eprivate%20static%20async%20Task%20SendEventsToEventHub(int%20numEventsToSend)%0A%7B%0A%20%20%20%20for%20(var%20i%20%3D%200%3B%20i%20%26lt%3B%20numEventsToSend%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20try%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20eventMsg%20%3D%20%24%22Event%20%7Bi%7D%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%24%22Sending%20event%3A%20%7BeventMsg%7D%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20await%20eventHubClient.SendAsync(new%20EventData(Encoding.UTF8.GetBytes(eventMsg))%2C%22Device1%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20catch%20(Exception%20exception)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%24%22%7BDateTime.Now%7D%20%26gt%3B%20Exception%3A%20%7Bexception.Message%7D%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20await%20Task.Delay(10)%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20Console.WriteLine(%24%22%7BnumEventsToSend%7D%20events%20sent.%22)%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20issue%20with%20this%20method%20is%20you%20don%E2%80%99t%20know%20exactly%20which%20partition%20the%20event%20will%20land%20on.%20In%20this%20case%20the%20sample%20code%20shows%20the%20events%20from%20%E2%80%98Device1%E2%80%99%20all%20landing%20on%20partition%20%E2%80%982%E2%80%99.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3ERegistering%20EventProcessor...%0AReceiving.%20Press%20ENTER%20to%20stop%20worker.%0ASimpleEventProcessor%20initialized.%20Partition%3A%20'2'%0ASimpleEventProcessor%20initialized.%20Partition%3A%20'0'%0ASimpleEventProcessor%20initialized.%20Partition%3A%20'3'%0ASimpleEventProcessor%20initialized.%20Partition%3A%20'1'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%200'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%201'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%202'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%203'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%204'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%205'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%206'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%207'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%208'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%209'%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20second%20way%20is%20to%20specify%20an%20actual%20partition%20ID%20when%20sending%20using%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.partitionsender.sendasync%3Fview%3Dazure-dotnet%23Microsoft_Azure_EventHubs_PartitionSender_SendAsync_Microsoft_Azure_EventHubs_EventData_%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESendAsync%3C%2FA%3Emethod%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.partitionsender%3Fview%3Dazure-dotnet%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EPartitionSender%3C%2FA%3Eobject.%20Use%20this%20option%20when%20you%20need%20to%20specifically%20control%20which%20events%20land%20on%20which%20partition.%20In%20this%20example%20I%20have%20explicitly%20specified%20partition%20%E2%80%980%E2%80%99%20when%20creating%20the%20partition%20sender%20object.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Estatic%20async%20Task%20Main(string%5B%5D%20args)%0A%7B%0A%20%20%20%20%2F%2F%20Creates%20an%20EventHubsConnectionStringBuilder%20object%20from%20the%20connection%20string%2C%20and%20sets%20the%20EntityPath.%0A%20%20%20%20%2F%2F%20Typically%2C%20the%20connection%20string%20should%20have%20the%20entity%20path%20in%20it%2C%20but%20this%20simple%20scenario%0A%20%20%20%20%2F%2F%20uses%20the%20connection%20string%20from%20the%20namespace.%0A%20%20%20%20var%20connectionStringBuilder%20%3D%20new%20EventHubsConnectionStringBuilder(EventHubConnectionString)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20EntityPath%20%3D%20EventHubName%0A%20%20%20%20%7D%3B%0A%0A%20%20%20%20eventHubClient%20%3D%20EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString())%3B%0A%0A%20%20%20%20%2F%2F%20Specify%20a%20specific%20partition%20Id%20to%20send%20the%20messages%20to.%0A%20%20%20%20partitionSender%20%3D%20eventHubClient.CreatePartitionSender(%222%22)%3B%0A%0A%20%20%20%20await%20SendEventsToEventHub(100)%3B%0A%0A%20%20%20%20await%20eventHubClient.CloseAsync()%3B%0A%0A%20%20%20%20Console.WriteLine(%22Press%20ENTER%20to%20exit.%22)%3B%0A%20%20%20%20Console.ReadLine()%3B%0A%7D%0A%0A%2F%2F%20Uses%20the%20event%20hub%20client%20to%20send%20100%20events%20to%20the%20same%20event%20hub%20partition%0Aprivate%20static%20async%20Task%20SendEventsToEventHub(int%20numEventsToSend)%0A%7B%0A%20%20%20%20for%20(var%20i%20%3D%200%3B%20i%20%26lt%3B%20numEventsToSend%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20try%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20eventMsg%20%3D%20%24%22Event%20%7Bi%7D%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%24%22Sending%20event%3A%20%7BeventMsg%7D%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20await%20partitionSender.SendAsync(new%20EventData(Encoding.UTF8.GetBytes(eventMsg)))%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20catch%20(Exception%20exception)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%24%22%7BDateTime.Now%7D%20%26gt%3B%20Exception%3A%20%7Bexception.Message%7D%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20await%20Task.Delay(10)%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20Console.WriteLine(%24%22%7BnumEventsToSend%7D%20events%20sent.%22)%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENote%3C%2FSTRONG%3E%3A%20Do%20not%20mix%20PartitionKey%20and%20PartitionSender%20as%20they%20are%20two%20mutually%20exclusive%20concepts.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId--411442470%22%20id%3D%22toc-hId--411442470%22%3EConsuming%20ordered%20events%20from%20a%20Partition%3C%2FH1%3E%0A%3CP%3ENow%2C%20how%20do%20we%20consume%20events%20from%20IoT%20Hub%20or%20Event%20Hub%3F%20There%20are%20currently%20two%20.NET%20Standard%20client%20libraries%20for%20reading%20events%20from%20IoT%20Hub%20and%20Event%20Hub%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.processor%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure.EventHubs.Processor%3C%2FA%3E%3A%20Contains%20the%20%3CEM%3E%3CSTRONG%3EE%3C%2FSTRONG%3E%3CSTRONG%3Event%20Processor%20Host%3C%2FSTRONG%3E%3C%2FEM%3E%26nbsp%3B(EPH)%20class%20that%20provide%20high%20level%20functionality%20to%20read%20events%20from%20all%20partitions%2C%20and%20enables%20keeping%20track%20of%20processed%20events%20via%20checkpoints.%20The%20EPH%20interface%20is%20the%20easiest%20way%20to%20read%20from%20an%20IoT%20Hub%20or%20Event%20Hub%20and%20always%20the%20best%20place%20to%20start.%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure.EventHubs%3C%2FA%3E%3A%20Provides%20more%20granular%20operations%20than%20EPH%20and%20is%20somewhat%20more%20complex%20to%20use.%20It%20does%20not%20automatically%20provide%20checkpoints.%20You'll%20need%20to%20add%20this%20yourself.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EReading%20events%20from%20IoT%20Hub%20or%20Event%20Hub%20is%20always%20accomplished%20through%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fevent-hubs%2Fevent-hubs-features%23consumer-groups%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EConsumer%20Group%3C%2FA%3E.%20As%20noted%20in%20the%20link%2C%26nbsp%3B%3CSTRONG%3E%3CEM%3Eit%20is%20recommended%20that%20there%20is%20only%20one%20active%20receiver%20on%20a%20partition%20per%20consumer%20group%3C%2FEM%3E.%20%3C%2FSTRONG%3EOtherwise%20you%20wind%20up%20with%20duplicate%20messages%20being%20processed.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1134854360%22%20id%3D%22toc-hId-1134854360%22%3EEvent%20Processor%20Host%3C%2FH2%3E%0A%3CP%3ETo%20consume%20Series%20(and%20Discrete)%20events%20from%20IoT%20Hub%20and%20Event%20Hub%20we%20always%20recommend%20using%20Event%20Processor%20Host%20(EPH).%20Event%20Processor%20Host%20is%20used%20by%20a%20number%20of%20higher%20level%20Azure%20services%20(such%20as%20Azure%20Functions%20-%20Event%20Hub%20Trigger%20and%20IoT%20Hub%20Trigger)%20and%26nbsp%3B%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3Ehas%20a%20number%20of%20advanced%2C%26nbsp%3B%3C%2FSPAN%3E%3CA%20style%3D%22font-family%3A%20inherit%3B%20background-color%3A%20%23ffffff%3B%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fevent-hubs%2Fevent-hubs-event-processor-host%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Edocumented%3C%2FA%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3Ecapabilities%20including%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EScale%20-%20%3CSPAN%3ECreate%20multiple%20consumers%2C%20with%20each%20consumer%20taking%20ownership%20of%20reading%20from%20a%20few%20Event%20Hubs%20partitions.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3ELoad%20balancing%20-%26nbsp%3B%3CSPAN%3EIncrease%20or%20reduce%20the%20consumers%20dynamically.%20For%20example%2C%20when%20a%20new%20sensor%20type%20(for%20example%2C%20a%20carbon%20monoxide%20detector)%20is%20added%20to%20each%20home%2C%20the%20number%20of%20events%20increases.%20In%20that%20case%2C%20the%20operator%20(a%20human)%20increases%20the%20number%20of%20consumer%20instances.%20Then%2C%20the%20pool%20of%20consumers%20can%20re-balance%20the%20number%20of%20partitions%20they%20own%2C%20to%20share%20the%20load%20with%20the%20newly%20added%20consumers.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3ESeamless%20resume%20on%20failure%20-%26nbsp%3B%3CSPAN%3EIf%20a%20consumer%20(%3C%2FSPAN%3E%3CSTRONG%3Econsumer%20A%3C%2FSTRONG%3E%3CSPAN%3E)%20fails%20(for%20example%2C%20the%20virtual%20machine%20hosting%20the%20consumer%20suddenly%20crashes)%2C%20then%20other%20consumers%20must%20be%20able%20to%20pick%20up%20the%20partitions%20owned%20by%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3Econsumer%20A%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3Band%20continue.%20Also%2C%20the%20continuation%20point%2C%20called%20a%26nbsp%3B%3C%2FSPAN%3E%3CEM%3Echeckpoint%3C%2FEM%3E%3CSPAN%3E%26nbsp%3Bor%26nbsp%3B%3C%2FSPAN%3E%3CEM%3Eoffset%3C%2FEM%3E%3CSPAN%3E%2C%20should%20be%20at%20the%20exact%20point%20at%20which%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3Econsumer%20A%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3Bfailed%2C%20or%20slightly%20before%20that.%20EPH%20saves%20checkpoints%20to%20Azure%20blob%20storage.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%3EThese%20capabilities%20are%20provided%20through%20the%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.processor.ieventprocessor%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-linktype%3D%22absolute-path%22%3EIEventProcessor%3C%2FA%3E%26nbsp%3Binterface%20of%20the%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.processor.eventprocessorhost%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-linktype%3D%22absolute-path%22%3EEventProcessorHost%3C%2FA%3E%26nbsp%3Bclass.%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3EConsumers%20register%20the%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.processor.eventprocessorhost%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-linktype%3D%22absolute-path%22%3EEventProcessorHost%3C%2FA%3E%3CSPAN%3E%26nbsp%3Binstance%20with%20the%20IoT%20Hub%20or%20Event%20Hubs%20service.%20Registering%20an%20event%20processor%20class%20with%20an%20instance%20of%20EventProcessorHost%20starts%20event%20processing.%20Registering%20instructs%20the%20Event%20Hubs%20service%20to%20expect%20that%20the%20consumer%20app%20consumes%20events%20from%20some%20of%20its%20partitions%2C%20and%20to%20invoke%20the%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.processor.ieventprocessor%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-linktype%3D%22absolute-path%22%3EIEventProcessor%3C%2FA%3E%3CSPAN%3E%26nbsp%3Bimplementation%20code%20whenever%20it%20pushes%20events%20to%20consume.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAccording%20to%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.processor.ieventprocessor%3Fview%3Dazure-dotnet%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Edocumentation%3C%2FA%3E%3A%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20center%3B%22%3E%3CFONT%20size%3D%225%22%3E%3CEM%3E%22Any%20given%20instance%20of%20an%20event%20processor%20class%20will%20only%20process%20events%20from%20one%20partition%20of%20one%20Event%20Hub.%22%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThis%20ensures%20there%20is%20only%20one%20consumer%20per%20partition%20and%20ensures%20messages%20are%20processed%20in%20order%20by%20your%20consumer.%20There%20is%20a%20great%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fevent-hubs%2Fevent-hubs-event-processor-host%23example%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Eexample%3C%2FA%3Eof%20how%20this%20is%20accomplished%20in%20the%20Event%20Host%20Processor%20documentation.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%20There%20are%20four%20(4)%20methods%20to%20implement%20in%20IEventProcessor%20as%20shown%20in%20the%20following%20code%20sample.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20%20%20public%20class%20SimpleEventProcessor%20%3A%20IEventProcessor%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20public%20Task%20CloseAsync(PartitionContext%20context%2C%20CloseReason%20reason)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%24%22Processor%20Shutting%20Down.%20Partition%20'%7Bcontext.PartitionId%7D'%2C%20Reason%3A%20'%7Breason%7D'.%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Task.CompletedTask%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20public%20Task%20OpenAsync(PartitionContext%20context)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%24%22SimpleEventProcessor%20initialized.%20Partition%3A%20'%7Bcontext.PartitionId%7D'%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Task.CompletedTask%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20public%20Task%20ProcessErrorAsync(PartitionContext%20context%2C%20Exception%20error)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%24%22Error%20on%20Partition%3A%20%7Bcontext.PartitionId%7D%2C%20Error%3A%20%7Berror.Message%7D%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Task.CompletedTask%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20public%20Task%20ProcessEventsAsync(PartitionContext%20context%2C%20IEnumerable%20messages)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20foreach%20(var%20eventData%20in%20messages)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20data%20%3D%20Encoding.UTF8.GetString(eventData.Body.Array%2C%20eventData.Body.Offset%2C%20eventData.Body.Count)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%24%22Message%20received.%20Partition%3A%20'%7Bcontext.PartitionId%7D'%2C%20Data%3A%20'%7Bdata%7D'%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20context.CheckpointAsync()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERunning%20the%20above%20code%20will%20result%20in%20the%20following%20output.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ERegistering%20EventProcessor...%0AReceiving.%20Press%20ENTER%20to%20stop%20worker.%0ASimpleEventProcessor%20initialized.%20Partition%3A%20'2'%0ASimpleEventProcessor%20initialized.%20Partition%3A%20'3'%0ASimpleEventProcessor%20initialized.%20Partition%3A%20'0'%0ASimpleEventProcessor%20initialized.%20Partition%3A%20'1'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%200'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%201'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%202'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%203'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%204'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%205'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%206'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%207'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%208'%0AMessage%20received.%20Partition%3A%20'2'%2C%20Data%3A%20'Event%209'%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20can%20be%20seen%20in%20the%20output%2C%20this%20IoT%20Hub%20has%204%20partitions%2C%20and%20the%20events%20are%20being%20processed%20in%20order%20on%20a%20per-partition%20basis.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBehind%20the%20scenes%20EPH%20will%20also%20checkpoint%20its%20progress%20in%20Azure%20Blob%20Storage%20whenever%20you%20call%20CheckpointAsync().%20Looking%20at%20the%20underlying%20storage%20account%20reveals%20four%20(4)%20checkpoint%20files%2C%20one%20per%20partition.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20941px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F130564i203A2409B5012F63%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22blob1.png%22%20title%3D%22blob1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOpening%20the%20partition%20%222%22%20file%20reveals%20the%20following%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%7B%22Offset%22%3A%22154864%22%2C%22SequenceNumber%22%3A2424%2C%22PartitionId%22%3A%222%22%2C%22Owner%22%3A%22host-3a90d623-e19d-45c0-bdff-be7909269f24%22%2C%22Token%22%3A%22a854d208-291e-4586-9cb6-2021c74af1cc%22%2C%22Epoch%22%3A4%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20Offset%2C%20SequenceNumber%20and%20PartitionID%20record%20your%20last%20position%20in%20case%20your%20consumer%20crashes%20and%20needs%20to%20be%20restarted.%20The%20remaining%20information%20relates%20to%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.processor.lease%3Fview%3Dazure-dotnet%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Elease%3C%2FA%3E.%20Using%20EPH%20hides%20much%20of%20this%20checkpoint%20complexity%20for%20you.%20But%20if%20you%20decide%20to%20use%20Event%20Hub%20Client%20instead%2C%20you%20will%20need%20to%20develop%20your%20own%20checkpoint%20code.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1417302601%22%20id%3D%22toc-hId--1417302601%22%3EEvent%20Hub%20Client%3C%2FH2%3E%0A%3CP%3EIf%20Event%20Processor%20Host%20(EPH)%20is%20not%20suitable%20for%20your%20needs%20you%20can%20use%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.eventhubclient%3Fview%3Dazure-dotnet%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EEventHubClient%3C%2FA%3E.%20Use%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.eventhubclient.createreceiver%3Fview%3Dazure-dotnet%23Microsoft_Azure_EventHubs_EventHubClient_CreateReceiver_System_String_System_String_Microsoft_Azure_EventHubs_EventPosition_Microsoft_Azure_EventHubs_ReceiverOptions_%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EEventHubClient.CreateReceiver()%3C%2FA%3Emethod%20and%20specify%20a%20partition%20ID%2C%20which%20returns%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.partitionreceiver%3Fview%3Dazure-dotnet%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EPartitionReceiver%3C%2FA%3Eobject.%20You%20will%20create%20one%20PartitionReceiver%20per%20Consumer%20Group%20%2B%20Partition%20combination%20so%20each%20partition%20has%20only%20one%20consumer.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F128617iF7BEE3415242B15D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22event-hub-consumer1.jpg%22%20title%3D%22event-hub-consumer1.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20following%20code%20sample%20demonstrates%20a%20single%20consumer%20reading%20from%20a%20single%20partition.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eprivate%20static%20async%20Task%20ReceiveMessagesFromPartition(string%20partition%2C%20CancellationToken%20ct)%0A%7B%0A%0A%20%20%20%20receiver%20%3D%20eventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName%2C%20partition%2C%20EventPosition.FromEnqueuedTime(DateTime.Now))%3B%0A%0A%20%20%20%20Console.WriteLine(%24%22Connected%20to%20partition%20%7Breceiver.PartitionId%7D%2C%20consumer%20group%20%7Breceiver.ConsumerGroupName%7D%22)%3B%0A%0A%20%20%20%20while%20(true)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(ct.IsCancellationRequested)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Receive%20a%20maximum%20of%20100%20messages%20in%20this%20call%20to%20ReceiveAsync%0A%20%20%20%20%20%20%20%20var%20ehEvents%20%3D%20await%20receiver.ReceiveAsync(100)%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20ReceiveAsync%20can%20return%20null%20if%20there%20are%20no%20messages%0A%20%20%20%20%20%20%20%20if%20(ehEvents%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Since%20ReceiveAsync%20can%20return%20more%20than%20a%20single%20event%20you%20will%20need%20a%20loop%20to%20process%0A%20%20%20%20%20%20%20%20%20%20%20%20foreach%20(var%20ehEvent%20in%20ehEvents)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Decode%20the%20byte%20array%20segment%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20message%20%3D%20UnicodeEncoding.UTF8.GetString(ehEvent.Body.Array)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%24%22Received.%20'%7Bmessage%7D'%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERunning%20the%20above%20code%20shows%20that%20the%20PartitionReceiver%20is%20receiving%20events%2C%20in%20order%2C%20from%20a%20single%20partition.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3EPress%20ENTER%20to%20exit.%0AConnected%20to%20partition%202%2C%20consumer%20group%20%24Default%0AReceived.%20'Event%200'%0AReceived.%20'Event%201'%0AReceived.%20'Event%202'%0AReceived.%20'Event%203'%0AReceived.%20'Event%204'%0AReceived.%20'Event%205'%0AReceived.%20'Event%206'%0AReceived.%20'Event%207'%0A%E2%80%A6%0AReceived.%20'Event%2099'%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECongratulations%20you%20are%20now%20receiving%20events%20in%20order!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EDon%E2%80%99t%20forget%20to%20periodically%20checkpoint%20(e.g.%20to%20blob%20storage)%20where%20you%20are%20in%20the%20partition%20stream%20so%20if%20your%20consumer%20crashes%2C%20a%20new%20consumer%20picks%20up%20where%20the%20previous%20consumer%20left%20off.%20The%20following%20code%20sample%20from%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.partitionreceiver.receiveasync%3Fview%3Dazure-dotnet%23Microsoft_Azure_EventHubs_PartitionReceiver_ReceiveAsync_System_Int32_%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3Eshows%20how%20you%20can%20determine%20your%20position%20using%20the%20SystemProperties%20%3CSTRONG%3ESequenceNumber%3C%2FSTRONG%3Eor%20%3CSTRONG%3EOffset%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3EEventHubClient%20client%20%3D%20EventHubClient.Create(%22__connectionString__%22)%3B%0APartitionReceiver%20receiver%20%3D%20client.CreateReceiver(%22ConsumerGroup1%22%2C%20%221%22)%3B%0AIEnumerable%20receivedEvents%20%3D%20await%20receiver.ReceiveAsync(BatchSize)%3B%0A%20%20%20%20%20%0Awhile%20(true)%0A%7B%0A%20%20%20%20int%20batchSize%20%3D%200%3B%0A%20%20%20%20if%20(receivedEvents%20!%3D%20null)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20foreach%20(EventData%20receivedEvent%20in%20receivedEvents)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%22Message%20Payload%3A%20%7B0%7D%22%2C%20Encoding.UTF8.GetString(receivedEvent.Body))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%22Offset%3A%20%7B0%7D%2C%20SeqNo%3A%20%7B1%7D%2C%20EnqueueTime%3A%20%7B2%7D%22%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20receivedEvent.SystemProperties.Offset%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20receivedEvent.SystemProperties.SequenceNumber%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20receivedEvent.SystemProperties.EnqueuedTime)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20batchSize%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20Console.WriteLine(%22ReceivedBatch%20Size%3A%20%7B0%7D%22%2C%20batchSize)%3B%0A%20%20%20%20receivedEvents%20%3D%20await%20receiver.ReceiveAsync()%3B%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20calling%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.eventhubclient.createreceiver%3Fview%3Dazure-dotnet%23Microsoft_Azure_EventHubs_EventHubClient_CreateReceiver_System_String_System_String_Microsoft_Azure_EventHubs_EventPosition_Microsoft_Azure_EventHubs_ReceiverOptions_%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EEventHubClient.CreateReceiver()%3C%2FA%3Emethod%20you%20would%20specify%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.eventposition%3Fview%3Dazure-dotnet%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EFromOffset%3C%2FA%3Eor%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.eventposition%3Fview%3Dazure-dotnet%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EFromSequence%3C%2FA%3Enumber%20when%20restarting%20the%20consumer.%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId-522021239%22%20id%3D%22toc-hId-522021239%22%3EAchieving%20Scale%3C%2FH1%3E%0A%3CP%3ENow%20that%20we%20are%20reading%20events%20in%20order%2C%20how%20do%20we%20achieve%20scale%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%2C%20consider%20a%20%3CSTRONG%3E%3CEM%3Efan-out%3C%2FEM%3E%3C%2FSTRONG%3Eapproach%20to%20increase%20the%20number%20of%20partitions%20and%20consumers%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3ECreate%20your%20IoT%20Hub%20and%2For%20Event%20Hub%20with%20lots%20of%20partitions%20to%20increase%20parallelism%0A%3CUL%3E%0A%3CLI%3EBy%20default%2C%20each%20IoT%20Hub%20and%20Event%20Hub%20supports%20up%20to%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fevent-hubs%2Fevent-hubs-faq%23how-many-partitions-do-i-need%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E32%20partitions%3C%2FA%3E.%20This%20value%20must%20be%20specified%20at%20creation%20but%20this%20can%20be%20increased%20to%20128%20by%20creating%20a%20support%20case%20or%20by%20creating%20with%20an%20ARM%20template.%3C%2FLI%3E%0A%3CLI%3EFor%20a%20larger%20number%20of%20partitions%2C%20consider%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fevent-hubs%2Fevent-hubs-dedicated-overview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EEvent%20Hub%20Dedicated%3C%2FA%3E%2C%20which%20offers%202%2C000%20partitions%20per%20CU.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3CLI%3EDistribute%20your%20telemetry%20over%20multiple%20IoT%20Hubs%20and%2For%20Event%20Hubs.%0A%3CUL%3E%0A%3CLI%3EWhen%20provisioning%20IoT%20devices%20using%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fiot-dps%2Fquick-setup-auto-provision%23link-the-iot-hub-and-your-device-provisioning-service%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDPS%3C%2FA%3E%2C%20setup%20a%20policy%20to%20distribute%20those%20device%20over%20multiple%20IoT%20Hubs.%3C%2FLI%3E%0A%3CLI%3EUse%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fiot-hub%2Fiot-hub-devguide-messages-d2c%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EIoT%20Message%20Routing%3C%2FA%3Eto%20fan%20out%20messages%20to%20different%20Event%20Hubs%20endpoints%20based%20in%20the%20message%20payload.%20E.g.%20GPS%20event%20are%20routed%20to%20Event%20Hub%20A%20while%20temperature%20events%20are%20routed%20to%20Event%20Hub%20B.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3CLI%3EEnsure%20there%20is%20one%20consumer%20per%20partition%20per%20IoT%20Hub%20and%2For%20Event%20Hub%20to%20maximize%20parallelism.%3C%2FLI%3E%0A%3CLI%3EEnsure%20you%20configure%20IoT%20Hub%20and%20Event%20Hub%20to%20meet%20your%20throughput%20requirements.%20Don't%20confuse%20the%20number%20of%20partitions%20(degree%20of%20downstream%20parallelism)%20with%20throughput.%26nbsp%3B%3CBR%20%2F%3E%0A%3CUL%3E%0A%3CLI%3EEach%20IoT%20Hub%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fiot-hub%2Fiot-hub-devguide-quotas-throttling%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EUnit%3C%2FA%3Eprovides%20additional%20capacity%20depending%20on%20the%20SKU.%26nbsp%3B%3C%2FLI%3E%0A%3CLI%3EEach%20Event%20Hub%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fevent-hubs%2Fevent-hubs-scalability%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EThroughput%20Unit%3C%2FA%3Eprovides%3A%26nbsp%3B%0A%3CUL%3E%0A%3CLI%3EIngress%3A%20Up%20to%201%20MB%20per%20second%20or%201000%20events%20per%20second%20(whichever%20comes%20first).%3C%2FLI%3E%0A%3CLI%3EEgress%3A%20Up%20to%202%20MB%20per%20second%20or%204096%20events%20per%20second.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F128618i913FFD114BE6A5A8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22event-hub-consumer2.jpg%22%20title%3D%22event-hub-consumer2.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EExpanding%20on%20the%20above%20scenario%2C%20imagine%20a%20fan%20out%20approach%20that%20distributes%20your%20IoT%20devices%20across%206%20IoT%20Hubs%2C%20each%20configured%20with%2032%20partitions%2C%20will%20result%20in%20192%20partitions.%20Your%20event%20processing%20consumer%20microservice%20can%20then%20run%20across%20192%20instances%2C%20with%20each%20instance%20consuming%20from%20a%20single%20partition.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20addition%20to%20the%20fan-out%20approach%2C%20try%20optimizing%20your%20event%20processing%20code.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3ELighten%20the%20work%20performed%20by%20your%20event%20processing%20code.%0A%3CUL%3E%0A%3CLI%3EPut%20the%20bare%20minimum%20code%20in%20place%20to%20process%20the%20event%20in%20order.%20Move%20tasks%20to%20other%20services%20that%20aren%E2%80%99t%20dependent%20on%20ordered%20events.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3CLI%3ERead%20events%20in%20batches%0A%3CUL%3E%0A%3CLI%3ECalling%20IoT%20Hub%20or%20Event%20Hub%20for%20a%20single%20event%20is%20expensive.%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.azure.eventhubs.partitionreceiver.receiveasync%3Fview%3Dazure-dotnet%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EReceiveAsync()%3C%2FA%3Eallows%20you%20to%20read%20in%20batches%20events%20from%20a%20specific%20partition.%3C%2FLI%3E%0A%3CLI%3EExperiment%20with%20different%20batch%20sizes.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3CLI%3EIf%20using%20EventProcessorHost%2C%20set%20the%20following%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.servicebus.messaging.eventprocessoroptions%3Fredirectedfrom%3DMSDN%26amp%3Bview%3Dazure-dotnet%23microsoft_servicebus_messaging_eventprocessoroptions%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EEventProcessorOptions%3C%2FA%3E%3A%0A%3CUL%3E%0A%3CLI%3EMaxBatchSize%20-%20this%20is%20the%20maximum%20size%20of%20the%20collection%20the%20user%20wants%20to%20receive%20in%20an%20invocation%20of%20ProcessEventsAsync.%20Note%20that%20this%20is%20not%20the%20minimum%2C%20only%20the%20maximum.%20If%20there%20are%20not%20this%20many%20messages%20to%20be%20received%20the%20ProcessEventsAsync%20will%20execute%20with%20as%20many%20as%20were%20available.%3C%2FLI%3E%0A%3CLI%3EPrefetchCount%20-%20this%20is%20a%20value%20used%20by%20the%20underlying%20AMQP%20channel%20to%20determine%20the%20upper%20limit%20of%20how%20many%20messages%20the%20client%20should%20receive.%20This%20value%20should%20be%20greater%20than%20or%20equal%20to%20MaxBatchSize.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3CLI%3EDecrease%20the%20frequency%20of%20checkpoints.%0A%3CUL%3E%0A%3CLI%3ECheckpointing%20too%20frequently%20(e.g.%20after%20each%20event%20is%20processed)%20will%20decrease%20the%20performance%20of%20your%20consumer.%3C%2FLI%3E%0A%3CLI%3ECheckpointing%20infrequently%20(e.g.%20after%20event%20100%20events%20are%20consumed)%20will%20cause%20events%20to%20be%20re-read%20from%20the%20partition%20when%20the%20consumer%20restarts%20after%20a%20crash%2C%20thereby%20causing%20duplicate%20messages%20to%20be%20processed.%3C%2FLI%3E%0A%3CLI%3EYou%20will%20need%20to%20weigh%20what%20more%20important%20to%20you...scale%20or%20preventing%20duplicate%20events%20from%20being%20processed.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3CLI%3EUtilize%20asynchronous%20programming%20within%20your%20event%20processing%20code.%0A%3CUL%3E%0A%3CLI%3EHowever%2C%20be%20cautious%20as%20this%20may%20also%20cause%20events%20to%20become%20out%20of%20order.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EIn%20conclusion%2C%20the%20best%20way%20to%20scale%20your%20IoT%20solution%20is%20to%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EAvoid%20or%20minimize%20the%20use%20of%20time%20ordered%20events%3B%20and%2For%3C%2FLI%3E%0A%3CLI%3ESave%20events%20out%20of%20order%20but%20return%20results%20in%20order.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EBut%20if%20your%20solution%20requires%20processing%20events%20in%20order%2C%20hopefully%20you%E2%80%99ll%20find%20the%20guidance%20in%20this%20article%20helpful.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ELinks%3C%2FSTRONG%3E%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ECode%20samples%20shown%20in%20this%20article%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fkhilscher%2FConsistencyExamples%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fkhilscher%2FConsistencyExamples%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EGood%20article%20on%20processing%20discrete%20events%20using%20Azure%20Functions%26nbsp%3B%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fmedium.com%2F%40iizotov%2Fazure-functions-and-event-hubs-optimising-for-throughput-549c7acd2b75%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmedium.com%2F%40iizotov%2Fazure-functions-and-event-hubs-optimising-for-throughput-549c7acd2b75%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-822926%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20594px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F138550i9A318ABFFD30C66D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22things-hero-bkg.jpg%22%20title%3D%22things-hero-bkg.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20are%20thousands%20of%20articles%20on%20the%20Internet%20talking%20about%20how%20the%20cloud%20can%20be%20used%20to%20process%20discrete%20(un-ordered)%20streaming%20IoT%20data.%20But%20almost%20all%20are%20short%20on%20specifics%20and%20the%20design%20trade-offs%20you%20need%20to%20make%20when%20processing%20%3CEM%3E%3CSTRONG%3Eordered%3C%2FSTRONG%3E%3C%2FEM%3EIoT%20streams%2C%20at%20scale.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20article%20discusses%20why%20scale%20can%20be%20difficult%20to%20achieve%2C%20especially%20when%20the%20ordering%20of%20the%20events%20(aka%20%3CSTRONG%3E%3CEM%3ESeries%20events%3C%2FEM%3E%3C%2FSTRONG%3E)%20is%20important.%20Jeff%20Hollan%20wrote%20an%20%3CA%20href%3D%22https%3A%2F%2Fmedium.com%2F%40jeffhollan%2Fin-order-event-processing-with-azure-functions-bb661eb55428%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Earticle%3C%2FA%3Ewhich%20illustrates%20the%20challenge%20when%20using%20Azure%20Functions%2C%20for%20example.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnd%20if%20you%20need%20a%20primer%20on%20%3CSTRONG%3E%3CEM%3EReal-time%20processing%3C%2FEM%3E%3C%2FSTRONG%3E%2C%20%3CSTRONG%3E%3CEM%3EEvents%3C%2FEM%3E%3C%2FSTRONG%3E%2C%20%3CSTRONG%3E%3CEM%3EMessages%3C%2FEM%3E%3C%2FSTRONG%3E%2C%20%3CSTRONG%3E%3CEM%3ESeries%20events%3C%2FEM%3E%3C%2FSTRONG%3Eand%20%3CSTRONG%3E%3CEM%3EDiscrete%20events%3C%2FEM%3E%3C%2FSTRONG%3E%2C%20please%20review%20the%20following%20articles%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Farchitecture%2Fdata-guide%2Fbig-data%2Freal-time-processing%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3EReal%20time%20processing%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fblog%2Fevents-data-points-and-messages-choosing-the-right-azure-messaging-service-for-your-data%2F%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3EEvents%2C%20Data%20Points%2C%20and%20Messages%20-%20Choosing%20the%20right%20Azure%20messaging%20service%20for%20your%20data%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fevent-grid%2Fcompare-messaging-services%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3EChoose%20between%20Azure%20messaging%20services%20-%20Event%20Grid%2C%20Event%20Hubs%2C%20and%20Service%20Bus%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-822926%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EEvent%20Hubs%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EIoT%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EIoT%20Hub%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EIoTHub%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Why are Ordered Events Important?

While many IoT data streams can, and should, be treated as Discrete events, some need to be treated as Series events, where events must be processed in order throughout the solution pipeline.

 

What are Series events? Series events are ordered and interrelated. The consumer needs the sequenced series of events to analyze what happened.

 

An example of Series events is streaming temperature values or GPS coordinates from a moving vehicle. These are time-ordered and often must be processed in order. For example, if the average temperature over the last 5 minutes is trending up, trigger an alert. Another example, if a vehicle's GPS coordinates are streamed to a geofence solution, and you process those events out-of-order, you could have a situation where solution detects the vehicle is leaving the geofence before it had entered the geofence.

 

Other examples include real-time fraud detection and systems that utilize Event Sourcing.

Ways to Handle Ordered Events

Before we delve into the complexities let's discuss other ways of handling ordered events, such as:

 

  1. Do not order events
    • Sounds obvious but often developers assume all events must be ordered. Not so. Many IoT data streams can be treated as Discrete. 
    • Analyze your streaming IoT data and determine which are Discrete events vs Series Events. Try to make most events discrete, as scale is easier to achieve with Discrete events.
  2. Store events out of order but return queries in ordered form
    • The challenge is that you need to land your data first (e.g. in Cosmos DB, Azure Data Explorer, etc.), then have your microservices query (poll) for this data in ordered form.
    • This introduces latency in the solution as the data needs to land first before processing it.
    • If your solution can tolerate this latency then this is a perfectly viable approach.

Failing these options, you will need to process events in order as they arrive. Herein lies the challenge.

Ordered Events and CAP Theorem

Processing ordered events, at scale, is difficult. Why? Blame CAP Theorem.

clipboard_image_0.png

  • Consistency: Every read receives the most recent write or an error.
  • Availability: Every request receives a (non-error) response – without the guarantee that it contains the most recent write.
  • Partition tolerance: The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes.

Much has been written about CAP Theorem and databases, with some database systems allowing you to choose between Consistency and Availability. Azure Cosmos DB is a great example of such as system. It supports tunable consistency levels. However, CAP Theorem can also be applied to IoT data streams and the trade-offs necessary to process ordered events.

 

As with databases, event ingestion services such as Azure IoT Hub, Azure Event Hub, Kafka etc. utilize partitions in order to scale out.

 

CAP theorem implies that in the presence of a network partition, one has to choose between consistency and availability.

 

So, think of Partition as a constant. With Partition as a constant, the choice becomes Consistency or Availability.

 

But what exactly is a partition? According to the Event Hub documentation, a partition is an ordered sequence of events. As newer events arrive, they are added to the end of this sequence. A partition can be thought of as a "commit log."

 

Partitions are a data organization mechanism that relates to the downstream parallelism required in consuming applications. The more partitions, the more parallelism.

The Tradeoff

Here's where the tradeoff comes into play. If you require consistency (where streaming events must be processed in order) you need to ensure that all events from that device arrive in the same partition. In doing so you are trading off availability, as you now have a reliance on individual partitions. You are choosing Consistency over Availability in terms of CAP Theorem.

Achieving Consistency with Event Hub and IoT Hub

In order to guarantee consistency, all events coming from the same device must go to the same partition. Azure IoT Hub already does this for us behind the scenes. All messages from a specific IoT Hub Device Id always end up on the same partition.

 

IotHub.jpg

 

But with Event Hub, you need to do this in your publisher code, as Event Hub has no concept of a device.

Sending events to an Event Hub Partition

There are two ways to guarantee consistency when sending events to Event Hub:

  1. Specify a partition key when sending an event, or
  2. Specify a partition ID to send events to a specific partition.

The first way is to specify a partition key when sending using the SendAsync method of the EventHubClient. Normally this would be a unique device ID or device name. A hash algorithm is run over the partition key to ensure all events with the same partition key land on the same partition.

 

eventhub-sending.jpg

 

The following code example shows 100 events landing on the same partition by specifying a partition key (e.g. “Device1”). Note: The Event Hub was created with 4 partitions, numbered 0 to 3.

 

 

private static async Task SendEventsToEventHub(int numEventsToSend)
{
    for (var i = 0; i < numEventsToSend; i++)
    {
        try
        {
            var eventMsg = $"Event {i}";
            Console.WriteLine($"Sending event: {eventMsg}");
            await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(eventMsg)),"Device1");
        }
        catch (Exception exception)
        {
            Console.WriteLine($"{DateTime.Now} > Exception: {exception.Message}");
        }

        await Task.Delay(10);
    }

    Console.WriteLine($"{numEventsToSend} events sent.");
}

 

 

The issue with this method is you don’t know exactly which partition the event will land on. In this case the sample code shows the events from ‘Device1’ all landing on partition ‘2’.

 

 

Registering EventProcessor...
Receiving. Press ENTER to stop worker.
SimpleEventProcessor initialized. Partition: '2'
SimpleEventProcessor initialized. Partition: '0'
SimpleEventProcessor initialized. Partition: '3'
SimpleEventProcessor initialized. Partition: '1'
Message received. Partition: '2', Data: 'Event 0'
Message received. Partition: '2', Data: 'Event 1'
Message received. Partition: '2', Data: 'Event 2'
Message received. Partition: '2', Data: 'Event 3'
Message received. Partition: '2', Data: 'Event 4'
Message received. Partition: '2', Data: 'Event 5'
Message received. Partition: '2', Data: 'Event 6'
Message received. Partition: '2', Data: 'Event 7'
Message received. Partition: '2', Data: 'Event 8'
Message received. Partition: '2', Data: 'Event 9'

 

 

The second way is to specify an actual partition ID when sending using the SendAsync method a PartitionSender object. Use this option when you need to specifically control which events land on which partition. In this example I have explicitly specified partition ‘0’ when creating the partition sender object.

 

 

static async Task Main(string[] args)
{
    // Creates an EventHubsConnectionStringBuilder object from the connection string, and sets the EntityPath.
    // Typically, the connection string should have the entity path in it, but this simple scenario
    // uses the connection string from the namespace.
    var connectionStringBuilder = new EventHubsConnectionStringBuilder(EventHubConnectionString)
    {
        EntityPath = EventHubName
    };

    eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

    // Specify a specific partition Id to send the messages to.
    partitionSender = eventHubClient.CreatePartitionSender("2");

    await SendEventsToEventHub(100);

    await eventHubClient.CloseAsync();

    Console.WriteLine("Press ENTER to exit.");
    Console.ReadLine();
}

// Uses the event hub client to send 100 events to the same event hub partition
private static async Task SendEventsToEventHub(int numEventsToSend)
{
    for (var i = 0; i < numEventsToSend; i++)
    {
        try
        {
            var eventMsg = $"Event {i}";
            Console.WriteLine($"Sending event: {eventMsg}");
            await partitionSender.SendAsync(new EventData(Encoding.UTF8.GetBytes(eventMsg)));
        }
        catch (Exception exception)
        {
            Console.WriteLine($"{DateTime.Now} > Exception: {exception.Message}");
        }

        await Task.Delay(10);
    }

    Console.WriteLine($"{numEventsToSend} events sent.");
}

 

 

Note: Do not mix PartitionKey and PartitionSender as they are two mutually exclusive concepts.

 

Consuming ordered events from a Partition

Now, how do we consume events from IoT Hub or Event Hub? There are currently two .NET Standard client libraries for reading events from IoT Hub and Event Hub:

 

  • Azure.EventHubs.Processor: Contains the Event Processor Host (EPH) class that provide high level functionality to read events from all partitions, and enables keeping track of processed events via checkpoints. The EPH interface is the easiest way to read from an IoT Hub or Event Hub and always the best place to start.
  • Azure.EventHubs: Provides more granular operations than EPH and is somewhat more complex to use. It does not automatically provide checkpoints. You'll need to add this yourself.

Reading events from IoT Hub or Event Hub is always accomplished through a Consumer Group. As noted in the link, it is recommended that there is only one active receiver on a partition per consumer group. Otherwise you wind up with duplicate messages being processed.

 

Event Processor Host

To consume Series (and Discrete) events from IoT Hub and Event Hub we always recommend using Event Processor Host (EPH). Event Processor Host is used by a number of higher level Azure services (such as Azure Functions - Event Hub Trigger and IoT Hub Trigger) and has a number of advanced, documented capabilities including:

 

  • Scale - Create multiple consumers, with each consumer taking ownership of reading from a few Event Hubs partitions.
  • Load balancing - Increase or reduce the consumers dynamically. For example, when a new sensor type (for example, a carbon monoxide detector) is added to each home, the number of events increases. In that case, the operator (a human) increases the number of consumer instances. Then, the pool of consumers can re-balance the number of partitions they own, to share the load with the newly added consumers.
  • Seamless resume on failure - If a consumer (consumer A) fails (for example, the virtual machine hosting the consumer suddenly crashes), then other consumers must be able to pick up the partitions owned by consumer A and continue. Also, the continuation point, called a checkpoint or offset, should be at the exact point at which consumer A failed, or slightly before that. EPH saves checkpoints to Azure blob storage.

These capabilities are provided through the IEventProcessor interface of the EventProcessorHost class. Consumers register the EventProcessorHost instance with the IoT Hub or Event Hubs service. Registering an event processor class with an instance of EventProcessorHost starts event processing. Registering instructs the Event Hubs service to expect that the consumer app consumes events from some of its partitions, and to invoke the IEventProcessor implementation code whenever it pushes events to consume.

 

According to the documentation

 

"Any given instance of an event processor class will only process events from one partition of one Event Hub."

 

This ensures there is only one consumer per partition and ensures messages are processed in order by your consumer. There is a great example of how this is accomplished in the Event Host Processor documentation.

 

There are four (4) methods to implement in IEventProcessor as shown in the following code sample.

 

 

   public class SimpleEventProcessor : IEventProcessor
    {
        public Task CloseAsync(PartitionContext context, CloseReason reason)
        {
            Console.WriteLine($"Processor Shutting Down. Partition '{context.PartitionId}', Reason: '{reason}'.");
            return Task.CompletedTask;
        }

        public Task OpenAsync(PartitionContext context)
        {
            Console.WriteLine($"SimpleEventProcessor initialized. Partition: '{context.PartitionId}'");
            return Task.CompletedTask;
        }

        public Task ProcessErrorAsync(PartitionContext context, Exception error)
        {
            Console.WriteLine($"Error on Partition: {context.PartitionId}, Error: {error.Message}");
            return Task.CompletedTask;
        }

        public Task ProcessEventsAsync(PartitionContext context, IEnumerable messages)
        {
            foreach (var eventData in messages)
            {
                var data = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
                Console.WriteLine($"Message received. Partition: '{context.PartitionId}', Data: '{data}'");
            }

            return context.CheckpointAsync();
        }
    }

 

 

Running the above code will result in the following output.

 

 

Registering EventProcessor...
Receiving. Press ENTER to stop worker.
SimpleEventProcessor initialized. Partition: '2'
SimpleEventProcessor initialized. Partition: '3'
SimpleEventProcessor initialized. Partition: '0'
SimpleEventProcessor initialized. Partition: '1'
Message received. Partition: '2', Data: 'Event 0'
Message received. Partition: '2', Data: 'Event 1'
Message received. Partition: '2', Data: 'Event 2'
Message received. Partition: '2', Data: 'Event 3'
Message received. Partition: '2', Data: 'Event 4'
Message received. Partition: '2', Data: 'Event 5'
Message received. Partition: '2', Data: 'Event 6'
Message received. Partition: '2', Data: 'Event 7'
Message received. Partition: '2', Data: 'Event 8'
Message received. Partition: '2', Data: 'Event 9'

 

 

As can be seen in the output, this IoT Hub has 4 partitions, and the events are being processed in order on a per-partition basis.

 

Behind the scenes EPH will also checkpoint its progress in Azure Blob Storage whenever you call CheckpointAsync(). Looking at the underlying storage account reveals four (4) checkpoint files, one per partition. 

 

blob1.png

 

Opening the partition "2" file reveals the following:

 

 

{"Offset":"154864","SequenceNumber":2424,"PartitionId":"2","Owner":"host-3a90d623-e19d-45c0-bdff-be7909269f24","Token":"a854d208-291e-4586-9cb6-2021c74af1cc","Epoch":4}

 

 

The Offset, SequenceNumber and PartitionID record your last position in case your consumer crashes and needs to be restarted. The remaining information relates to the lease. Using EPH hides much of this checkpoint complexity for you. But if you decide to use Event Hub Client instead, you will need to develop your own checkpoint code.

 

Event Hub Client

If Event Processor Host (EPH) is not suitable for your needs you can use the EventHubClient. Use the EventHubClient.CreateReceiver() method and specify a partition ID, which returns a PartitionReceiver object. You will create one PartitionReceiver per Consumer Group + Partition combination so each partition has only one consumer.

event-hub-consumer1.jpg

 

The following code sample demonstrates a single consumer reading from a single partition.

 

 

private static async Task ReceiveMessagesFromPartition(string partition, CancellationToken ct)
{

    receiver = eventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partition, EventPosition.FromEnqueuedTime(DateTime.Now));

    Console.WriteLine($"Connected to partition {receiver.PartitionId}, consumer group {receiver.ConsumerGroupName}");

    while (true)
    {
        if (ct.IsCancellationRequested)
        {
            return;
        }

        // Receive a maximum of 100 messages in this call to ReceiveAsync
        var ehEvents = await receiver.ReceiveAsync(100);

        // ReceiveAsync can return null if there are no messages
        if (ehEvents != null)
        {
            // Since ReceiveAsync can return more than a single event you will need a loop to process
            foreach (var ehEvent in ehEvents)
            {
                // Decode the byte array segment
                var message = UnicodeEncoding.UTF8.GetString(ehEvent.Body.Array);
                Console.WriteLine($"Received. '{message}'");
            }
        }
    }
}

 

 

Running the above code shows that the PartitionReceiver is receiving events, in order, from a single partition.

 

 

Press ENTER to exit.
Connected to partition 2, consumer group $Default
Received. 'Event 0'
Received. 'Event 1'
Received. 'Event 2'
Received. 'Event 3'
Received. 'Event 4'
Received. 'Event 5'
Received. 'Event 6'
Received. 'Event 7'
…
Received. 'Event 99'

 

 

Congratulations you are now receiving events in order!

 

Don’t forget to periodically checkpoint (e.g. to blob storage) where you are in the partition stream so if your consumer crashes, a new consumer picks up where the previous consumer left off. The following code sample from here shows how you can determine your position using the SystemProperties SequenceNumber or Offset.

 

 

EventHubClient client = EventHubClient.Create("__connectionString__");
PartitionReceiver receiver = client.CreateReceiver("ConsumerGroup1", "1");
IEnumerable receivedEvents = await receiver.ReceiveAsync(BatchSize);
     
while (true)
{
    int batchSize = 0;
    if (receivedEvents != null)
    {
        foreach (EventData receivedEvent in receivedEvents)
        {
            Console.WriteLine("Message Payload: {0}", Encoding.UTF8.GetString(receivedEvent.Body));
            Console.WriteLine("Offset: {0}, SeqNo: {1}, EnqueueTime: {2}", 
                receivedEvent.SystemProperties.Offset, 
                receivedEvent.SystemProperties.SequenceNumber, 
                receivedEvent.SystemProperties.EnqueuedTime);
            batchSize++;
        }
    }
         
    Console.WriteLine("ReceivedBatch Size: {0}", batchSize);
    receivedEvents = await receiver.ReceiveAsync();
}

 

 

When calling the EventHubClient.CreateReceiver() method you would specify FromOffset or FromSequence number when restarting the consumer.

Achieving Scale

Now that we are reading events in order, how do we achieve scale?

 

First, consider a fan-out approach to increase the number of partitions and consumers:

 

  1. Create your IoT Hub and/or Event Hub with lots of partitions to increase parallelism
    • By default, each IoT Hub and Event Hub supports up to 32 partitions. This value must be specified at creation but this can be increased to 128 by creating a support case or by creating with an ARM template.
    • For a larger number of partitions, consider Event Hub Dedicated, which offers 2,000 partitions per CU.
  2. Distribute your telemetry over multiple IoT Hubs and/or Event Hubs.
    • When provisioning IoT devices using DPS, setup a policy to distribute those device over multiple IoT Hubs.
    • Use IoT Message Routing to fan out messages to different Event Hubs endpoints based in the message payload. E.g. GPS event are routed to Event Hub A while temperature events are routed to Event Hub B.
  3. Ensure there is one consumer per partition per IoT Hub and/or Event Hub to maximize parallelism.
  4. Ensure you configure IoT Hub and Event Hub to meet your throughput requirements. Don't confuse the number of partitions (degree of downstream parallelism) with throughput. 
    • Each IoT Hub Unit provides additional capacity depending on the SKU. 
    • Each Event Hub Throughput Unit provides: 
      • Ingress: Up to 1 MB per second or 1000 events per second (whichever comes first).
      • Egress: Up to 2 MB per second or 4096 events per second.

 

event-hub-consumer2.jpg

 

Expanding on the above scenario, imagine a fan out approach that distributes your IoT devices across 6 IoT Hubs, each configured with 32 partitions, will result in 192 partitions. Your event processing consumer microservice can then run across 192 instances, with each instance consuming from a single partition.

 

In addition to the fan-out approach, try optimizing your event processing code.

 

  1. Lighten the work performed by your event processing code.
    • Put the bare minimum code in place to process the event in order. Move tasks to other services that aren’t dependent on ordered events.
  2. Read events in batches
    • Calling IoT Hub or Event Hub for a single event is expensive.
    • ReceiveAsync() allows you to read in batches events from a specific partition.
    • Experiment with different batch sizes.
  3. If using EventProcessorHost, set the following EventProcessorOptions:
    • MaxBatchSize - this is the maximum size of the collection the user wants to receive in an invocation of ProcessEventsAsync. Note that this is not the minimum, only the maximum. If there are not this many messages to be received the ProcessEventsAsync will execute with as many as were available.
    • PrefetchCount - this is a value used by the underlying AMQP channel to determine the upper limit of how many messages the client should receive. This value should be greater than or equal to MaxBatchSize.
  4. Decrease the frequency of checkpoints.
    • Checkpointing too frequently (e.g. after each event is processed) will decrease the performance of your consumer.
    • Checkpointing infrequently (e.g. after event 100 events are consumed) will cause events to be re-read from the partition when the consumer restarts after a crash, thereby causing duplicate messages to be processed.
    • You will need to weigh what more important to you...scale or preventing duplicate events from being processed.
  5. Utilize asynchronous programming within your event processing code.
    • However, be cautious as this may also cause events to become out of order.

In conclusion, the best way to scale your IoT solution is to:

  • Avoid or minimize the use of time ordered events; and/or
  • Save events out of order but return results in order.

But if your solution requires processing events in order, hopefully you’ll find the guidance in this article helpful.

 

Links: