Skip to main content

Improving Rest API Performance Using Cloudfront


With one of the clients from the healthcare industry, we were facing slow API response time causing bad user experience. Client technology integrates solving the lack of healthcare interoperability with its patient-centered mobile platform, from where patients can engage, communicate and participate in their healthcare across various providers that might be using different PM/EHR/EMR products.




The product includes a SaaS platform for healthcare providers to share and communicate patient health information along with its cutting edge point-of-service patient check-in app for waiting rooms.

Due to the complex nature of the product, one of the requirements of the product team was to solve this issue without changing a lot of existing code. The client had already evaluated a couple of paid tools that were working great but very costly, so another challenge was to find an open-source tool that can serve the purpose. Hence, we had a two-pronged challenge- Improve performance and reduce cost. 

A few months back I had seen Slack case study on AWS and how they improved their API performance using Cloudfront. This triggered me to go back and check AWS documentation for the same. I started playing around with Cloudfront for custom origins. Till now, we had used CloudFront alongside S3 for static website hosting which had worked well. After configuring CloudFront for one API we were able to achieve almost 233% improvements in API response time without changing a single line of code on the backend.

Create Cloudfront Distribution:

  1. In the CloudFront console, choose Create Distribution.
  2. On the first page of the Create Distribution Wizard, in the Web section, choose Get Started.
  3. For Origin Domain Name, enter your API URL, which is nothing but your current API url(or base url e.g api.xxxx.com/search)
  4. In Origin Path, keep as it is.
  5. For Origin Protocol Policy, choose HTTPS Only.
    Note: API Gateway does not support unencrypted (HTTP) endpoints. For more information, see Amazon API Gateway FAQs.
  6. To forward custom headers to your origin, enter one or more custom headers for Origin Custom Headers.
    Note: There are several custom headers that CloudFront can’t forward to your origin.
  7. Choose to Create Distribution.
  8. Wait for your distribution to deploy. This might take about 30 minutes. When its Status appears as Deployed in the console, the distribution is ready.

Configure Caching Using Behaviours:

Assuming cloudfront is deployed, it’s time to configure behaviours for caching. I am using above example to configure two APIs /search/pcp and /search/allergy to cache the content based on our needs. Here are the steps for same –
  1. Click on CF distribution
  2. Go to Behaviour Tab and click on Create Behaviour
  3. Enter path pattern – in my case of pcp api it was /search/pcp
  4. I will skip other settings which are self explanatory, lets move to the main settings, select customise from Object Caching section, enter minimum TTL as 0, maximum and default value 31536000(this is max)
  5. Under Query string forwarding and caching select the option forward all, cache based on whitelist,in text box enter all your query parameters which you want to be cached(meaning when these changes backend api response changes), in my case it was Keyword,Limit,Page,PracticeId and Zipcode, enter one in each line
  6. Save the behaviour
Once distribution is ready, you can copy the cloudfront URL and hit the API. Note down the response time. Now hit the same API again and compare the response time. You will see a big difference in the response time which means caching is working. Follow the above steps for adding multiple behaviours.

Challenges:

  • Handling Selecting Caching

While implementing one of the challenges was that we needed to cache the response only when certain request parameter value is available. In our case our backend was sometimes hitting our own database or hitting third party depending on request parameter value. Whenever we are hitting third party API we don’t want to cache the result. We have handled this using max-age=1 parameter in API response, so whenever cloudfront finds max-age=1 it caches for 1 second and overrides default settings. Now backend has control on what to cache and when.

  • Automating Cache Updates

One of the requirements was to update the cache whenever backend database is updated, so we wrote AWS Lambda function to invalidate specific cache entries using cloudfront invalidate API. We exposed this lambda function using AWS API gateway so that our backend can call the APi and invalidate the specific cache entries based on which db values are updated. I will cover this in our upcoming blogs.

Conclusion:

The new architecture turned the response time down to less than half of original resulting in faster responses which in turn made the end user delighted. All this was done without having to change any aspects of the core application or any downtime for the application.
In the end the customer was happy to see savings for time as well as cost

Comments

Popular posts from this blog

Installing BEA Weblogic 10.3 on Mac/OSx

Updated post:- I have to go through a long hassle of making Weblogic work on my MacBook. Few things which I learned and would like to share: -Before starting to install you have to trick the installer into thinking that the local JDK is the generic Sun JDK. If you skip this step the installer will not accept the default Mac OS X JDK and complain that it is Invalid. $ cd /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home $ sudo mkdir -p jre/lib $ cd jre/lib $ sudo touch rt.jar $ sudo touch core.jar -To Install, run the following command from terminal : (Updated for the "fatal error occurred while installing 10.3.2") $ java -Xms256m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=256m -jar -Dos.name=unix -jar server103_generic.jar -Once installation is completed, Create a new domain through "./config.sh" from "/bea/wlserver_103/common/bin" -Once the domain is created: --Need to update the env setting in " .setDomainEnv.sh " file i.e. locat...

DIABETES - ITS EARLY SYMPTOMS

Diabetes, especially Type-2 diabetes is often termed as the 'silent killer disease'. Why? The reason behind it is that this disease does not come with prominent symptoms. Sometimes this disease is not detected until the condition gets severe. However, there are certain symptoms of Type-1 and Type-2 Diabetes, which one should know, in order to identify this ailment at an earlier stage. Let us gain an elaborative insight about this chronic disease and the warning signs that it comes with. 1. Intensive Thirst One of the earliest symptoms of diabetes is peeing more than regular. A normal individual usually pees for 4-7 times in a day but a diabetic might do much more than that. This in turn also leads to intensive thirst as the person starts feeling much thirstier than usual. Why does this happen? This happen because the glucose, which your body reabsorbs in normal conditions, is not able to do so when your blood sugar level goes up. 2. Dry Mouth Lack of necessary amount of fluids...

DEPRESSION – HOW TO FIGHT IT

‘Depression’ is a condition which is quite prevalent these days courtesy a hectic lifestyle. One might think that this condition is an imaginary one where it is just all in the head, however, on the contrary, depression is a ‘real’ disease, which has an extremely adverse effect on the brain. There are several reasons, apart from changes in the brain chemistry, that lead to depression which include difficult life situations, stress or any other medical condition. There are many symptoms that are associated with this disease that are commonly found in people who are suffering from depression. These symptoms include: a) Angry Outbursts b) Mood Swings  c) Withdrawal from people d) Losing interest in any activity e) Immense Sadness f) Hallucination  g) Lack Of Sleep h) Concentration Problems  Depression is a common problem. Several studies suggest that one in every 5 women and one in every 10 men suffer from the problem of depression. How to fight this disease? Is ther...