BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Simplify HTTP Request Buffering with Queue-Level Routing Configuration and BufferTask API

Simplify HTTP Request Buffering with Queue-Level Routing Configuration and BufferTask API

This item in japanese

Bookmarks

Google recently added two new features for Cloud Tasks with queue-level routing configuration and BufferTask API in public preview. The two new features combined enable creating of HTTP tasks and adding to a queue without needing the Tasks client library.

Cloud Tasks is a fully-managed service on the Google Cloud  Platform that manages the execution, dispatch, and asynchronous delivery of many tasks to App Engine or any arbitrary HTTP endpoint. For example, developers can use the service to add standalone tasks, such as an HTTP request, to a queue, which remain there until they are asynchronously executed by an App Engine application or any other HTTP endpoint of choosing. In addition, the service offers several resiliency features, such as task deduplication, guaranteed delivery, and rate and retry controls.

However, HTTP Tasks come with a caveat as the Cloud Tasks client library needs to wrap the HTTP requests into tasks and add them to a queue. This creates a dependency between callers and the Cloud Tasks client library. Mete Atamel, a Google developer advocate, wrote in a Google blog post:

The burden of creating the task should really be on the target service rather than the caller, as the target service is the one benefiting from the queue. The new queue-level routing configuration and the BufferTask API address this problem and provide an easier way to create tasks.

With the queue-level task routing configuration feature, developers can change the HTTP task routing for the entire queue for all new and pending tasks. This will make creating tasks more manageable as the HTTP target doesn’t need to be set at the task level. Furthermore, the control shifts to the service provider, which is better equipped to define the objective of all the tasks in a queue. For instance, the service provider can redirect traffic to an alternate backend if the original is unavailable.

Source: https://cloud.google.com/blog/products/serverless/buffer-http-requests-with-cloud-tasks

The BufferTask API is another feature in Cloud Tasks that enables developers to generate an HTTP task without inputting any task specifications, including the HTTP URL, headers, and authorization. Instead, they can send a standard HTTP request to the Buffer API, which then packages the request into an HTTP task utilizing the default settings within the queue-level routing configuration.

The Target URI configuration must be set for the queue to utilize the BufferTask API. This implies that the queue-level routing configuration feature must be enabled as a prerequisite for using the BufferTask API.

var BufferTaskApiUrl = $"https://cloudtasks.googleapis.com/v2beta3/projects/{ProjectId}/locations/{Location}/queues/{Queue}/tasks:buffer";

using (var client = new HttpClient())

{

    client.DefaultRequestHeaders.Add("Authorization", $"Bearer {AccessToken}");

    var response = await client.GetAsync(BufferTaskApiUrl);

    var content = await response.Content.ReadAsStringAsync();

    Console.WriteLine($"Response: {content}");

}

When asked by InfoQ about what benefits developers using these features, here is what Atmel had to say:

Buffering is useful when you want to protect your services against a burst of traffic or load. For example, if your service can only process 5 requests per second, there's no point in sending 100 requests per second and overwhelming your service. Instead, you can add a queue in front of your service and drain the requests from the queue at a rate acceptable to your service.

In addition, he added:

With these features, the caller of the service does not need to create tasks anymore but instead just sends regular HTTP requests, and the Cloud Tasks queue handles the rest with queueing, draining, and rate limiting.

Lastly, pricing details of Cloud Tasks are available on the pricing page. In addition, more details and guidance on the new features are available in the GitHub Cloud Tasks samples repo and hands-on code lab.

About the Author

Rate this Article

Adoption
Style

BT