Построение микросервисных систем с использованием Kafka

Mikalai Saskavets

Построение микросервисных систем с использованием Kafka

Mikalai Saskavets

iTechMeetup Mogilev #5

Mikalai Saskavets

Full Stack Web Developer

DevOps Enthusiast


• more than 12 years in web development

some experience with: php javascript python java perl golang lua nginx apache
postgres mysql redis rabbitmq cassandra dynamodb kafka docker kubernetes ansible
solr elasticsearch jquery react webpack django celery wordpress linux git latex


• last 6 years is falling into Python
• member of PyCon Belarus Program Committee
• contributing member of Minsk Python Meetup
art by René Aigner

Monolithic MVP

Microservices: Coupling and Cohesion?

Distributed Monolith?

Microservices: Synchronous Communication

( REST API )

Microservices: Synchronous Communication

( REST API )

Microservices: Async Communication (Celery)

Microservices: Async Communication (Celery)


shall we use
Kafka here???

What is Kafka?

What is Kafka?

Distributed Streaming Platform
Initially conceived as a messaging queue... Now Apache Kafka is a full-fledged distributed event streaming platform capable of handling trillions of events a day. [↪]

Kafka for what??

Messaging Metrics Website Activity Tracking Log Aggregation Stream Processing Event Sourcing Commit Log
Queue

Why Kafka???

up to 2,000,000 messages per sec
or even more... [↪]

Alternatives for Kafka

RabbitMQ ActiveMQ
NATS Apache Pulsar NSQ
AWS Kinesis

Kafka: Overview

see some details on dzone.com

Kafka: Partitions for Topics

Fig. from mrsrinivas

Kafka: Partitions: few consumer groups

derivative of the fig. from mrsrinivas

Kafka: Consumers Offsets Example for 1 Partition




          ┌───[Consumer Group 1]
          │
1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18
    │
    └───[Consumer Group 2]
            

Kafka: Consumers Offsets Example for 1 Partition




                  ┌───[Consumer Group 1]
                  │
1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20
      │
      └───[Consumer Group 2]
            

Kafka: Consumers Offsets Example for 1 Partition




                           ┌───[Consumer Group 1]
                           │
1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23
│           │
│           └───[Consumer Group 2]
│
└───[Consumer Group 3]
            

Kafka: Consumers Offsets Example for 1 Partition


                                    [Consumer Group 4]───┐
                                                         │
                           ┌───[Consumer Group 1]        │ 
                           │                             │
1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23
│           │
│           └───[Consumer Group 2]
│
└───[Consumer Group 3]
            

Kafka: Reset Consumer Group Offsets tooling


  Reset to Datetime 
  Reset from Duration
  Reset to Earliest
  Reset to Latest
  Reset to Current Time
  Reset to Offset
  Shift Offset by 'n' 
  Reset from file

            

Kafka: Consumers Offsets Example for 3 Partitions


                        ┌───[CG 1 C 1] 
                        │ 
Partition 1 >> 11|42|73|104|135|166|197|228|259
                     │
                     └───[CG 2 C 1]

                  ┌───[CG 1 C 2] 
                  │ 
Partition 2 >> 21|52|83|114|145|176|207|238
                  │
                  └───[CG 2 C 2]

                            ┌───[CG 1 C 3] 
                            │ 
Partition 3 >> 31|62|93|124|155|186|217|248
               │
               └───[CG 2 C 3]
 
            
legend:

CG — Consumers Group
C — Consumer (in group)

Kafka: Overview, again :-)

see some details on dzone.com

Kafka: Cluster

Kafka: Partition Leaders

Kafka: Replication

some details on dzone.com

Kafka: Message structure

key, value
byte[], byte[]

Kafka: Serializers

JSON, Protobuf, Thrift, Avro

Kafka: Schema overhead

Fig. from confluent.io

Kafka: Schema Registry Concept

Kafka: some conclusion

Microservices?

Microservices: Async Communication (Celery)

Microservices: Async Communication (Kafka)

Microservices: Async. What about databases?

Relational Database → Kafka

Stream data from RDBS to Kafka

Fig. from debezium.io

Stream data from PostgreSQL to Kafka

Fig. from debezium.io

Microservices: Async. What about databases?

Microservices: Async. Debezium

Microservices: Async. Debezium. Full

Application Diagram

Application Diagram. External Data Handling

Conclusion?

Thanks!
Questions?