How to Optimize REST API Response time

17,526

Solution 1

Try using the below configurations for Java-spring projects :

# Enable response compression
server.compression.enabled=true

# The comma-separated list of mime types that should be compressed
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json

# Compress the response only if the response size is at least 1KB
server.compression.min-response-size=1024

# Enable HTTP/2 support, if the current environment supports it
server.http2.enabled=true

Solution 2

What is the current response time you have in ms and what you are aiming to?

A good cached Api should work at the java level around 1-30ms depends on the amount of content and the type of cachong you are using.

Solution 3

Working with partial resources

A simple way to improve the performance of your API calls is by sending and receiving only the portion of the data that you're interested in. This lets your application avoid transferring, parsing, and storing unneeded fields, so it can use resources including network, CPU, and memory more efficiently.

There are two types of partial requests:

Partial response:

A request where you specify which fields to include in the response (use the fields request parameter). Patch: An update request where you send only the fields you want to change (use the PATCH HTTP verb). More details on making partial requests are provided in the following sections. Partial response By default, the server sends back the full representation of a resource after processing requests. For better performance, you can ask the server to send only the fields you really need and get a partial response instead. To request a partial response, use the fields request parameter to specify the fields you want returned. You can use this parameter with any request that returns response data.

Note that the fields parameter only affects the response data; it does not affect the data that you need to send, if any. To reduce the amount of data you send when modifying resources, use a patch request.

Example
The following example shows the use of the fields parameter with a generic 
(fictional) "Demo" API.

Simple request: This HTTP GET request omits the fields parameter and returns 
the full resource.

https://www.googleapis.com/demo/v1?key=YOUR-API-KEY
Full resource response: The full resource data includes the following 
fields, along with many others that have been omitted for brevity.

{
  "kind": "demo",
  ...
  "items": [
  {
    "title": "First title",
    "comment": "First comment.",
    "characteristics": {
      "length": "short",
      "accuracy": "high",
      "followers": ["Jo", "Will"],
    },
    "status": "active",
    ...
  },
  {
    "title": "Second title",
    "comment": "Second comment.",
    "characteristics": {
      "length": "long",
      "accuracy": "medium"
      "followers": [ ],
    },
    "status": "pending",
    ...
  },
  ...
  ]
}

Request for a partial response: The following request for this same resource uses the fields parameter to significantly reduce the amount of data returned.

Partial response: In response to the request above, the server sends back a response that contains only the kind information along with a pared-down items array that includes only HTML title and length characteristic information in each item.

Response status : 200 OK

Share:
17,526
NeeruKSingh
Author by

NeeruKSingh

I spend most of my free time reading question on stackoverflow and learning all the cool stuff thats out there. Checkout my LinkedIn Profile : https://www.linkedin.com/in/neeru-k-singh-18a803101/

Updated on June 11, 2022

Comments

  • NeeruKSingh
    NeeruKSingh almost 2 years

    am working on a E-commerce project which has internally four projects(parts).
    1.) core -has users info
    2.) cms-contents info
    3.) oms-orders info
    4.) wallet-payment info

    Used:-
    UI-AngularJs
    Backend-java

    • No third paty api calls.
    • caching already used
    • only http calls among above four projects.

    we have multiple REST API calls one after other to same domain(with different path and query parameters).

    What are the ways that I can optimize api response time.

    Suggestions for java and AngularJs is welcome,rather than caching give me more suggestions that i can use to optimize response time of api's.

    Appreciated!!!

    I have found same question on stackoverflow How to Optimize REST API calls ,answer is helpul but found no best solution for me.

    • Tim Biegeleisen
      Tim Biegeleisen over 6 years
      I have found same question ... what have you tried so far?
    • NeeruKSingh
      NeeruKSingh over 6 years
      anything,rather than caching can wedo to optimize api's response time ,because caching already used
  • NeeruKSingh
    NeeruKSingh over 6 years
    every api's response time is about 100-150 ms. i want within 100 ms.
  • Nir Sivan
    Nir Sivan over 6 years
    Start by measuring where do those 150ms are 'wasted', remember that Java is super fast and probably accounts to max of 1ms the rest 149ms are probably Database calls try to move then into Java memory cache (if applicable), you can also use Redis cache if you have multiple servers in production.