DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Efficient and Fault-Tolerant Solutions for Distributed Mutual Exclusion
  • Non-Volatile Random Access Memory: Key Guidelines for Writing an Efficient NVRAM Algorithm

Trending

  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka
  • Designing Agentic Systems Like Distributed Systems
  • How We Diagnosed a Hidden Scheduler Failure in a Docker Swarm Cluster Serving 2 Million Users
  • Beyond Conversation: Mastering Context with Claude Code Skills and Agents

Simple Mutual Exclusion

Learn how to increase your services' availability through a mutual exclusion mechanism, like the one we'll set up in this tutorial.

By 
Yusuf Aytaş user avatar
Yusuf Aytaş
·
Jul. 15, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
16.3K Views

Join the DZone community and get the full member experience.

Join For Free

Nowadays, for almost all services, we would like to set up at least poor man’s HA. This means we would have more than one node/server/pod at a time. This is great for load balancing and availability purposes. Nevertheless, there’s a simple problem with this setup. What if you want to execute a piece of code in only one node? We can do this via a simple mutual exclusion mechanism. There are various ways to this but I would like to limit this post to achieve mutual exclusion with Java and Spring Framework.

Now, imagine you have a cron job that sends an e-mail to your customers at 9 a.m. every day. You wouldn’t want to send the same e-mails twice, right? If you are using Spring Framework, you can simply do this via Scheduled annotation. A typical code would look like:

@Scheduled(cron = "59 59 8 * * *" /* Every day at 8:59:59am */)
public void sendEmails() {
  List<Email> emails = emailDAO.getEmails();
  emails.forEach(email -> sendEmail(email));
}

So, we need one node to execute above not the others. It would be great if we can do something like this right?

@Scheduled(cron = "59 59 8 * * *" /* Every day at 8:59:59am */)
@TryLock(name = "emailLock", owner = NODE_NAME, lockFor = TEN_MINUTE)
public void sendEmails() {
  List<Email> emails = emailDAO.getEmails();
  emails.forEach(email -> sendEmail(email));
}

Thus, I’ve implemented a little library to exactly do this. Currently, it offers synchronization through Postgres and MySQL but one can extend it to other technologies like ZooKeeper, Consul and etcd as well.

By the way, this isn’t just about crons. You can also synchronize over different stuff as well. For instance, you might want to process a message once even if it’s received by multiple nodes.

@Component
class MessageProcessor{
  ...
  @TryLock(name = "messageProcessor", owner = NODE_NAME, lockFor = TEN_MINUTE)
  public void processMessage(Message message) {
    ...
  }
}
@Component
class MessageService{
  ...
  @PostConstruct
  public void initProcessor(){
    while (true) {
      Message message = pollMessage();
      messageProcessor.processMessage(message);
    }
  }
}

In consequence, you can implement a simple lock with a timeout with dlock.

Mutual exclusion

Published at DZone with permission of Yusuf Aytaş. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Efficient and Fault-Tolerant Solutions for Distributed Mutual Exclusion
  • Non-Volatile Random Access Memory: Key Guidelines for Writing an Efficient NVRAM Algorithm

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook