Rafael Souza
Rafael Souza

Reputation: 1351

How many kafka topics to create for an api?

I'm using kafka for my api. I'm using spring with microservice I'll post my kafka code below:

Command:

private static final Logger logger = 
        LoggerFactory.getLogger(UserCommandServiceImpl.class);

@Autowired
private KafkaTemplate<String, Object> kafkaTemplate;

public void sendMessage(User objeto) 
{
    
    logger.info(String.format("Message sent -> %s", objeto.toString()));
    this.kafkaTemplate.send("quickstart-events", objeto);

}

Query:

private final Logger logger = LoggerFactory.getLogger(UserQueryServiceImpl.class);

   
@Autowired
private MongoTemplate mongoTemplate;

@KafkaListener(topics = "quickstart-events" , groupId = "group-id")
public void consume(String message) 
{
    logger.info(String.format("Message recieved -> %s", message));
    mongoTemplate.insert(message, "user");
    
}

I installed kafka from that site: I'm using CQRS Pattern so each query is a microservice and command another. My question is simple for each microservice I create a kafka topic? Thanks!

Upvotes: 1

Views: 97

Answers (1)

Victor
Victor

Reputation: 8490

Imagine a Kafka Topic as a database table, use one topic per kind of data.

If you are wondering how you can scale your application, you may ask how many partitions your topic should have. A topic is a set of partitions that will handle all data.

Take a look at the image below, a topic will receive values from more than one producer and it will have just one kind of message. A message can be stored in any partition and this is defined by the message key.

A topic contains more than one partition and the event will be sent to one partition

Upvotes: 1

Related Questions