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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World
  • Injecting Implementations With Jakarta CDI Using Polymorphism
  • The Energy Efficiency of JVMs and the Role of GraalVM

Trending

  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  • Power BI Embedded Analytics — Part 2: Power BI Embedded Overview
  1. DZone
  2. Coding
  3. Java
  4. Distributed and Guaranteed Executor Service

Distributed and Guaranteed Executor Service

An overview.

By 
Arun Lingala user avatar
Arun Lingala
·
Updated Apr. 01, 20 · Analysis
Likes (6)
Comment
Save
Tweet
Share
8.6K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World
  • Injecting Implementations With Jakarta CDI Using Polymorphism
  • The Energy Efficiency of JVMs and the Role of GraalVM

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!