Log(n) Engineering

API Optimizations for Mobile

With the popularity of mobile devices growing more and more everyday, they are becoming the driving force in user interaction with our applications, for this reason, our systems should evolve in order to deliver the best experience to the end user, and this is not restricted only to a fancy design, or an easy app navigation, or how it integrates with other services. This redesign should come from the very core of the system, the backend.

The current mobile platform suffers from the following limitations when compared to a desktop computer:

  • Lower processing power: No matter if you have the latest and shiniest mobile phone in the market, with an octa-core processor, independent GPU, etc. It will never match the processing power of a computer.

  • Limited battery life: Each mobile device is an independent unit, it connect to the power outlet just to recharge, and for this same reason, this is a limitation. As the battery has a limited amount of power available for usage, every action the user does on the device will affect the battery duration, desktops has a continuous power connection, so no matter how much processing power they use, or how hard they have to work, desktops just doesn’t have to worry about power supply. One key feature about the battery consumption, is that despite what most people think, the screen is not what consumes the most battery of the device, is the modem, so having the device doing a constant scan of networks (due a weak or congested signal) and doing constant network requests is what drains more battery of your device.

  • Limited Memory: Today’s high end mobile phones comes with 3 - 4Gb of RAM, and from 16 to 64Gb of storage. This may sound plenty enough, but take in consideration that as the mobile device is an independent unit, it has to do a lot more things in the background than a desktop computer, like update location services, connect and disconnect from mobile networks, synchronize application data etc. All this actions will consume memory, so the Operating System is in a constant job of giving and taking away resources to the running applications, and making sure data is not lost during the process.

For those reasons, the mobile application optimization has to start from the Back End, that way our app will have the smallest footprint in the device performance, hence the user will be able to use our awesome app longer.

External Agents

Latency

As you know, latency is the time it takes from the moment you request the data, and the moment you actually start receiving it.

When you’re requesting a single large file, the time it takes from your device to connect to the server (ping!) compared to the time it will take downloading the file will actually be insignificant. However, if your device is requesting a small amount of data from the server, the time it takes to do a ping, compared to the time it takes to download the data is considerable, and what happens when your device is executing multiple API calls? The sum of all the pings will be of several milliseconds, or seconds.

http://nordicapis.com/wp-content/uploads/Latency-Diagram.png Source

One key point to remember when talking about latency in mobile devices, is that mobile devices don’t rely in a single network, they can jump from Wifi, to 4G, 3G, 2G, etc., and each network will have different broadband, which affects the connection time.

http://nordicapis.com/wp-content/uploads/Latency-Diagram-2-white.png Source

Different connection times depending of the network type, displaying the minimum, maximum time, and the standard deviation.

Broadband

Although the theoretical broadband could be easily compared to a desktop computer, in the real life things are very different, worldwide the network connection speed in mobile devices is measured in Megabytes while for the rest of the world is measured in Teraflops.

Having said this, in almost every mobile device are third party components as advertising SDK’s, analytics SDK’s, location services among other applications that are constantly transmitting information background, this will greatly limits the actual broadband the device has available to send and receive the information the user actually wants at a particular time.

Channel Quality

Unlike desktop computers that has a constant network signal, mobile devices are in movement, and the cell towers signal can be affected the terrain, distance or objects (buildings). And not only the signal strength is something that affects, but also, changing between cell towers or network types, network congestion, and weather affects the network connection of the mobile devices resulting in packets being lost, incomplete data transfers, and duplicate requests. I.e. The handover between a LTE network and a HSPA+ takes about 2.4 seconds.

Data Cap

In the most part of the world, network operators uses a network cap, in order to avoid users to congest the whole network, and affect the broadband of other users. Unfortunately though the network speeds have improved the data cap remains the same, this means that every network request counts, API responses needs to be as efficient and lightweight enough so their impact on the network cap is minimum.

Data Cost

Very similar to the data cap, is the data cost, and usually they go hand by hand. In most part of the world users has to pay for the data transferred, although the cost per kilobyte is barely noticeable, when you sums up all the data you’ve transferred during the day, it can drain your wallet quickly. For the same reason as the data cap EVERY NETWORK REQUEST COUNTS, doing unnecessary network requests cost money to the users.

Ways to Reduce API Impact

Merge Responses

Don’t provide entities the same way they’re structured in the Backend, instead focus only on what the user is intended to see. This means that instead of doing several APIs calls to obtain the information required to display to the user, you can merge them into a single request. By doing this, you will not only save resources and time to the mobile device, but you will also save money to the user, reduce the amount of network connections your servers needs to handle (reduces costs in a cloud based infrastructure), reduce maintenance to your backend (less API’s), may reduce the number of queries on your database (worse case scenario, they remain the same), and will increase the data integrity, as the backend will have more control of how the data is handled to the clients.

Expansion

Merging responses is effective when the teams are working closely, but when that’s not the case, resource expansion is a great mechanism to return customized responses. Instead of return the whole content of GET /people/ you can let developers customize the request to obtain just the fields they need GET /people?fields={firstname, lastname}, that way will not only help the developers obtain the information they need, but will also reduce the response size, improving response time, money and resources.

Data Subset

It’s very common to share the same API’s between your web platform and mobile, but when a browser with a low latency connection is able to handle the complete response without problems, a mobile device with limited resources and processing power, will have issues. This will result in altering the user experience (mostly due performance). For this reason it’s recommended to deliver the least amount of information the mobile platform requires to perform an action, in conjunction with other techniques as pagination, expansion, etc.

Complete Model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"person": {
  "id": 38385,
  "firstName": "John",
  "lastName": "Doe",
  "age": 25,
  "country": "US",
  "phones": {
      "home": "800-123-2485",
      "cell": "800-159-1947",
      "work": "800-325-1851"
  }
  "email" {
      "johndoe@example.com",
      "johndoe@example.org",
  }
}
}

Trimmed Model

1
2
3
4
5
6
{
  "firstName": "John",
  "lastName": "Doe",
  "age": 25,
  "country": "US"
}

Compress Data

In addition of reducing the response payload in JSON, by adding GZIP compression, you will gain great benefit by being able to reduce even more the size of the request/response. It’s very effective in large amounts of data, by reducing the size up to a 90%.

Avoid/Minimize Cookies

Since every time a client sends a request to the domain it will include all of the cookies that it has from that domain – even duplicated entries or extraneous values. This means that keeping cookies small (and not requiring them if they aren’t need) is another way to keep payloads down and performance up. Don’t use or require cookies unless necessary. Serve static content that doesn’t require permissions from a cookieless domain. (Source)

Use Device Profiles per API’s

As mobile devices vary in screen size, specs etc from desktop computers, it a good idea to create profile for each platform, that way you can customize and send the right data to each recipient depending of their needs.

Example:

  1. Mobile - GET /user/photos?profile=mobile. Return only small size images
  2. Table - GET /user/photos?profile=tablet. Return medium size images
  3. Desktop - GET /user/photos?profile=desktop. Return full size images

The profiles for mobile, tablet and desktop, are not limited only to image sizes, you can use the same logic to change the JSON response, return a data subset for mobile, and the whole entities for web, plus, you can use the profiles in conjunction with “limit” and “offset” to return the correct amount of data depending of the screen size.

References