Sobes.tech
Middle

Tell about asynchronous processing via Kafka: what data was transmitted, what keys were used, how was the order of processing ensured?

sobes.tech AI

Answer from AI

When processing asynchronously through Kafka, messages with data that need to be processed or transferred between services are usually sent.

  • Data: can be anything — events, commands, computation results, logs, etc. The format is often JSON, Avro, Protobuf.
  • Keys: are used for partitioning messages. The key determines which partition the message will go to, which is important for processing order.

To ensure message processing order:

  • Use keys that logically group related messages (e.g., user ID, order ID, etc.).
  • Kafka guarantees the order of messages within a single partition.
  • For processing messages in order, all messages with the same key should go to one partition.

Example:

from kafka import KafkaProducer
import json

producer = KafkaProducer(bootstrap_servers='localhost:9092', value_serializer=lambda v: json.dumps(v).encode('utf-8'))

order_id = '12345'
message = {'order_id': order_id, 'status': 'created'}

# Key — order_id, so all events for the order go to one partition
producer.send('orders_topic', key=order_id.encode('utf-8'), value=message)
producer.flush()

Thus, processing order is guaranteed for messages with the same key, and scalability is achieved through multiple partitions and keys.

Tell about asynchronous processing via Kafka: what… - sobes.tech