Skip to content
tezvyn:

Implement an API caching layer with offline support and storage trade-offs

Source: vibe-studio.aiHardHow cards are made

Implement an API caching layer with offline support and storage trade-offs
Summary

Designing a tiered cache for speed and resilience.

Key points

Use in-memory for hot data and disk for offline, pick Hive or SQLite by shape, and use TTL with a sync queue.

Watch out for

Ignoring invalidation or treating all data identically.

What's really being asked

This question evaluates whether you can design a production-ready caching architecture beyond simple key-value storage. Interviewers want to see tiered thinking, knowledge of Flutter-specific persistence options, and awareness of the hard problems in distributed systems: invalidation, consistency, and offline conflict resolution. They are looking for product sense too, meaning you choose policies based on data freshness requirements rather than applying one pattern everywhere.

The full answer

First, a two-tier hierarchy: an in-memory LRU cache for sub-millisecond access to hot data and a persistent on-disk cache for offline resilience and larger payloads. Second, storage selection aligned to data shape: use Hive or ObjectBox for fast key-value JSON blobs, SQLite for relational data requiring queries and joins, and SharedPreferences only for small flags or timestamps. Third, fetch policies per endpoint: cache-first for offline availability, network-first for critical fresh data, and stale-while-revalidate for feeds where immediate pixels matter more than absolute freshness. Fourth, invalidation mechanics: TTL for simplicity, ETag or If-None-Match to save bandwidth via 304 responses, and versioning for bulk invalidation. Fifth, offline write strategy: an optimistic local update backed by a sync queue that replays mutations when connectivity returns, using idempotency keys to make retries safe.

The mistakes people make

Treating in-memory and disk as interchangeable rather than complementary layers. Picking Hive for everything without acknowledging SQLite is better for relational queries, or vice versa. Proposing a cache without any invalidation strategy. Ignoring the difference between read caching and offline write queuing. Suggesting that cache size is unlimited or that eviction is not necessary on mobile devices with constrained storage.

What usually comes next

How would you handle cache eviction under memory pressure on Android and iOS? What is your conflict resolution strategy when two offline clients mutate the same record? How do you test offline behavior without relying solely on airplane mode? How would you migrate the cache schema when your API response shape changes? What metrics would you log to prove the cache is actually improving perceived performance?

A concrete example

A news feed endpoint should use stale-while-revalidate. On app launch, the UI renders immediately from a Hive box storing the last JSON payload. A background Dio interceptor fires a network request sending an ETag from the previous response. If the server returns 304 Not Modified, no JSON is parsed and bandwidth is saved. If new data arrives, the Hive box and an in-memory LRU cache are atomically updated and the UI refreshes. If the user likes an article while offline, the app writes the optimistic like to Hive and enqueues the mutation in a sync queue with a UUID idempotency key; when the device reconnects, the queue replays the request, and if it fails permanently the UI rolls back and surfaces a snackbar.

Interview question

Which approach best handles a news feed requiring fast launch, offline survival, and minimal bandwidth usage?

  • a.Stale-while-revalidate with Hive persistence and ETag conditional requestsCorrect
  • b.Hive-only storage with versioning for bulk invalidation and no eviction limits
  • c.Network-first with SQLite caching and TTL-based invalidation only
  • d.Cache-first with in-memory LRU and no persistent store for JSON payloads
Why?

Stale-while-revalidate renders the feed instantly from Hive while refreshing in the background, and ETag avoids redundant downloads via 304 responses. Option D is wrong because an in-memory LRU cache alone is lost on app termination, providing no offline resilience despite the cache-first policy.

Just read this? Test yourself on what you have been reading.

Read the original → vibe-studio.ai

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.

See open roles