KatInterview

Quota & Rate-Limit System for a Mobile AI App

System DesignModel answer
MediumModel answerSystem Designrate-limitingandroidmobile

Last updated

Design the client-server quota enforcement system for a mobile AI assistant, covering API contract, Android architecture, and multi-device synchronization.

Context

You are a mobile engineer at a company shipping an AI chat app on Android. The app offers multiple model tiers: a free tier capped at 10 requests per day, and a premium tier capped at 40 requests per 3-hour rolling window for the flagship model. When users approach or hit their cap, the app must respond gracefully — showing remaining message counts, a live countdown to the next reset, and prompts to either wait or upgrade — all without adding any latency to message dispatch.

What You Need to Design

Functional Expectations

  • Users can always see how many messages they have left and when their quota refills.
  • The send button disables (or redirects to a fallback model) the moment the cap is reached.
  • Mid-stream quota exhaustion, back-to-back rapid sends, and usage from another device are all handled cleanly.
  • The server is the authoritative source of truth, but the UI feels instant.

Non-Functional Expectations

  • Quota enforcement must add zero client-side round trips before a message is sent.
  • The system should tolerate device clock drift and brief network outages.
  • Target scale: 50 million daily active users, peak 100,000 requests per second.

What to Address

  1. API contract and sync strategy — how quota data travels between server and client (headers, SSE events, polling, etc.) and how reset timestamps are communicated and interpreted.
  2. Android architecture — the state machine, persistence layer, and reactive data flow that keep multiple UI components (composer, model picker, banners) in sync.
  3. Concurrency and edge cases — race conditions from rapid taps, speculative UI updates that need to be rolled back on a 429, clock skew between device and server, and multi-device desynchronization.

Describe your end-to-end design with concrete API shapes, data structures, and code where it helps clarify the approach.

Hints

Hint 1

Resist the urge to add a dedicated polling loop for quota status. Every response the server already sends is an opportunity to piggyback fresh quota data — use that instead, and reserve a dedicated quota endpoint only for cold-start or foreground resume.

Hint 2

Absolute epoch timestamps from the server are safer than relative offsets (e.g., 'resets in 3600 seconds'), but they only solve half the clock problem. A device with a wrong wall clock will still mis-render a countdown. Anchoring to the monotonic clock using the server-client time delta is what fully eliminates drift.

Hint 3

Keep a single application-scoped repository as the sole writer of quota state, and derive all UI representations from one shared StateFlow. This prevents the composer, the model picker, and the banner from independently managing quota and getting out of step with each other.