Multithread REST API Requests

December 3, 2014 | by

If you would like to improve performance when calling the Marketo API, you can make concurrent requests. This approach will allow you to get more data in a shorter period of time.

When making an API request, part of the round-trip time between the client and server is the transfer time on the wire. So if we can reduce the transfer time on the wire for the requests in aggregate, we will improve performance.

The sample code below shows you how to do this in Ruby. It uses EventMachine, which is an event-processing library used for making multithreaded requests.

The example below calls the Lead Activities API, and makes two concurrent requests. This approaches eliminates the transfer time from the client to server for the second request. It does this by including the second request at the same time as the first request. The API responses are written to a text file.