Networking and API Contracts for Mobile Clients

Topics Covered

Designing the API Around the Screen

Count round trips, not endpoints

The backend for frontend pattern

Payload size and field selection

Batching writes and analytics

What to say in the interview

Caching what the API returns

Step three of the framework from the first lesson is the data and API contract. In a backend interview that step is about what the service exposes to the world. In a mobile interview the question is narrower and more interesting: what does this particular client need to draw each screen, and what is the cheapest way to get it across a network that is slow, expensive and unreliable? The client you are designing is the main consumer of the API, so you are allowed, and expected, to shape the contract around it.

The interviewer is checking whether you understand what makes a mobile network different from the data center network your backend colleagues think about. Round trips are expensive. A request from a phone on a busy cellular cell can easily take several hundred milliseconds before the first byte comes back, and the first request after the radio has been idle pays extra to wake the radio up. Bandwidth varies by two orders of magnitude between a good Wi-Fi network and a weak signal in a train. Users pay for data on many plans. Every byte you send also costs battery, because the radio stays in a high power state while it transfers and for a few seconds afterwards. A good mobile API design minimises round trips, sends only the bytes the screen needs, and works well when the network is bad.

Count round trips, not endpoints

Imagine the home screen of a social app. It shows the user's avatar and unread count, a list of posts, a few suggested accounts and a promoted item. A backend team organised around services might expose this as four endpoints: one for the profile, one for the feed, one for suggestions and one for ads. A web client on a fast connection can call all four in parallel without much pain. A phone on a weak connection pays for each one. Even with parallel requests over one HTTP/2 connection, every call is a separate chance to fail, each has its own headers and its own error handling in the client, and the screen cannot render until the slowest one arrives or the client writes careful partial rendering logic.

The usual fix is a screen shaped endpoint: one call, such as GET /home, that returns everything the home screen needs for its first paint. The fan out to the four services still happens, but it happens inside the data center, where a round trip costs a millisecond instead of two hundred. The phone pays one round trip and receives one response it can render in one pass.

One screen shaped call moves the fan out from the slow radio link to the fast data center network.

This does not mean every screen gets one endpoint and nothing else. Screens have parts that change at different speeds. The feed refreshes when the user pulls down, the unread badge updates when a push arrives, and suggestions can be stale for an hour. A sensible design returns the first paint in one call and lets the parts that update independently have their own small endpoints. Say this explicitly in the interview: one call for first render, targeted calls for updates.

The backend for frontend pattern

The component that builds screen shaped responses is often called a backend for frontend, or BFF. It is a thin service owned by the mobile team, or at least shaped by it, that sits between the app and the domain services. It aggregates calls, trims fields, converts formats, and absorbs differences between app versions so the domain services do not have to. Some companies run one BFF per client platform; others run one BFF for all mobile clients and a separate one for the web.

A BFF has costs, and naming them shows judgment. It is another service to deploy, monitor and keep available, and if it is down every screen is down. It can grow into a second copy of the business logic if the team is not disciplined about keeping it to aggregation and shaping. It also adds a hop of latency in the data center, which is small but real. In an interview, the right move is usually to propose a BFF or an aggregated endpoint when the screen needs data from several services, and to state that its job is composition, not business rules.

http
1GET /v1/screens/home?feed_limit=20 HTTP/2
2Host: api.example.com
3Authorization: Bearer eyJhbGciOi...
4Accept-Encoding: br, gzip
5X-App-Version: 6.0.1 (ios)

Payload size and field selection

The second lever is the size of each response. Domain services tend to return full objects: a post with its author, the author's full profile, every media variant, edit history and moderation flags. A feed row needs the author's name and avatar URL, the text, one image URL at the right size, and counts. The difference can easily be five to ten times in bytes. On a fast network that difference is invisible; on a slow one it is the difference between a feed that appears and a spinner.

There are three common ways to send less. The BFF can return a view model shaped for the screen, with exactly the fields it draws. The API can accept a field selection parameter, such as fields=id,text,author.name, which keeps one endpoint flexible across screens. Or the client can use a query language such as GraphQL, where the client states its fields in every request. All three work. View models are simplest and easiest to cache; field selection is a good middle ground; GraphQL moves the most control to the client, with costs discussed in the next section.

Field selection and compression shrink what the radio has to carry.

Images deserve a special mention because they are usually the largest bytes on a screen. The API should not return one original image URL and leave the client to download a twelve megapixel photo for a sixty point thumbnail. It should return URLs for a few sizes, or a URL template the client fills with its target width and pixel density, so the client downloads what it will display. The details of the image pipeline belong to the images and media lesson; the contract point here is that the API must make the right size requestable.

Batching writes and analytics

The same thinking applies to traffic going up. A naive app sends an analytics event per tap, a like per tap and a read receipt per message, each as its own request. That keeps the radio awake and multiplies failures. Group what can be grouped: batch analytics events and flush them every so often or when the app goes to the background, coalesce rapid toggles of the same like button into the final state, and send read receipts as a high water mark per conversation rather than one per message. Interviewers like to hear that you have thought about the upload direction, because most candidates only design reads.

What to say in the interview

When you reach the API step, list the screens from your requirements and, for each, write the one call that renders it, the main fields and the pagination style. Then call out the two or three writes the app makes and how they are made safe to retry. A compact table on the whiteboard is enough. Mention the BFF if several services are involved, mention field trimming and image sizes, and move on. The point is not to design every endpoint; it is to show that the contract was designed from the client's side of a slow link.

A useful habit is to say the cost of the first screen in round trips. For example: on a cold start the app makes one call for the session, which can be skipped if the token is still valid, and one call for the home screen, so the user sees content after one round trip plus render time; images then stream in. That sentence ties the API to the start time the user feels, which is exactly the link the interviewer wants to see.

Caching what the API returns

The last contract question for this section is how long a response stays valid. A screen shaped response can carry its own freshness rules: a Cache-Control max-age for parts that are safe to reuse for a minute, an ETag so a revisit can ask whether anything changed and receive a tiny 304 Not Modified instead of the full body, and a server timestamp the client stores with the data. Both URLSession and OkHttp ship an HTTP cache that honours these headers when it is enabled and sized. For personalised responses the HTTP cache is a convenience, not the main store; the main store is the local database described in the offline lesson. Still, an ETag on the home response is a cheap way to make a pull to refresh that changes nothing cost a few hundred bytes instead of fifty kilobytes.

Latency lives on the radio

A round trip inside the data center costs about a millisecond; a round trip from a phone on cellular often costs a few hundred. Moving fan out from the phone to a BFF trades many slow hops for one, which usually matters more than any server side optimisation.