seriousgeek
seriousgeek

Reputation: 1034

Handling multiple requests efficiently in a REST api

I've built a REST api using Spring Boot that basically accepts two images via POST and performs image comparison on them. The api is invoked synchronously. I'm not using an external application server to host the service, rather I package it as a jar and run it.

@RequestMapping(method = RequestMethod.POST, value = "/arraytest")

    public String compareTest(@RequestParam("query") MultipartFile queryFile,@RequestParam("test") MultipartFile testFile,RedirectAttributes redirectAttributes,Model model) throws IOException{
        CoreDriver driver=new CoreDriver();
        boolean imageResult=driver.initProcess(queryFile,testFile);
        model.addAttribute("result",imageResult);
        return "resultpage";

    }

The service could be invoked in parallel across multiple machines and I would need my service to perform efficiently. I'm trying to understand how are parallel calls to a REST service handled? When the request is sent to the service , does a single object of the service get created and same object get used in multiple threads to handle multiple requests?

A follow-up question would be whether if it is possible to improve the performance of a service on the perspective of handling requests rather than improving the performance of the service functionality.

Upvotes: 0

Views: 14120

Answers (2)

tgkprog
tgkprog

Reputation: 4608

The controller will be singleton but there are ways to make the processing async. Like a thread pool or JMS. Also you can have multiple nodes. This way as long as you return a key and have a service for clients to poll to get the result later, you can scale out back end processing.

Besides you can cluster your app so there are more nodes to process. Also if possible cache results; if you get the same input and they have the same output for 30% or more of the requests.

Upvotes: 0

Taylor
Taylor

Reputation: 4086

Spring controllers (and most spring beans) are Singletons, i.e. there is a single instance in your application and it handles all requests.

Assuming this is not web sockets (and if you don't know what that means, it's probably not), servlet containers typically maintain a thread pool, and will take a currently unused thread from the pool and use it to handle the request.

You can tune this by, for example, changing some aspects of the thread pool (initial threads, max threads, etc...). This is the servlet container stuff (i.e. configuring tomcat/jetty/whatever you're using) not spring per se.

You can also tune other http aspects such as compression. This can usually be done via the container, but if I recall correctly spring offers a servlet filter that will do this.

The image library and image operations you perform will also matter. Many libraries convert the image into raw in memory in order to perform operations. This means a 3 meg jpg can take upwards of 100 megs of heap space. Implication of this is that you may need some kind of semaphore to limit concurrent image processing.

Best approach here is to experiment with different libraries and see what works best for your usecase. Hope this helps.

Upvotes: 1

Related Questions