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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • The Java Story: The Official Documentary Is Here
  • Multithreading in Modern Java: Advanced Benefits and Best Practices
  • Optimizing Java Applications for Arm64 in the Cloud

Trending

  • The Three Tiers of Agentic Incident Response: When to Trust AI Autonomy
  • Applying Microservices Lessons to Multi-Agent AI Systems
  • Beyond the Endpoint: How Agent Cards Enable Discovery, Interoperability, and Trust
  • Giving AI Agents Safe Access to Legacy Systems: Introducing legacy2mcp

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. Distributed and Guaranteed Executor Service

Distributed and Guaranteed Executor Service

An overview.

By 
Arun Lingala
·
Updated Apr. 01, 20 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Sometimes the start is the most difficult part.

Sometimes the start is the most difficult part. 
You may also like: Using Java's Future and ExecutorService

If you have ever implemented an async API/Event listener then the following would have been the typical implementation

Typical async implementation

High-Level Diagram of the Implementation 


  • Validate the payload.
  • Extract the needed parameters from the payload into a runnable and submit it to JDK Executor service.
  • Respond with a correlation ID in case of an API and acknowledge the event broker in case of async event listener respectively.
  • JDK Executor threads poll from the queue and execute them asynchronously.

When you have implemented as above and raised a code review, have you ever been stumbled over the following questions from reviewers?

Image title

  1. How the submitted runnable execution is guaranteed in the below cases?
    • JVM crashes when the executor service queue is piled up with 100s of runnable.
    • JVM crashes while executing a runnable.
  2. If runnable execution fails due to unavailability of a downstream service
    • How to retry the failed execution?

Well, we also have faced the same questions for our async implementations. 

Following is the typical answer to the above as most of us could imagine.

  • Validate the payload.
  • Extract the needed parameters from the payload and store it in the DB table.
  • Implement a scheduled job that picks each record from the DB table, executes it and maintains the state.

We have stopped here.

We would welcome a new set of challenges with scheduler and state management etc. and need to rewrite the whole implementation.

So, I decided to delve deeper into JDK Executor service implementation to figure out a solution and following are the questions that we discussed while doing so.

Q1: How does the Executor service store a runnable?

A1: It stores in in-memory(heap) Linked Blocking Queue.

The root cause for the first challenge: Runnables are stored in heap. Hence, they are potentially at risk as the heap is cleared when JVM crashes.

Q2: It is possible to change the implementation of Executor service?

A2: Yes, indeed.
Image title

What Does it Mean?

Instantiate the Executor service thread pool with appropriate BlockingQueue that stores the runnable out of JVM heap memory.

Q3: Now, how to implement such a BlockingQueue?
A3: Let's try to find out of the box solutions.

Q4: Did we find it?

A4: Yes, indeed. A beautiful open source library Redisson that has BlockingQueue implementation based on Redis and more.

Image title

High-Level Diagram of the Above Proposal Implementation 


Q5: How does the solution be the savior from JVM crashes?

A5: Indeed, it is for the following reasons.

If JVM is crashed when it has submitted several runnable but not executed them

  • If the service cluster has more than one JVM then another JVM can pick them from the queue and execute as Executor service threads across service cluster poll the same runnable queue i.e. based on Redis.
  • If the service cluster has only one JVM then the re-spawned JVM will pick them from the queue and execute.

 WOW, we solved the first challenge i.e. guaranteed execution.

We could NOT get out of the box solution for the second challenge i.e. retrying failed runnable execution.

Q6: How did we solve the second challenge?

A6: We have enhanced the Redisson BlockingQueue implementation as following:

  • Keep a copy of runnable in Redisson RMap.
  • If runnable execution fails due to any re-triable errors such as downstream unavailability then keep the copy otherwise remove it.
  • Implemented a scheduler that periodically scans the RMap and puts its elements into the Redisson BlockingQueue so that the Executor service threads could pick them and execute.

Thus, we have solved the challenges posed to us with very minimal extra code and zero modification to the already implemented code.


Further Reading

A Deep Dive Into the Java ExecutorService

Java Multi-Threading With the ExecutorService

Executor (software) Java (programming language) Java virtual machine Implementation

Opinions expressed by DZone contributors are their own.

Related

  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • The Java Story: The Official Documentary Is Here
  • Multithreading in Modern Java: Advanced Benefits and Best Practices
  • Optimizing Java Applications for Arm64 in the Cloud

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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