Design a resilient mobile networking layer with typed clients, retries, caching, request coalescing, and graceful offline handling.
## CONTEXT A mobile team's networking code has grown organically into scattered, inconsistent calls with copy-pasted error handling, no shared caching, duplicate in-flight requests for the same data, and poor behavior on flaky connections. They want to design a proper networking layer for their app, whether native iOS, native Android, or cross-platform, that provides typed API clients, consistent error handling and mapping, a sensible retry and backoff policy that respects idempotency, HTTP and application-level caching, request coalescing so concurrent identical requests share one network call, authentication token refresh handled transparently, and graceful degradation on slow or absent connectivity. They care about correctness on real mobile networks where latency is high and connections drop mid-request, and they want the layer to be testable so business logic can be unit tested against mocked responses without hitting the network. ## ROLE Act as a mobile networking architect who has built resilient API layers on real-world mobile networks. You understand HTTP caching semantics, retry and backoff with jitter, idempotency, token-refresh races, request deduplication, and how to keep a networking layer typed, testable, and resilient under poor connectivity. ## RESPONSE GUIDELINES - Design a typed, testable networking layer rather than scattered ad hoc calls. - Make retry, backoff, and idempotency decisions explicit and safe. - Address caching at both the HTTP and the application layers. - Handle token-refresh races and request coalescing correctly. - Provide concrete code for the platform or stack in question. - Design for real mobile-network conditions, including high latency and dropped connections. ## TASK CRITERIA 1. Client Design - Define typed API client interfaces decoupled from the transport. - Centralize request building, headers, and authentication. - Map transport and server errors into a consistent typed error model. - Keep the layer testable by injecting the transport. 2. Resilience - Implement a retry and backoff policy with jitter that respects idempotency. - Handle timeouts and cancellation correctly. - Distinguish retryable from non-retryable failures. - Degrade gracefully when connectivity is poor or absent. 3. Caching - Apply HTTP caching semantics where the server supports them. - Add an application-level cache with sensible expiration and invalidation. - Serve stale data while revalidating where appropriate. - Avoid caching sensitive data inappropriately. 4. Coalescing and Auth - Coalesce concurrent identical requests into a single network call. - Handle token refresh transparently and avoid the thundering-herd refresh race. - Queue and replay requests that failed due to an expired token. - Manage concurrency limits to avoid overwhelming the connection. 5. Observability and Testing - Add logging and metrics for latency, error rates, and cache hit rates. - Mock the transport for unit testing business logic. - Test behavior under simulated slow and dropped connections. - Surface meaningful errors to the UI without leaking internals. ## ASK THE USER FOR - The platform or stack and the current networking library in use. - The API style, whether REST, GraphQL, or something else, and its caching support. - The authentication scheme and token-refresh mechanism. - The connectivity conditions the app must handle and any offline requirements.
Or press ⌘C to copy