Pulling distributed request status

 In an e-commerce website, microservices often communicate with the frontend (UI) through APIs. When a specific microservice operation is completed, such as a payment transaction, the microservice sends a response back to the frontend. This response typically includes a status code and a message indicating whether the operation was successful or failed.


In a distributed system, operations are often asynchronous and use a queue for processing. The UI can still be updated based on the status of these operations, but it requires a different approach. Here’s a general idea of how it might work:

  1. When an operation is initiated (like a payment), a message is sent to a queue to be processed. At the same time, a record of the operation (like a payment record) is created in a database with a status of ‘pending’.

  2. The frontend polls the backend at regular intervals to check the status of the operation. This could be done using a WebSocket for real-time updates or regular HTTP requests for less immediate updates.

  3. When the operation is processed, the microservice updates the record in the database with the new status (‘success’ or ‘failure’).

  4. The next time the frontend checks the status of the operation, it sees the updated status and updates the UI accordingly.

Here’s a simplified example in JavaScript:

// Send request to start operation
fetch('/api/startOperation', {
  method: 'POST',
  body: JSON.stringify(operationData),
})
.then(response => response.json())
.then(data => {
  // Operation started, now poll for status
  const operationId = data.operationId;
  setInterval(() => {
    fetch(`/api/checkStatus?operationId=${operationId}`)
    .then(response => response.json())
    .then(data => {
      if (data.status === 'success') {
        // Update UI for successful operation
        // Proceed to the next step
      } else if (data.status === 'failed') {
        // Update UI for failed operation
      }
    });
  }, 5000); // Check status every 5 seconds
})
.catch((error) => {
  console.error('Error:', error);
});

In this example, operationData is the data needed to start the operation, and /api/startOperation and /api/checkStatus are the endpoints of the microservice. The fetch function sends a request to start the operation, and the setInterval function starts polling for the status of the operation.

Please note that this is a simplified example. In a real-world application, you would need to handle more cases (e.g., different types of failures) and secure the operation data. You would also likely use a state management library or framework to manage the UI state.

Remember, it’s crucial to handle these operations securely and provide clear messages to the user about the status of their operations. This helps build trust and provides a good user experience. Also, keep in mind that polling too frequently can put unnecessary load on your servers, so choose an appropriate interval based on your specific needs.



We can also use a push mechanism like SignalR to notify the UI about the status of an operation in a microservice architecture. SignalR is a library for ASP.NET developers that simplifies the process of adding real-time web functionality to applications. Real-time web functionality is the ability to have server-side code push content to connected clients instantly as it becomes available.

Here’s a simplified example of how it might work:

  1. The frontend initiates an operation (like a payment) by sending a request to the backend.
  2. The backend receives the request and sends a message to a queue to be processed by the appropriate microservice. It also creates a record of the operation in a database with a status of ‘pending’.
  3. The microservice processes the operation. Once it’s done, it updates the status of the operation in the database and sends a message to the backend.
  4. The backend receives the message from the microservice and uses SignalR to push a message to the frontend with the status of the operation.
  5. The frontend receives the message from the backend and updates the UI accordingly.

In this scenario, the frontend doesn’t need to poll the backend for the status of the operation. Instead, it receives updates directly from the backend as they happen. This can lead to a more responsive and user-friendly UI.

Remember, it’s crucial to handle these operations securely and provide clear messages to the user about the status of their operations. This helps build trust and provides a good user experience. Also, keep in mind that setting up and managing a real-time push mechanism like SignalR can be more complex than a simple polling mechanism. You’ll need to consider factors like connection management, error handling, and scalability.

Vikash Chauhan

C# & .NET experienced Software Engineer with a demonstrated history of working in the computer software industry.

Post a Comment

Previous Post Next Post

Contact Form