How to Make the Most of Your Mobile APIs

By Posted in Tech Topics 16 June 2014

mirega

This is a guest post from Pierre-Luc Simard, CTO of Mirego. Mirego designs and develops innovative mobile strategies for iPhone, iPad, Android and other mobile devices.

When creating a native mobile application, one of the most critical indicators of its success is the user experience. The less time a user spends waiting for your application to respond, the better that experience will be. So in this post, I’ll walk you through a few tips related to caching that can help you ensure the best possible performance of your mobile app. Before we get started, consider these stats:

  • The average mobile session is anywhere between 1 and 4 minutes. (On tablets, it’s typically 2.5 to 3 minutes; on mobile phones, it’s about 1.5 to 2 minutes.)
  • The average response time for a mobile app is 1.24 seconds. (80% of that time is spent on the network; 20% of that time is spent communicating with the server.)

So if we were to plug in those numbers into this basic formula, session duration ÷ request duration = requests per session, we’d find out that the average app makes 120 requests per session. I did a bit of informal testing to check this calculation and I found that:

  • To check my bank account balance, my bank’s mobile app sends 43 requests in 70 seconds
  • To check-in on Foursquare, the mobile app makes 46 requests in 30 seconds
  • To open a message in Basecamp, the mobile app makes 24 requests in 40 seconds

As you can see, even just one simple action often requires a number of different requests to be made to the server. But the more requests your app has to make in order to complete that action, the longer your response time, and in turn, the worse the experience for the user. So what you want to do is make the fewest requests possible to complete an action. And that’s where caching comes in.

Why you should take advantage of caching

Many API developers tend to overlook caching. After all, they’re used to using web frameworks in which the browser will cache everything for them by default. But when you’re developing for mobile, the default setting is not to cache anything. That means your app has to continually make the same calls every time the user performs an action. To avoid these unnecessary calls that slow down your app’s performance, I offer these three tips:

  1. Be specific with HTTP headers. By default, a mobile phone will process a request, show the data on its screen, and throw all that information away as the request gets processed. So what you want to make sure you do is have the server tell the mobile app to keep this data, and you do that by being declarative in your HTTP headers, as shown in the example below.

    api

    As you can see, the server controls the cache using the bolded headers, while the client validates those commands.

  1. Set specific cache control editors. Forgetting to set cache control editors is something I’ve seen trip up many junior developers. They’ll include a profile pic to load, for example, but they won’t set the cache control editor. So every time the app is opened, it’ll have to send an API call out to pull up the image. You really want that data to be stored. That’s why you should remember to set specific cache control editors for your objects when uploading them in the metadata.
  2. Enable caching on the device. Once you set your HTTP headers and have the server telling the device to keep your data for a specified amount of time, the device will then need to be told where to keep it. If you choose to go with an iOS or Android devices’ default settings, only a very small amount of data will be stored on the device (a big issue when dealing with large files like images). So make sure to configure your caches and allocate enough memory and disk capacity to store all the content you know will be reused frequently (e.g. a user profile image or your app’s logo). That way, your app will be able to process more requests in less time, since the data will already be readily available. Just make sure to never hard code your cache logic, as this can sometimes cause synching issues.

After caching, focus on measuring

Once you’ve started caching, your next area of focus should be on measuring performance. Based on load or the fluctuation of information going in and out of your app, you’re going to need to adapt and tune your cache editors to perform optimally. So as I like to remind myself and Mirego’s team of developers, it’s important to remember to “measure early, measure often.” I go into more detail about measuring mobile app performance in this blog post, so take a read and feel free to get in touch if you have any questions.

For more details on getting the most from your mobile APIs, check out this slide deck:

About the author

plsimard@mirego.com'As Mirego’s CTO Pierre-Luc spends most of his time working on projects revolving around Mobile. He’s all about software running on small devices. He spent a big portion of the past 16 years working on embedded and mobile platforms in different environments. When he’s not working, Pierre-Luc is probably out on a hike somewhere or out on a run.

Tell us your thoughts Or Send us an internal high five

Talk to @newrelic