3

We have our HTTP layer served by Play Framework in Scala. One of our APIs is something of the form:

POST          /customer/:id

Requests are sent by our UI team which calls these APIs through a React Framework.

The issue is that, sometimes, the requests are issued in batches, successively one after the other for the same customer ID. When this happens, different threads process these requests and so our persistent layer (MySQL) reaches an inconsistent state due to the difference in the timestamp of the handling of these requests.

Is it possible to configure some sort of thread affinity in Play Scala? What I mean by that is, can I configure Play to ensure that requests of a particular customer ID are handled by the same thread throughout the life-cycle of the application?

gravetii
  • 9,273
  • 9
  • 56
  • 75

1 Answers1

1

Batch is

put several API calls into a single HTTP request.

A batch request is a set of command in one HTTP request, like here https://developers.facebook.com/docs/graph-api/making-multiple-requests/

You describe it as

The issue is that, sometimes, the requests are issued in batches, successively one after the other for the same customer ID. When this happens, different threads process these requests and so our persistent layer (MySQL) reaches an inconsistent state due to the difference in the timestamp of the handling of these requests.

This is a set of concurrent requests. Play framework usually works as a stateless server. I assume you also organize it as stateless. There is nothing that binds one request to another, you can't control order. Well, you can, if you create a special protocol, like "opening batch request", request #1, #2, ... "closing batch request". You need to check if exactly all request was correct. You also need to run some stateful threads and some queues ... Thought akka can help with this but I am pretty sure you wan't do it.

This issue is not a "play-framework" depended. You will reproduce it in any server. For example, the general case: Is it possible to receive out-of-order responses with HTTP?

You can go in either way:

1. "Batch" the command in one request

You need to change the client so it jams "batch" requests into one. You also need to change server so it processes all the commands from the batch one after another.

Example of the requests: https://developers.facebook.com/docs/graph-api/making-multiple-requests/

2. "Pipeline" requests

You need to change the client so it sends the next request after receive the response from the previous.

Example: Is it possible to receive out-of-order responses with HTTP?

The solution to this is to pipeline Ajax requests, transmitting them serially. ... . The next request sent only after the previous one has returned successfully."

Andriy Kuba
  • 8,093
  • 2
  • 29
  • 46