Junior
Architectural task: there is a Users service and a Notification service. When registering a user, it is necessary to guarantee sending a message to Kafka (for sending email). How to ensure delivery guarantee if Kafka is unavailable?
sobes.tech AI
Answer from AI
To guarantee message delivery in Kafka when registering a user, even if Kafka is temporarily unavailable, you can use the Outbox pattern:
- When registering a user in the Users service, save the user data and the Kafka message in a single database transaction (for example, in an outbox table).
- A separate process or worker periodically reads unconfirmed messages from the outbox table and attempts to send them to Kafka.
- After successful sending, the message is marked as sent.
Thus, even if Kafka is unavailable at the time of registration, the message will not be lost and will be sent later.
An example of a simplified schema:
// In a transaction
saveUser(user);
saveOutboxMessage(userId, "UserRegistered");
// Separate process
List<Message> messages = readUnsentOutboxMessages();
for (Message msg : messages) {
try {
kafkaProducer.send(msg);
markMessageAsSent(msg);
} catch (Exception e) {
// log and retry later
}
}
This ensures reliability and consistency between the service and Kafka.