1

We have rest-api based java service (spring REST application in Tomcat server), which is basically handling the CRUD operation against the database. Now we have a requirement to consume some kafka events to update few tables.

Is it good practice keep the kafka consumers inside the rest api based java application (or) can we have dedicated service which handles the kafka events.

Queries:

1). If we include these consumers in rest application, How Kafka consumers will behave along with rest api. for example if there is request received from rest api call & same time a kafka event is available to consume (or) already in processing.

2). If we have kafka consumer inside the rest api application, how we can gracefully deploy / rollout the code changes, Considering in rest based app we can drain the connection to the containers from load-balancers while shutting them down.

3). Maintaining the number of consumers and kafka topic partitions during auto-scaling (i.e: During auto scaling of pods).

1 Answer 1

1

I have some experience on designing and developing micro services CRUD repositories and Kafka consumers, there are always advantages and disadvantages having Kafka consumers in CRUD repository API. Let's discuss some of those below

Kafka Consumer In CRUD Repository :

There is will be no problem on transactions or data wise, because Kafka consumers will run on the Kafka Container Listener threads and API requests will execute on Tomcat threads. Which ever thread executes threads 1st will update/insert the row in table and the another thread follows.

  • Advantages

    • Single application having consumer and CRUD operations, easy maintenance and single application deployment.
    • There will be no additional TPS since consumer is inside the application.
    • Easy to handle and retry the failure messages, like having the DB connection you can have retry/failures tables for messages those are failed to actually insert into the table.
  • Disadvantages

    • It's a single point failure, if there is any issue with the Kafka consumer it might bring down the whole application and API requests are effected.
    • There might be horizontal scaling issues or resource not being used efficiently, if the cluster is having 10 pods and Kafka partitions are less than 10, all the pods will not consume the data since partitions are less than Kafka consumer threads. And vice versa if you have more partitions to consume and spin up more pods but you actually don't need many pods since the TPS is low.

Kafka Consumer As Different Micro service :

I personally prefer this approach because we have more controller over horizontal and vertical scaling as needed and not dependent on API.

  • Advantages

    • Individual micro service with more control and no single point failure.
    • Easy to horizontal or vertical scaling.
  • Disadvantages

    • This is not complete disadvantage, but bit of more effort is need on handling API failure.
    • Need to implement proper alerting system with retry mechanism. Like for example pausing the consumer when API is failing etc..
    • Rest API calls with negligible network latency which will not have in 1st approach.
Sign up to request clarification or add additional context in comments.

2 Comments

thanks @Ryuzaki L for the detailed response, I Have a question in the disadvantage of kafka consumers in different micro-service. All the three points mentioned also applicable to rest + kafka consumer (i.e: first approach) as well, Since in first approach also we need to handle consumer message processing errors / message ack & retries. appreciate your clarifications.
the main disadvantage using 1st approach is scaling and single point failure, your API can come down if there is any bug or error in consumer code @KannanK

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.