Code Review: what is wrong with the updateRoute handler code? (gRPC call inside a transaction, Kafka inside a transaction, duplicate validation, lack of error handling)
sobes.tech AI
Answer from AI
There are several serious issues in the updateRoute handler code:
-
gRPC call inside a transaction — this is dangerous because a gRPC call can be long-running and non-atomic. Transactions should be short and contain only database operations. External service calls are better made after committing the transaction.
-
Kafka inside a transaction — if message sending to Kafka occurs within a transaction, the message might be sent even if the transaction is rolled back, leading to data inconsistency. Patterns like outbox or transactional integration should be used.
-
Duplicate validation — if data validation is repeated in multiple places, it complicates maintenance and can lead to errors. Validation should be centralized.
-
Lack of error handling — errors during gRPC, Kafka, or database calls should be handled properly to prevent crashes and ensure correct system behavior.
Recommendations:
- Move external calls (gRPC, Kafka) outside of transactions.
- Use patterns for guaranteed message delivery.
- Centralize validation.
- Add error handling with logging and rollback if necessary.