Performance Improvements in APIs

Significant improvement in the API responses after effectively changing the query structure.

Performance Improvements in APIs

Client Background

A global leader in the management of critical infrastructure and utilities providing the best in class equipment and solutions designed to prevent damage, manage assets, and protect lives.

Problem Statement:

In the client’s application User Interface, the list of all users is to be displayed in a grid with a default sorting option. License, First Name, Last Name, User Name, Role, Time/Date Filter, and Account Status were few fields on the filter. Based on the application of the selected filter elements, the system shall display the results in a gird format.
The legacy application deployed an API service that has to retrieve the data from the Database (i.e., MongoDB and Azure Active Directory). The requirement was to retrieve a list of users who are in both MongoDB and Azure Active Directory. As we have sorting and filters on User Interface, an endpoint with the POST method was implemented to retrieve data from the database. When tested for efficiency in local environment, the implemented endpoint POST method took 31.40 seconds to retrieve just 99 records. Thus making the application tedious and slow.

Challenges

  • Modernizing the Legacy Applications
  • Improving the Application performance through effectively querying the Data bases.
  • Fetch the data from multiple databases.

Solution

  • Bearer Token: Every time the API made a call to the Microsoft endpoint to generate a new token. This token expires every one hour. Hence there was a need to call this endpoint and store the token in local variable to re-use it within 1 hour. This was we could reduce the Microsoft endpoint execution times in an hour for token generation.
  • Before changes: 3 seconds (each time execution)

    After changes: 0 seconds
  • Calling the DB Multiple Times: Multiple calls were made to MongoDB to get data specific records. This led to lower performance of APIs. Hence, instead of making multiple calls to the database, we could modify the query which would make just one single call to get all records from MongoDB / Azure AD and store it in a local variable. Used local variable wherever required with LINQ query on specific user id. A script “UsersValidFromAD“ was designed to frequently retrieve data from Azure AD. This was configured in AppSetting file.
  • Before changes: 2 seconds (each time execution)

    After changes: 0 seconds
  • To List () in MongoDB query: Once a query is properly built with all filters and sorting then we used ToList () on the query to retrieve a list of records.
  • Before changes: 7 seconds

    After changes: 3 seconds
  • Find in LINQ query: Indexing will not work for the Find method on MongoDB collection, so we preferred to use the Where Method.
  • Before changes: 4 seconds

    After changes: 1 seconds

Key Benefits

Bearer Token:
  • Before changes: 3 seconds (each time execution)
  • After changes: 0 seconds
Calling the DB Multiple Times: A single call was made to both MongoDB and Azure AD to retrieve and store the data in a local variable.
  • Before changes: 2 seconds (each time execution)
  • After changes: 0 seconds
  • To List () in MongoDB query:
    • Before changes: 7 seconds
    • After changes: 3 seconds
    Find in LINQ query:
    • Before changes: 4 seconds
    • After changes: 1 seconds
Load Test:

POST {{base_url}}/v1/API_ServiceName/users

Performance testing for above endpoint using JMeter and below are the Performance test results.
Performance Improvements in APIs
Below are the steps followed to reduce the time taken to read the survey data:
  • 1. Logs: Implemented logs to find the code where it takes more time in execution.
  • 2. In detailed Logs: Based on logs, we go into deep logs which methods take more time in execution.
  • 3.Identifying a line of code that takes more time in execution: Identifying which line of code takes more time in execution ,to enhance the performance of those lines.
  • 4.Alternative solution: Looking for alternative ways to improve performance on Identified lines.
  • 5.Verify logs after changes: Need to check logs after code change whether execution time is reduced or not.
API Execution Times:
Before Code changes
After code changes
Corporate Office (USA)
500 College Road East, Suite 205, Princeton NJ 08540
INDIA
Sandhya Techno -I, 5th Floor Raidurg, Gachibowli
Hyderabad -500032

Singapore

DWP Global Pte. Ltd.
160 Robinson Road #14-04
Singapore (068914)

Call Us

+91 91001 70770

  • +1 (732) 374-9123

Email Us

ISO 27001
© 2023 DWP Global Corp.. All rights reserved.