I was reading through the NSX API Guide recently and a section caught my attention: Configuring Edge Services in Async Mode.
When configuring edge services asynchronously, accepted commands return an Accepted status (HTTP response status code 202) and a jobId (or taskId). The advantage of the async mode is that APIs are returned very fast and actions are done behind the scene.
This asynchronous mode is working with any 4.0 service configuration URL for POST, PUT, and DELETE calls (edge deployment, services configuration, reboots and so on). To configure async mode, simply include ?async=true at the end of the request.
How To Configure A NSX Edge Service Asynchronously?
Let’s compare a request, with and without the async mode. The request below will create a TCP Service Monitor in “normal/sync” mode.
The API call returned the following after completion :
- HTTP response status code: 201 Created
- Time: almost 8 seconds*
* This is a lab environment with limited resources; this length might not reflect the effective duration in a production infrastructure.
Let’s launch the same request, but asynchronously this time (I only changed some values in the request body, to create a service monitor with different values):
The API call finished this time with:
- HTTP response status code: 202 Accepted (as opposed to the 201 Created above)
- Time: 1 second
The HTTP response status code of 202 indicates the successful start of an asynchronous action. In addition to this code, the response headers contain the jobId which can be used to retrieve the status of the job.
To get the status of the task, you can check the status of the returned jobId. You can also query all jobs or only active ones (see the screenshot below).
When querying the job status, the call returns the URI and ID of the resource and the status which can be one of the following.
Async Mode? Yes, but for what?
Although the example above is quite basic, it shows the benefits of asynchronous actions: APIs are returned very fast and other actions can be done in parallel. In a highly automated world, it is beneficial for automation workflows to continue while some actions are executed in background. Async actions can also help to build user interfaces that can live refresh.