title: How to track progress status of async tasks running in multiple servers
body: <p>I have multiple async tasks running in spring boot.These tasks read an excel file and insert all that data into the database.</p>
  
  <p>The task is started when a request is made from the front-end. The front-end then periodically keeps requesting for the progress status of the task.</p>
  
  <p>I need to track the progress of each of these tasks and know when they are completed.</p>
  
  <p>This is the controller file that takes in requests for tasks and for polling their progress status:</p>
  
  <pre><code>public class TaskController {
  
      @RequestMapping(method = RequestMethod.POST, value = "/uploadExcel")
      public ResponseEntity&lt;?&gt; uploadExcel(String excelFilePath) {
          String taskId = UUID.randomUUID().toString();
          taskAsyncService.AsyncManager(id, excelFilePath);
  
          HashMap&lt;String, String&gt; responseMap = new HashMap&lt;&gt;();
          responeMap.put("taskId",taskId);
          return new ResponseEntity&lt;&gt;(responseMap, HttpStatus.ACCEPTED);
      }
  
      // This will be polled to get progress of tasks being executed
      @RequestMapping(method = RequestMethod.GET, value = "/tasks/progress/{id}")
      public ResponseEntity&lt;?&gt; getTaskProgress(@PathVariable String taskId) {
          HashMap&lt;String, String&gt; map = new HashMap&lt;&gt;();
  
          if (taskAsyncService.containsTaskEntry(id) == null) {
              map.put("Error", "TaskId does not exist");
              return new ResponseEntity&lt;&gt;(map, HttpStatus.BAD_REQUEST);
          }
  
          boolean taskProgress = taskAsyncService.getTaskProgress(taskId);
  
          if (taskProgress) {
              map.put("message", "Task complete");
              taskAsyncService.removeTaskProgressEntry(taskId);
              return new ResponseEntity&lt;&gt;(map, HttpStatus.OK);
          }
  
          //Otherwise task is still running
          map.put("progressStatus", "Task running");
          return new ResponseEntity&lt;&gt;(map, HttpStatus.PARTIAL_CONTENT);
  
      }
  }
  </code></pre>
  
  <p>This is the code that executes the async tasks.</p>
  
  <pre><code>public class TaskAsyncService {
      private final AtomicReference&lt;ConcurrentHashMap&lt;String, Boolean&gt;&gt; isTaskCompleteMap = new AtomicReference&lt;ConcurrentHashMap&lt;String, Boolean&gt;&gt;();
  
      protected boolean containsTaskEntry(String taskId) {
          if (isTaskCompleteMap.get().get(taskId) != null) {
              return true;
          }
          return false;
      }
  
      protected boolean getTaskProgress(String taskId, String excelFilePath) {
          return isTaskCompleteMap.get().get(taskId);
      }
  
      protected void removeTaskProgressEntry(String taskId) {
          if (isTaskCompleteMap.get() != null) {
              isTaskCompleteMap.get().remove(taskId);
          }
      }
  
      @Async
      public CompletableFuture&lt;?&gt; AsyncManager(String taskId) {
          HashMap&lt;String, String&gt; map = new HashMap&lt;&gt;();
  
          //Add a new entry into isTaskCompleteMap
          isTaskCompleteMap.get().put(taskId, false);
  
          //Insert excel rows into database
  
          //Task completed set value to true
          isTaskCompleteMap.get().put(taskId, true);
          map.put("Success", "Task completed");
  
          return CompletableFuture.completedFuture(map);
      }
  }
  </code></pre>
  
  <blockquote>
    <p>I am using AWS EC2 with a load balancer. Therefore, sometimes a
    polling request gets handled by a newly spawned server which cannot
    access the isTaskCompleteMap and returns saying that "TaskId does not exist".</p>
  </blockquote>
  
  <p>How do I track the status of the tasks in this case? I understand i need a distributed data structure but don't understand of what kind and how to implement it.</p>