Recently we started working with a healthcare company that was facing slow API response time causing bad user experience. The customer is a US based Healthcare company 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 which were working great but very costly, so another challenge was to find an open source tool which can serve the purpose. Hence, we had a two pronged challenge- Improve performance and reduce cost.
We started doing some research and found tools like Solr which seemed like a good fit for us initially, but while we were almost close to setting it up, we thought to get some detailed insights from API response times which opened up new revelations for us making Solr insignificant. Some of the stats from different APIs were like this –
API | TOTAL TIME | DATABASE CALL | % TIME CAN BE OPTIMISED USING SOLR |
---|---|---|---|
PCP | 2192 ms | 662 ms | 30% |
Allergy | 3758 ms | 475 ms | 12% |
So after looking at low level timings, it was found that database call was not that a bigger culprit, there were some calculations happening on backend which were causing the major delays along with SSL handshake and a few other things. So we started rethinking about a new approach to solve this problem.
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 single line of code on backend. Below figure depicts the results after using Cloudfront.
API | BEFORE CF | AFTER CF | % IMPROVEMENT |
---|---|---|---|
PCP | 2192 ms | 14 ms | 197% |
Allergy | 3758 ms | 16 ms | 233% |
So far all good, but how to configure cloudfront to achieve such results? Below are the steps which can help anyone to configure cloudfront with existing APIs.
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 –
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.
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.
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.
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.
What’s on your mind? Tell us a little bit about yourself and your question, and we will be in touch with you within 12 hours
Free eBook on Telemedicine Platform Development: All About Telemedicine
Download Free eBook Now!