In today’s age of technology, many things can be done through API (application programming interface) calls to services. These services often offer a RESTful (Representational State Transfer) API set that your application can use. For example, there are APIs out there that can provide language understanding from a sentence you provide and it returns to you a breakdown of the sentence with the intention and keywords of interest. There are APIs where you can upload an image and then you get back all the information about that image like what is the object in focus, is the image showing a person, is the person sad, happy, or angry, and etc. You can make an API call and have a text message and or email be sent.
Anyways, I’m sure you get the idea. Things that used to take years of engineering effort to make are now available through APIs offered by other companies. Through the expansive APIs out there you can quickly create a complicated application. So, it would make sense why consuming APIs would be a common trend in applications you’ll be building nowadays. In this post, I want to talk about consuming APIs in a way where it can improve your application’s responsiveness.
Why Does When You’re Consuming APIs Matter
When in your application an API call occurs can make a difference. How so you might be wondering. Well, that is because calling an API at the wrong place can lead the user to wait. Users these days are impatient and wouldn’t put up with a few seconds wait. They expect an application to be snappy and fast and if your application doesn’t fit that description, they might not be returning.
From the technical side, the API call might be able to happen concurrently with some other operations. So, when an API call occurs at the wrong place it can leave your application idle until the response come back.
When Should You Call an API
You should call an API as soon as possible. That is when all the information that is necessary for the API call is available. This is not to be confused with calling the API then waiting for the response back. What I mean is to create a new thread to make the API call as soon as all the information to make the call is available then continue with operations in the main thread.
Ideally, by the time you need the response from the API, it should already be available. But if it is not that is when you wait for the response to come back to continue operations. Another important thing to note is that you shouldn’t give up getting an API response after one failure. You should try multiple times before giving up and showing an error message.
When Should You Optimize API Consumption
In an ideal world, all the API consumptions would be optimized to occur as early as possible but in reality, that’s unlikely to be the case. Maybe some less experienced developers were building the application before and they designed things in a way where after an API call, the application waits for the response back. Maybe you’re working with a legacy codebase handed down from many years ago and it wasn’t designed to consume APIs. There are plenty of possibilities, but at the end of the day, it comes to a matter of would optimizing API consumptions make a difference for the application.
There are some key signs to look for that would indicate that optimizing API consumption is a worthwhile endeavor. They are the following:
- There are frequent timeout for API calls from your analytics data
- Loading indicator is frequently used throughout the application for operations that involves an API call behind the scene
- An API call takes more than 3 seconds to return
- The application doesn’t feel snappy when an API call is happening behind the scene
This is not an exhaustive list, but this should give you a good idea of what to consider. Users are impatient nowadays and if your application doesn’t respond immediately it leaves a bad impression right off the bat.
To recap, users expect your application to be swift and if that is not the case you might have lost them. One of the bottlenecks for your application can be in the way how APIs are consumed. If API consumptions are a bottleneck within your application then you can address the issue by creating a new thread to make the API call as soon as all the information is available.
I hope this post was helpful to you. If you found this post helpful, share it with others so they can benefit too.
To get in touch, follow me on Twitter, leave a comment, or send me an email at steven@brightdevelopers.com.