Web Service APIs
To provide the best possible user experience, mobile apps need to be fast and highly responsive. Most cases of slow loading and poor experience can be traced back to server-side issues. Optimizing web service APIs for mobile use cases helps to address and resolve this.
Not all users update their mobile apps as new versions come out and some choose to keep outdated versions for extended periods of time. This might seem innocent, but it can be problematic. Older mobile app versions may rely on obsolete API contracts. Because of this, backward compatibility of APIs used by a mobile app is of critical importance.
There are several ways to approach backward compatibility issues, but two techniques that can help are:
- Use API Expand-Contact pattern (aka Parallel Change) to avoid non-backward compatible API changes.
For example, renaming a required field from an API response payload is a backward-incompatible API contract change. So, instead of renaming the field, you would add a new field and keep the original field until it is safe to delete it.
- Use the API versioning and keep older API versions until you make sure nobody is using them anymore.
Additionally, to limit the number of old web service API versions in use you can implement in-app updates and nudge users to install the latest app version (see Android in-app updates).
Mobile-Optimized API Contracts
API contracts used by a 12-factor mobile app should support the following goals:
- Avoid over-fetching; where an API response returns a lot of data that is not used by a mobile app.
- Avoid under-fetching; where a single API response does not return all the data required by a mobile app, so more API requests need to be made. Normally a mobile app would only have to make a single API request for a single user action.
- Prefer less verbose payload format (e.g. JSON over XML).
APIs that do not support the above goals are likely to consume more battery and bandwidth and affect user experience by making a mobile app less responsive.
There are a couple techniques that may help to optimize APIs for mobile, such as:
- The use of GraphQL flexibility to prevent over- and under-fetching.
- Pragmatic REST APIs optimized for your mobile use cases.
- Pagination to limit an API response size.
- Compression of large API responses.
It might be challenging for a single general-purpose backend to expose API contracts optimized for mobile and desktop apps. Backend For Frontend (BFF) architectural pattern might be used in such cases to create a dedicated backend optimized specifically for mobile.