Using Kafka with Microservices? Most likely you are handling retransmissions incorrectly







Apache Kafka has become the leading platform for asynchronous communication between microservices. It has powerful features that enable you to build fault- and fault-tolerant asynchronous architectures.







. , (, ) , , .







: . , , . , , , .







Kafka



, , Kafka. , Kafka , . .







,



Kafka β€” . Kafka :







(event log), .

(producers, publishers), .

(consumers), .













, RabbitMQ, Kafka , (.. Kafka pull, push ). (offset), ( ) . .









(topics), , . , , , :







  • , .
  • , .




(partitions). . Kafka (partition key). β€” ( , , ), .













UUID . (, UUID ), - .







, , , .







, . Kafka .







Kafka



Kafka . , . , .









, . (.. ), . - , .







, , , ..







. .







(bounded context) - .







Kafka .













, : ( ) . , : , .







(cross-boundary event publishing).







, (aggregates). β€” , . «» , , .







UserAccount. , , User ( ID: , , ..). , (EmailAddress, PhoneNumber ..). , ID ID.







UserAccount , (JSON Avro, ) UserAccount. , ID . UserAccount .







, ?



Kafka , . . , , , : .













?









, , . , … , - .







, . , . : ?







(retry)?



, (.. ), . , ?







, . . , .







, ?



. , β€œ ”, UserAccount, . (, HTTP 400) .







, . , -, . , .







, . , . , , , .







, , .







?



. , , , , . : , .







Retry topic:



, , (retry topic). , :







  1. .
  2. , , .
  3. , , . . , .
  4. , ( (backoff strategy)). , , (dead letter queue, DLQ), .








retry-topic ( ) , . , , 1 , . , , . , , (dead letter queue, DLQ)







?



, . . , . , , .









: , : .







(recoverable errors) β€” , , , . , . , , . , .







- : , . , , . ( . «» , β€” β€” . , - β€” β€” .)







, . , . () . - , , ..







(non-recoverable errors) β€” , , , . , NullPointerException. - .







, , . , , , ..







, , , «» ( «» : !) , , NULLS , .







?







, . , , . , , . , , … , .







? . , (, ).







, , . , …









. Kafka. , ID .







? , .







. ? , . , , , . .







. User , . Zoey Zo… Zoiee. , (, Login) Zoiee, Zo.







Login User. , Zoiee : Β« , Zo!Β»







retry topic. . , Zo, , . , Zoiee, , Zo.







Zoiee/Zo . . , . , - , . , , , .







retry topic ?



, . , , . , , β€” . :







  • , - .
  • , ( , .
  • , .


.









. . . β€” , .. β€” .







. . , . , , . , , .







, 100% , :







  • , , ,
  • , .


?



, , , ?







, , , . , .









, . , , .







, :







void processMessage(KafkaMessage km) {
 try {
   Message m = km.getMessage();
   transformAndSave(m);
 } catch (Throwable t) {
   if (isRecoverable(t)) {
     // ...
   } else {
     // ...
   }
 }
}
      
      





- Java isRecoverable()



(whitelist approach) , t . , t, (, SQL REsT), true, . , false. .







, . , SQLException () (). , , , , stash topic ( -), … , .









, . , . , .







, .







. . -Java - :







void processMessage(KafkaMessage km) {
 try {
   Message m = km.getMessage();
   transformAndSave(m);
 } catch (Throwable t) {
   if (isRecoverable(t)) {
     doWithRetry(m, Backoff.EXPONENTIAL, this::transformAndSave);
   } else {
     // ...
   }
 }
}
      
      





(, , , , ).







,



, , , , , . ? . n , DLQ. ?







, - , . DLQ, . .









, . User/Login. Login , Zo. , . Zoiee .













Zo , Zoiee . .







, , . Zo , .













Zo, . User Zoiee, Login Zo.







, . Zo Login Zoiee, . , .







ID , , ID. , … .







, (: , !). . , -. . , ( , ), .







, , . , :







  • , ;
  • , .


, , -.







?



. , . , .







, ( β€œ ”) . , .









, , . . Kafka, . , β€” , β€” , .







, , , .







, . , , :







  • , Kafka , .
  • .
  • , .
  • Kafka , . ? . , ? .
  • , .


:

, Apache Kafka, Stripe. . , .







:










All Articles