UCWA by the numbers - #6 When to Batch

When developing a UCWA application it is usually worthwhile to consider the types of data being request and potentially the frequency. UCWA exposes the ability to batch requests into a single message via the Batch Resource. The answer to the question of when to Batch requests versus making single requests is almost always, as long as you aren't making single batched requests.

Anatomy of a Request

A typical Web Request is made up of the following:

  • {HTTP Method} {URI} {Protocol Version}
  • {Header(s)}
  • {Body}

Simple Web Request

The example above contains all, but body data due to it demonstrating a GET request (no body content). With a Batch request we can expect that the body data will contain individual web requests with a slight twist. The body of a batch request is partially determined by the Content-Type header as well a standard format (multipart/batching). The Content-Type header contains a boundary parameter which defines where a batch message (request or response) begins and ends. In the case of a batch request we could expect the following:

GET http://domain.com/batch HTTP/1.1 Content-Type: multipart/batching;boundary=1234 ...

--1234 Content-Type: application/http; msgtype=request

GET /url1 HTTP/1.1 Host: domain.com Accept: application/json

--1234 Content-Type: application/http; msgtype=request

GET /url2 HTTP/1.1 Host: domain.com Accept: application/json


The batch response would contain similar formatting, but with a replaced of inner Content-Type where msgtype would be equal to response.

Why Batch

Immediately it may seem that batching requests is going to add a bit of overhead and that would not be incorrect due to additional headers and specific formatting. Batch does not shine if the intended application only makes a single request. When sending a Web Request through the browser and request object add additional headers that give context clues to the remote end. When sending a UCWA request it is required to provide an authorization token (via the Authorization header) which would also be saved during a batch.

If we were to compare a non-batched request with a batched request we could start to see what headers could be saved.

Simple Web Request

Batched Request (~993 Characters)

The headers that come to mind include:

  • Authorization (only needed on the initial batch request)
  • X-Requested-With
  • Referrer
  • Accept-Language
  • Accept-Encoding
  • User-Agent
  • DNT
  • Connection
  • Cache-Control

The application wouldn't immediately be able to recoup the savings of these headers for a single-request batch, but over the course of multiple request it is easy to see that there is potential for savings:

2 Batched Requests (~1224 Characters)

3 Batched Requests (~1451 Characters)

For this simple scenario the jump from one to two requests was only an additional 230 characters and two to three was 227 characters. If we were to compare it to executing the single request three times it would result in a final total of 1947 characters with a difference of 496 from the batched route. Different requests may result in different size results, but the general idea should be that grouping these requests should result in some non-zero benefit.

Batch & UCWA

UCWA has some clear suggestions when it comes to batching in that it has a global limit per user and when this value is exceeded the batch can/will be rejected with an response code of 429 (Too Many Requests) and it is up to the developer to split the batch and re-issue. The other interesting limitation is that batches should contain batch requests (of course), photo (I'll touch on this in a follow-up where it is highly possible), and P-GET on the event channel (if this was possible it would prevent the other requests in the batch from returning until the event channel received data or timed out).

Batch Sample and Trace

I've included a sample that demonstrates making a request to find a contact followed up with choosing to execute single or batched requests to presence data (presence, location, note).

I've also included a sample trace that I captured while testing with the above sample.

Batch Sample: Batch.7z Batch Trace: Batch Sample.saz

Next Steps

I'll touch on how to Batch photo requests and see if it is possible to make reasonable modifications to the UCWA SDK Samples to support this deviation.