Darshan Mehta
Darshan Mehta

Reputation: 30839

REST API - Batch processing vs mutiple calls

I am writing the API to accept a POST request and provide the output. The use case is, I should be able to support single as well as inputs. So, I came up with a structure like this:

Input:

{
    "inputs": [{
            "id": 1,
            "foo": "bar"
        },
        {
            "id": 2,
            "foo": "baz"
        }
    ]
}

Output:

{
    "outputs": [{
            "id": 1,
            "result": "bax"
        },
        {
            "id": 2,
            "result": "bar"
        }
    ]
}

This format supports both single as well as multiple calls. However, this means the API will have to handle CPU and Threads, e.g. an API call might contain 100 inputs and processing those might take a while. So, I need the inputs on the following points:

If this question suits more to Stack Exchange then I am happy for it to be moved across.

Upvotes: 2

Views: 3751

Answers (1)

Adam
Adam

Reputation: 5455

Set up your web server to accept HTTP2 and write client calls with an HTTP2-aware library that can use HTTP2 multiplexing.

Top of google is https://hyper.readthedocs.io/en/latest/

This allows you to revert to pure REST without batching, where the network impact of multiple calls is reduced by the HTTP2 connection multiplexing - i.e. just one connection between client and server is created.

It depends on the trade-off between maintainability and performance. HTTP2 will spare you the headache of managing multiple threads. Presumably though multiple calls will still have a network impact compared to batching, especially if the batches are large.

Upvotes: 1

Related Questions