Sobes.tech
Senior

How to solve the problem of duplicate notifications when there are multiple instances of the job service?

sobes.tech AI

Answer from AI

The problem of duplicate notifications when running multiple instances of a job service occurs because each instance independently processes the same tasks.

Solutions:

  1. Using distributed locking — before sending a notification, an instance attempts to acquire a lock in a distributed storage (e.g., Redis with Redlock). Only the instance that acquires the lock sends the notification.

  2. Message queue with guaranteed single processing — use a message broker (Kafka, RabbitMQ) configured so that each notification is processed exactly once by a single consumer.

  3. Identification and deduplication of notifications — add a unique identifier to each notification and check in the database or cache whether it has already been sent.

  4. Leader election — one instance becomes the leader and is responsible for sending notifications, others do not.

Example with Redis Redlock (Java):

// Acquire lock before sending notification
RLock lock = redissonClient.getLock("notification_lock");
boolean isLocked = lock.tryLock(100, 1000, TimeUnit.MILLISECONDS);
if (isLocked) {
    try {
        // Send notification
    } finally {
        lock.unlock();
    }
}
How to solve the problem of duplicate notifications… - sobes.tech