<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shakibaenur</title>
    <description>The latest articles on DEV Community by Shakibaenur (@shakibaenur).</description>
    <link>https://dev.to/shakibaenur</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1020798%2Fba35e3f2-4ae6-4915-a128-6733d51178b0.jpg</url>
      <title>DEV Community: Shakibaenur</title>
      <link>https://dev.to/shakibaenur</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shakibaenur"/>
    <language>en</language>
    <item>
      <title>Design a Stock Trading App: A Mobile System Design Walkthrough</title>
      <dc:creator>Shakibaenur</dc:creator>
      <pubDate>Sun, 01 Nov 2026 12:30:21 +0000</pubDate>
      <link>https://dev.to/shakibaenur/design-a-stock-trading-app-a-mobile-system-design-walkthrough-2736</link>
      <guid>https://dev.to/shakibaenur/design-a-stock-trading-app-a-mobile-system-design-walkthrough-2736</guid>
      <description>&lt;p&gt;A stock trading app has one job that makes it different from most apps: it moves real money based on prices that change several times a second. A slow feed is annoying in a social app. In a trading app, a price that's two seconds old can cost someone money.&lt;/p&gt;

&lt;p&gt;That raises the bar for the mobile client. Prices must arrive fast without draining the battery. Charts must stay smooth while data streams in. And the app must never show a wrong or stale number as if it were live.&lt;/p&gt;

&lt;p&gt;This walkthrough answers "Design a stock trading app" the way you would in a 45-minute interview, in five steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Clarify the requirements&lt;/li&gt;
&lt;li&gt;Design the API and data model&lt;/li&gt;
&lt;li&gt;Sketch the client architecture&lt;/li&gt;
&lt;li&gt;Deep dive into the hard problems&lt;/li&gt;
&lt;li&gt;Wrap up with trade-offs&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 1: Clarify the Requirements
&lt;/h2&gt;

&lt;p&gt;Start by asking what can be traded. Stocks only, or options, futures, and crypto too? Each one changes the order types, the risk checks, and the screens. For this session, assume stocks only, on a few international markets.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functional requirements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;See a portfolio with live prices and total value&lt;/li&gt;
&lt;li&gt;View a stock's details: live price, day high and low, and a price history chart&lt;/li&gt;
&lt;li&gt;Place buy and sell orders, and cancel open orders&lt;/li&gt;
&lt;li&gt;See order status change in real time (open, filled, cancelled)&lt;/li&gt;
&lt;li&gt;Set price alerts and get a push notification when a stock reaches the target&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Non-functional requirements
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Requirement&lt;/th&gt;
&lt;th&gt;Target&lt;/th&gt;
&lt;th&gt;What it means for the client&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Latency&lt;/td&gt;
&lt;td&gt;Live prices on screen within about a second&lt;/td&gt;
&lt;td&gt;A persistent connection for market data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Correctness&lt;/td&gt;
&lt;td&gt;Money is never rounded wrongly&lt;/td&gt;
&lt;td&gt;Exact decimal types, never floating point&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Smoothness&lt;/td&gt;
&lt;td&gt;No dropped frames while prices stream in&lt;/td&gt;
&lt;td&gt;Buffer updates instead of redrawing on every tick&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Security&lt;/td&gt;
&lt;td&gt;Financial data protected in transit and on disk&lt;/td&gt;
&lt;td&gt;TLS everywhere, nothing sensitive stored in plain text&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scale&lt;/td&gt;
&lt;td&gt;About 500,000 daily users, thousands of symbols&lt;/td&gt;
&lt;td&gt;Subscribe only to the symbols on screen&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Out of scope
&lt;/h3&gt;

&lt;p&gt;Set aside sign-up and identity verification, deposits and withdrawals, options and futures, and the exchange's matching engine. Mention them as extensions.&lt;/p&gt;

&lt;h3&gt;
  
  
  The screens
&lt;/h3&gt;

&lt;p&gt;Three screens cover the scope:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Home:&lt;/strong&gt; account balance, a portfolio value chart, holdings, and recent orders&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stock details:&lt;/strong&gt; live price, key stats, a history chart, and Buy and Sell buttons&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Place order:&lt;/strong&gt; a bottom sheet to choose quantity, review the estimated cost, and confirm&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 2: Design the API and Data Model
&lt;/h2&gt;

&lt;p&gt;A trading app has two kinds of traffic. The user starts some of it: placing an order, loading the portfolio, fetching price history. The market starts the rest: prices change, orders fill. The design uses REST for the first kind and a WebSocket for the second.&lt;/p&gt;

&lt;h3&gt;
  
  
  REST endpoints
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET    /v1/portfolio
GET    /v1/stocks/{symbol}
GET    /v1/stocks/{symbol}/history?interval=1m&amp;amp;period=1d
GET    /v1/orders?status=open&amp;amp;page=1&amp;amp;limit=20

POST   /v1/orders
Idempotency-Key: 9b2e...41
{ "symbol": "AAPL", "side": "BUY", "type": "LIMIT", "quantity": "10", "limitPrice": "189.40" }
→ 201 Created { "orderId": "ord_7731", "status": "OPEN" }

DELETE /v1/orders/{orderId}

POST   /v1/alerts
{ "symbol": "AAPL", "targetPrice": "195.00", "direction": "ABOVE" }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that &lt;code&gt;quantity&lt;/code&gt; and &lt;code&gt;limitPrice&lt;/code&gt; are strings, not numbers. JSON numbers are usually parsed as floating point, and that loses precision. Sending money as a string and parsing it into a decimal type keeps every digit exact. More on that in the deep dives.&lt;/p&gt;

&lt;p&gt;Orders use page-based pagination, not a cursor. Orders rarely change after they're created, and people mostly look at the latest few, so the shifting-page problem that feeds have doesn't really apply here.&lt;/p&gt;

&lt;h3&gt;
  
  
  WebSocket messages
&lt;/h3&gt;

&lt;p&gt;The app opens one secure socket and subscribes to the symbols it needs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"subscribe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"channel"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"prices"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"symbols"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"AAPL"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"MSFT"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"GOOG"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server then streams updates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"symbol"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"AAPL"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"189.42"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"ts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1761980400123&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"order_update"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"orderId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ord_7731"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"FILLED"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"filledPrice"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"189.38"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;ts&lt;/code&gt; field comes from the server. The app uses it to show "last updated" times and to detect stale data, never the phone's own clock.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Sketch the Client Architecture
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UI LAYER
  HomeScreen          → HomeViewModel
  StockDetailScreen   → StockDetailViewModel
  PlaceOrderSheet     → PlaceOrderViewModel
                              │ actions ↓   ↑ state
DATA LAYER
  PortfolioRepository   (holdings, balance)
  StocksRepository      (live prices, history)
  OrdersRepository      (place, cancel, track)
        │
        ├─ HttpClient           REST calls
        ├─ MarketSocket         WebSocket: prices, order updates
        ├─ HistoryCache         disk cache with expiry
        └─ PushClient           FCM / APNs for alerts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each screen has a ViewModel that exposes one state object. Each kind of data has one repository, and the screens only talk to repositories.&lt;/p&gt;

&lt;h3&gt;
  
  
  Different data, different caching
&lt;/h3&gt;

&lt;p&gt;This is where a trading app differs from a feed or a chat app, so explain it clearly:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Data&lt;/th&gt;
&lt;th&gt;Where it's kept&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Live prices&lt;/td&gt;
&lt;td&gt;Memory only&lt;/td&gt;
&lt;td&gt;Writing them to disk wastes battery and could show an old price as current after a restart&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Price history&lt;/td&gt;
&lt;td&gt;Disk cache with an expiry&lt;/td&gt;
&lt;td&gt;It doesn't change for past periods, and it saves data on every open&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Portfolio and orders&lt;/td&gt;
&lt;td&gt;Fetched on open, then updated by socket events&lt;/td&gt;
&lt;td&gt;The server's record is the truth, because real money is involved&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Display preferences&lt;/td&gt;
&lt;td&gt;Key-value storage&lt;/td&gt;
&lt;td&gt;Small, flat settings like the chart's default range&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The key rule: a live price on screen must always come from the current session. If the app can't confirm a price is fresh, it says so.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Deep Dive into the Hard Problems
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Money must never be a Double
&lt;/h3&gt;

&lt;p&gt;This is the first thing I'd raise in any trading interview. Binary floating point can't represent most decimal fractions exactly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// 0.30000000000000004&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a price, that error is tiny. Multiply it across quantities, fees, and thousands of orders, and the totals drift. Use an exact decimal type and set the rounding rule explicitly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;price&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BigDecimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"189.42"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;quantity&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BigDecimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"10"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;fee&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BigDecimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"0.15"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;total&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fee&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setScale&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;RoundingMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;HALF_EVEN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// 1894.35&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On iOS, use &lt;code&gt;Decimal&lt;/code&gt;. Two more rules go with this. Parse money from strings, never from JSON numbers. And let the server do the final calculation: the app shows an &lt;em&gt;estimated&lt;/em&gt; cost, and the confirmed amount comes back with the order.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Streaming prices without dropping frames
&lt;/h3&gt;

&lt;p&gt;During market hours a busy symbol can tick several times a second. If every tick goes straight to the UI, the main thread redraws constantly, the chart stutters, and the battery drains.&lt;/p&gt;

&lt;p&gt;The fix is to buffer and coalesce. Ticks arrive in the repository, which keeps only the latest price per symbol. The UI reads a snapshot at a fixed rate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StocksRepository&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MarketSocket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;latest&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ConcurrentHashMap&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Tick&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ticks&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onEach&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;tick&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;latest&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tick&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;  &lt;span class="c1"&gt;// keep only the newest&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launchIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ioScope&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// The UI collects this: at most 4 updates a second, whatever the tick rate&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symbols&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;):&lt;/span&gt; &lt;span class="nc"&gt;Flow&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Tick&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
        &lt;span class="nf"&gt;ticker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;periodMs&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;250&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;latest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filterKeys&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;symbols&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;distinctUntilChanged&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flowOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Default&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Four updates a second looks live to a person, while the app does a fraction of the work. A few more rules complete the picture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Subscribe only to what's on screen.&lt;/strong&gt; When the user leaves a stock's page, unsubscribe from it. Thousands of symbols exist, but a screen shows a handful.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Close the socket in the background.&lt;/strong&gt; Price alerts work through push notifications, so there's no reason to stream prices nobody can see.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never validate an order against the throttled UI price.&lt;/strong&gt; The display rate is for people. The server checks the real price when it executes the order.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Rendering charts
&lt;/h3&gt;

&lt;p&gt;One trading day at one-minute resolution is 391 points, and five years of daily prices is over 1,250. There are four realistic ways to draw them:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Approach&lt;/th&gt;
&lt;th&gt;Strength&lt;/th&gt;
&lt;th&gt;Weakness&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Draw it yourself (Canvas, Core Graphics)&lt;/td&gt;
&lt;td&gt;Fastest, full control&lt;/td&gt;
&lt;td&gt;Built twice, once per platform&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Native chart library&lt;/td&gt;
&lt;td&gt;Quick to adopt, gestures included&lt;/td&gt;
&lt;td&gt;You depend on someone else's maintenance&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Server-rendered images&lt;/td&gt;
&lt;td&gt;Almost no work on the phone&lt;/td&gt;
&lt;td&gt;No interactivity, and every zoom needs the network&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;WebView with a web chart library&lt;/td&gt;
&lt;td&gt;One chart for web, Android, and iOS&lt;/td&gt;
&lt;td&gt;Heavier, and it needs a JavaScript bridge&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If the company also has a web app, a WebView chart keeps all three platforms identical. It needs care to stay fast:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prepare the data on a background thread before passing it to the chart, so JavaScript does no heavy array work.&lt;/li&gt;
&lt;li&gt;Load a coarse version first, and add detail as the user zooms in.&lt;/li&gt;
&lt;li&gt;Send new prices in batches through the bridge, at the same 250 ms rate as above, and append to the chart instead of redrawing it.&lt;/li&gt;
&lt;li&gt;Keep the order flow out of the WebView. Placing an order is a native screen, so it stays fast even if the chart is busy.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Placing an order safely
&lt;/h3&gt;

&lt;p&gt;An order is the one action where a duplicate is truly expensive. Imagine the user taps Buy, the request times out, and the app retries. If the first request actually reached the server, the user now owns twice the shares they wanted.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Idempotency-Key&lt;/code&gt; header prevents this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When the order sheet opens, the app generates a UUID for this order.&lt;/li&gt;
&lt;li&gt;Every attempt to submit the order sends the same key.&lt;/li&gt;
&lt;li&gt;If the server has already seen that key, it returns the original order instead of creating a new one.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The UI is honest about state, too. Unlike a Like button, an order is never shown as done optimistically. After the user confirms, the sheet shows "Submitting...", then the real status from the server. A filled order shows the server's &lt;code&gt;filledPrice&lt;/code&gt;, which can differ from the estimate.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Showing stale prices honestly
&lt;/h3&gt;

&lt;p&gt;The worst bug in a trading app is a frozen price that looks live. Every price on screen carries the server timestamp of its last update. If the socket disconnects, or no update has arrived for a few seconds during market hours, the UI changes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The price turns grey with a "Last updated 10:42:13" label.&lt;/li&gt;
&lt;li&gt;The Buy and Sell buttons warn that prices may be delayed.&lt;/li&gt;
&lt;li&gt;The app reconnects with backoff, then refetches current prices over REST before showing anything as live again.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Outside market hours, the app shows the closing price with a "Market closed" label instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Wrap Up
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Decision&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;th&gt;Trade-off&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;REST for orders, WebSocket for prices&lt;/td&gt;
&lt;td&gt;Clear results for orders; a fast stream for market data&lt;/td&gt;
&lt;td&gt;Two channels to maintain&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Decimal types and string-encoded money&lt;/td&gt;
&lt;td&gt;Exact amounts, no drift&lt;/td&gt;
&lt;td&gt;Slightly slower math and more parsing code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Coalesce ticks to 4 updates a second&lt;/td&gt;
&lt;td&gt;Smooth UI and lower battery use&lt;/td&gt;
&lt;td&gt;The screen can be up to 250 ms behind the latest tick&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Live prices in memory only&lt;/td&gt;
&lt;td&gt;No stale price shown after a restart&lt;/td&gt;
&lt;td&gt;Prices must be refetched on every launch&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;WebView charts&lt;/td&gt;
&lt;td&gt;One chart across web and mobile&lt;/td&gt;
&lt;td&gt;Extra memory and a JavaScript bridge to manage&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No optimistic UI for orders&lt;/td&gt;
&lt;td&gt;The user always sees the real order state&lt;/td&gt;
&lt;td&gt;Placing an order feels slightly slower&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;With more time, natural extensions are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Biometric confirmation&lt;/strong&gt; before large orders&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Regional compliance checks&lt;/strong&gt;, such as pattern day trading rules&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Watchlists&lt;/strong&gt; with their own price subscriptions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Offline support for history&lt;/strong&gt;, so charts for past periods open without a connection&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accessibility&lt;/strong&gt;, such as announcing price changes to screen readers without flooding them&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don't call the design perfect. Each choice above has a cost, and naming it shows you understand the design.&lt;/p&gt;

</description>
      <category>mobile</category>
      <category>systemdesign</category>
      <category>android</category>
      <category>ios</category>
    </item>
    <item>
      <title>Design WhatsApp: A Mobile System Design Walkthrough</title>
      <dc:creator>Shakibaenur</dc:creator>
      <pubDate>Sun, 01 Nov 2026 12:13:57 +0000</pubDate>
      <link>https://dev.to/shakibaenur/design-whatsapp-a-mobile-system-design-walkthrough-4ici</link>
      <guid>https://dev.to/shakibaenur/design-whatsapp-a-mobile-system-design-walkthrough-4ici</guid>
      <description>&lt;p&gt;"Design WhatsApp" is one of the most common mobile system design questions. It looks simple: one person sends a message, another receives it. On a phone, it runs into most of the hard problems in mobile engineering.&lt;/p&gt;

&lt;p&gt;The app needs two-way real-time streaming that doesn't drain the battery. Messages must stay in order when phone clocks disagree. Nothing can be lost when the network drops mid-send. And people still need to be reached when the app is closed.&lt;/p&gt;

&lt;p&gt;This walkthrough answers the question the way you would in a 45-minute interview, in five steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Clarify the requirements&lt;/li&gt;
&lt;li&gt;Design the API and data model&lt;/li&gt;
&lt;li&gt;Sketch the client architecture&lt;/li&gt;
&lt;li&gt;Deep dive into the hard problems&lt;/li&gt;
&lt;li&gt;Wrap up with trade-offs&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The focus is the mobile client. The backend appears only where the app depends on it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Clarify the Requirements
&lt;/h2&gt;

&lt;p&gt;Don't start drawing. "Chat" could mean one-to-one texting, group channels, voice calls, or all of them. Agree on the scope with the interviewer first.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functional requirements
&lt;/h3&gt;

&lt;p&gt;For a 45-minute session, a sensible scope is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A chat list showing recent conversations, newest first, with unread counts&lt;/li&gt;
&lt;li&gt;One-to-one text messages, sent and received in real time&lt;/li&gt;
&lt;li&gt;Online status and a typing indicator inside a conversation&lt;/li&gt;
&lt;li&gt;Message status ticks: sent, delivered, and read&lt;/li&gt;
&lt;li&gt;Message history available offline&lt;/li&gt;
&lt;li&gt;Push notifications when the app is in the background or closed&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Non-functional requirements
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Requirement&lt;/th&gt;
&lt;th&gt;Target&lt;/th&gt;
&lt;th&gt;What it means for the client&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Scale&lt;/td&gt;
&lt;td&gt;About 30 million daily users&lt;/td&gt;
&lt;td&gt;Keep payloads small and reconnects cheap&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Latency&lt;/td&gt;
&lt;td&gt;Messages appear in under a second on a good network&lt;/td&gt;
&lt;td&gt;A persistent connection, not polling&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Reliability&lt;/td&gt;
&lt;td&gt;Zero lost messages&lt;/td&gt;
&lt;td&gt;Every outgoing message is saved to disk before sending&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Battery&lt;/td&gt;
&lt;td&gt;No noticeable drain while idle&lt;/td&gt;
&lt;td&gt;One connection, no timers waking the radio&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data ownership&lt;/td&gt;
&lt;td&gt;The phone holds the real history&lt;/td&gt;
&lt;td&gt;The local database is the source of truth&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Data ownership shapes the rest of the design. WhatsApp uses the server mainly as a relay, so storage, ordering, and sync logic live on the phone.&lt;/p&gt;

&lt;h3&gt;
  
  
  Out of scope
&lt;/h3&gt;

&lt;p&gt;To leave time for the hard parts, set these aside: sign-up and contacts sync, group chats, media and voice notes, calls, and the details of end-to-end encryption. Mention that each is a natural extension.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Design the API and Data Model
&lt;/h2&gt;

&lt;p&gt;Traffic in a chat app flows both ways. The client starts some actions: sending a message, syncing missed ones. The server starts others: a new message arrives, someone is typing, a message was read. HTTP alone can't deliver server-started events, because the app never asked for them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Choosing the protocol
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Verdict for chat on mobile&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Short polling&lt;/td&gt;
&lt;td&gt;Rejected. Constant requests keep the radio awake and drain the battery&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Long polling&lt;/td&gt;
&lt;td&gt;Workable, but millions of held requests are hard to balance across servers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Server-Sent Events&lt;/td&gt;
&lt;td&gt;Rejected. Server-to-client only, so sending still needs another channel&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;WebSocket&lt;/td&gt;
&lt;td&gt;Chosen. One persistent two-way connection with tiny per-message overhead&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The design uses a &lt;strong&gt;hybrid&lt;/strong&gt;: HTTPS REST for sending and syncing, and one encrypted WebSocket for server-pushed events. REST gives sends clear success and failure codes, retries, and auth. The socket stays small and focused, which makes it easier to scale and to reconnect.&lt;/p&gt;

&lt;h3&gt;
  
  
  REST endpoints
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /v1/messages
Authorization: Bearer &amp;lt;token&amp;gt;
{
  "requestId": "6f1c...e2",      // client-generated UUID (idempotency key)
  "toUserId": "u_482",
  "content": "See you at 7",
  "clientCreatedAt": 1761980400000
}
→ 201 Created
{ "requestId": "6f1c...e2", "messageId": "1853920018473312256", "serverCreatedAt": 1761980400412 }

GET /v1/messages?afterMessageId=1853920018473312256&amp;amp;limit=100
→ 200 OK { "messages": [ ... ], "hasMore": false }

POST /v1/messages/receipts
{ "messageIds": ["..."], "type": "DELIVERED" | "READ" }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  WebSocket events
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wss://chat.example.com/v1/socket

{ "type": "new_message",     "payload": { "messageId": "...", "fromUserId": "...", "content": "..." } }
{ "type": "receipt",         "payload": { "messageIds": ["..."], "status": "DELIVERED" | "READ" } }
{ "type": "typing",          "payload": { "userId": "...", "isTyping": true } }
{ "type": "presence",        "payload": { "userId": "...", "online": false, "lastSeenAt": 1761980400000 } }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explain these three decisions to the interviewer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The client generates &lt;code&gt;requestId&lt;/code&gt;.&lt;/strong&gt; If a send times out and the app retries, the server recognises the duplicate and returns the original result instead of storing the message twice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The server generates &lt;code&gt;messageId&lt;/code&gt; and the timestamp.&lt;/strong&gt; Phone clocks can be wrong or changed by the user, so the server is the authority on order.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sync is cursor-based.&lt;/strong&gt; &lt;code&gt;afterMessageId&lt;/code&gt; asks for everything after the last message the phone has, so a reconnect fetches exactly what was missed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Message states
&lt;/h3&gt;

&lt;p&gt;Every outgoing message moves through one state machine, and the UI simply renders the current state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PENDING ──send ok──▶ SENT ──receipt──▶ DELIVERED ──receipt──▶ READ
   │
   └──retries exhausted──▶ FAILED ──user taps retry──▶ PENDING
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;State&lt;/th&gt;
&lt;th&gt;UI&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;PENDING&lt;/td&gt;
&lt;td&gt;Clock icon&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SENT&lt;/td&gt;
&lt;td&gt;One grey tick&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DELIVERED&lt;/td&gt;
&lt;td&gt;Two grey ticks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;READ&lt;/td&gt;
&lt;td&gt;Two blue ticks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;FAILED&lt;/td&gt;
&lt;td&gt;Red warning with "Tap to retry"&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Step 3: Sketch the Client Architecture
&lt;/h2&gt;

&lt;p&gt;The app splits into a UI layer and a data layer, with data flowing one way: actions go down, state comes up.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UI LAYER
  ChatListScreen      → ChatListViewModel
  ConversationScreen  → ConversationViewModel
                               │ actions ↓   ↑ state (Flow / Combine)
DATA LAYER
  ConversationsRepository   (chat list, unread counts)
  MessagesRepository        (history, sending, receipts)
  PresenceRepository        (online + typing, memory only)
        │
        ├─ MessagesApi         REST: send, sync, receipts
        ├─ SocketClient        WebSocket: events in, typing out
        ├─ ChatDatabase        SQLite via Room / GRDB
        └─ SendScheduler       background worker for the outbox
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The local database is the source of truth
&lt;/h3&gt;

&lt;p&gt;The screens never read from the network. They observe the local database, and everything else writes into it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A message the user sends is written to the database first, then sent.&lt;/li&gt;
&lt;li&gt;A message arriving over the socket is written to the database, then shown.&lt;/li&gt;
&lt;li&gt;A sync after reconnecting writes the missed messages to the database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The UI reacts to database changes, so one code path draws every message, whatever its origin. Offline history works automatically, and two screens can never disagree.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MessagesRepository&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;dao&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MessageDao&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;scheduler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SendScheduler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;conversation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;peerId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Flow&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
        &lt;span class="n"&gt;dao&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;observeConversation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;peerId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// the only thing the UI reads&lt;/span&gt;

    &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;peerId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;dao&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insertOutgoing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nc"&gt;OutgoingMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;requestId&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randomUUID&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
                &lt;span class="n"&gt;toUserId&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;peerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PENDING&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;createdAt&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;currentTimeMillis&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;scheduler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;kick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;                  &lt;span class="c1"&gt;// send in the background&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why presence lives in memory
&lt;/h3&gt;

&lt;p&gt;Online status and typing indicators change every few seconds and are useless a minute later. Writing them to disk wastes battery and flash storage. &lt;code&gt;PresenceRepository&lt;/code&gt; keeps them in memory only, and they're rebuilt from socket events after every reconnect.&lt;/p&gt;

&lt;h3&gt;
  
  
  Threading
&lt;/h3&gt;

&lt;p&gt;All database work, JSON parsing, and socket handling run off the main thread: &lt;code&gt;Dispatchers.IO&lt;/code&gt; on Android, actors and background tasks on iOS. The main thread only renders state. When a busy conversation receives many messages at once, they're written in a single transaction, so the list updates once instead of fifty times.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Deep Dive into the Hard Problems
&lt;/h2&gt;

&lt;p&gt;Choose the problems that matter most for this app and lead the discussion. For WhatsApp, I'd raise these six.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Message ordering
&lt;/h3&gt;

&lt;p&gt;Messages can arrive out of order because of network delays, offline sends, and phones whose clocks disagree.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sorting by the phone's clock&lt;/strong&gt; shows messages instantly, but clocks drift and users can change them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sorting by server ID&lt;/strong&gt; is consistent everywhere, but an offline message has no server ID yet.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The fix combines both. Confirmed messages are ordered by a time-sortable server ID, such as a 64-bit Snowflake ID, where a newer message always has a higher number. Pending messages are ordered by local time and pinned to the bottom with a clock icon. When the server confirms one, it takes its real ID and settles into its correct position.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;peer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;peer&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt;
  &lt;span class="k"&gt;CASE&lt;/span&gt; &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;server_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;ELSE&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;END&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;-- pending last&lt;/span&gt;
  &lt;span class="n"&gt;server_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;local_created_at&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. The send pipeline
&lt;/h3&gt;

&lt;p&gt;A message must survive the app being killed, the phone rebooting, or the network dropping mid-request. So it's written to disk before anything is sent:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The user taps send. A row is inserted with status &lt;code&gt;PENDING&lt;/code&gt; and a new &lt;code&gt;requestId&lt;/code&gt;. The bubble appears instantly.&lt;/li&gt;
&lt;li&gt;The background &lt;code&gt;SendScheduler&lt;/code&gt; (WorkManager on Android, &lt;code&gt;BGTaskScheduler&lt;/code&gt; plus a URLSession background task on iOS) picks up pending rows in order.&lt;/li&gt;
&lt;li&gt;It sends &lt;code&gt;POST /v1/messages&lt;/code&gt; with the same &lt;code&gt;requestId&lt;/code&gt; on every attempt.&lt;/li&gt;
&lt;li&gt;On &lt;code&gt;201 Created&lt;/code&gt;, one transaction stores the server's &lt;code&gt;messageId&lt;/code&gt; and sets the status to &lt;code&gt;SENT&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;On a network error, the row stays &lt;code&gt;PENDING&lt;/code&gt;, its retry count increases, and the scheduler waits with exponential backoff and jitter: about 1, 2, 4, then 8 seconds, up to a cap.&lt;/li&gt;
&lt;li&gt;After too many attempts, the row becomes &lt;code&gt;FAILED&lt;/code&gt; and the bubble shows "Tap to retry".
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;backoffDelayMs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;base&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1_000L&lt;/span&gt; &lt;span class="n"&gt;shl&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;coerceAtMost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// 1s, 2s, 4s ... 64s&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;jitter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nextLong&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;// spread retries out&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;jitter&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;coerceAtMost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60_000L&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The jitter matters at scale. Without it, every phone that lost connection in the same outage retries at the same instant and overloads the server again.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;requestId&lt;/code&gt; makes retries safe. If the first request actually reached the server but the response was lost, the retry returns the original &lt;code&gt;messageId&lt;/code&gt; instead of creating a second message.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Delivery and read receipts
&lt;/h3&gt;

&lt;p&gt;The ticks come from receipts flowing back from the recipient:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the recipient's phone stores an incoming message, it sends a &lt;code&gt;DELIVERED&lt;/code&gt; receipt.&lt;/li&gt;
&lt;li&gt;When the conversation is on screen and the message is visible, it sends a &lt;code&gt;READ&lt;/code&gt; receipt.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Receipts are batched. Opening a chat with 40 unread messages sends one request with 40 IDs, not 40 requests. When a receipt arrives for the sender, the client updates the status column, and the ticks change through the normal database flow.&lt;/p&gt;

&lt;p&gt;A receipt can only move a message forward, never back. A late &lt;code&gt;DELIVERED&lt;/code&gt; that arrives after &lt;code&gt;READ&lt;/code&gt; is ignored.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. The socket lifecycle
&lt;/h3&gt;

&lt;p&gt;A WebSocket on a phone dies often: tunnels, Wi-Fi handoffs, the OS suspending the app. The client must manage it carefully:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Connect only while the app is in the foreground.&lt;/strong&gt; In the background, rely on push notifications to save battery.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Detect dead connections with heartbeats.&lt;/strong&gt; Send a ping every 30–60 seconds. If no pong comes back, assume the connection is half-open, close it, and reconnect.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reconnect with backoff and jitter&lt;/strong&gt;, the same as message retries, and immediately when the OS reports the network is back.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sync after every reconnect.&lt;/strong&gt; Events sent while the socket was down are lost, so call &lt;code&gt;GET /v1/messages?afterMessageId=&amp;lt;last&amp;gt;&lt;/code&gt; to fill the gap. Then flush the outbox.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The socket delivers new events quickly, and the sync endpoint makes sure none are missing.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Presence and typing indicators
&lt;/h3&gt;

&lt;p&gt;These are the noisiest events in the system, so throttle them on the client:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Send &lt;code&gt;isTyping: true&lt;/code&gt; at most once every 3 seconds while the user types, and &lt;code&gt;false&lt;/code&gt; when they stop or send.&lt;/li&gt;
&lt;li&gt;On the receiving side, hide the indicator if no update arrives within about 5 seconds. A lost &lt;code&gt;false&lt;/code&gt; event never leaves "typing..." on screen forever.&lt;/li&gt;
&lt;li&gt;Subscribe to presence only for the open conversation, not the whole contact list.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6. The local schema
&lt;/h3&gt;

&lt;p&gt;Three tables cover the scope:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;user_id&lt;/span&gt;      &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;         &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;avatar_url&lt;/span&gt;   &lt;span class="nb"&gt;TEXT&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;conversations&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;peer_id&lt;/span&gt;              &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="n"&gt;last_message_preview&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;last_activity_at&lt;/span&gt;     &lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;unread_count&lt;/span&gt;         &lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;local_id&lt;/span&gt;          &lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="n"&gt;AUTOINCREMENT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;request_id&lt;/span&gt;        &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;-- set for messages this phone sent&lt;/span&gt;
  &lt;span class="n"&gt;server_id&lt;/span&gt;         &lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="c1"&gt;-- NULL until the server confirms&lt;/span&gt;
  &lt;span class="n"&gt;peer_id&lt;/span&gt;           &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;is_outgoing&lt;/span&gt;       &lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;content&lt;/span&gt;           &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;status&lt;/span&gt;            &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;        &lt;span class="c1"&gt;-- PENDING, SENT, DELIVERED, READ, FAILED&lt;/span&gt;
  &lt;span class="n"&gt;retry_count&lt;/span&gt;       &lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;local_created_at&lt;/span&gt;  &lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_messages_peer&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;peer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;server_id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;conversations&lt;/code&gt; table is denormalised on purpose. The chat list is the most-opened screen, and reading a preview and unread count from one row is far cheaper than scanning every message.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;UNIQUE&lt;/code&gt; constraint on &lt;code&gt;server_id&lt;/code&gt; protects against duplicates. If the same message arrives over the socket and again in a sync, the second insert is ignored.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus: push notifications
&lt;/h3&gt;

&lt;p&gt;When the app isn't in the foreground, there's no socket. The server sends a push through Firebase Cloud Messaging (Android) or APNs (iOS). A push only signals that something new exists. It wakes the app or shows a banner, and the app then fetches the messages from the sync endpoint, because pushes can be delayed, collapsed, or dropped by power-saving modes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Wrap Up
&lt;/h2&gt;

&lt;p&gt;In the last few minutes, summarise the decisions and be honest about what each one costs:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Decision&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;th&gt;Trade-off&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;REST for sends, WebSocket for events&lt;/td&gt;
&lt;td&gt;Clear errors and retries for sends; a small, focused socket&lt;/td&gt;
&lt;td&gt;Two channels to maintain&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Local database as source of truth&lt;/td&gt;
&lt;td&gt;Offline history, one rendering path, no inconsistent screens&lt;/td&gt;
&lt;td&gt;More sync and migration logic on the client&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Outbox with &lt;code&gt;requestId&lt;/code&gt; and backoff&lt;/td&gt;
&lt;td&gt;No lost or duplicate messages&lt;/td&gt;
&lt;td&gt;Pending messages need their own UI states&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Server IDs for ordering, local time for pending&lt;/td&gt;
&lt;td&gt;Instant feedback plus a consistent final order&lt;/td&gt;
&lt;td&gt;A message can shift position once confirmed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Socket only in the foreground&lt;/td&gt;
&lt;td&gt;Saves battery&lt;/td&gt;
&lt;td&gt;Background delivery depends on push, which can be delayed&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Then name what you'd add with more time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Group chats:&lt;/strong&gt; fan-out on the server, per-member receipts, and member roles&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Media:&lt;/strong&gt; compress on the phone, upload to object storage first, then send a message that references the file&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;End-to-end encryption:&lt;/strong&gt; keys stored on the device, so the server only relays encrypted content&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiple devices:&lt;/strong&gt; syncing history and read state between a phone and a desktop&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Encrypted local storage:&lt;/strong&gt; SQLCipher or the platform's file protection for the message database&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don't call the design perfect. Each choice above has a cost, and naming it shows you understand the design.&lt;/p&gt;

</description>
      <category>mobile</category>
      <category>systemdesign</category>
      <category>android</category>
      <category>ios</category>
    </item>
    <item>
      <title>A Mind Map for Mobile System Design Preparation</title>
      <dc:creator>Shakibaenur</dc:creator>
      <pubDate>Wed, 02 Sep 2026 09:03:17 +0000</pubDate>
      <link>https://dev.to/shakibaenur/a-mind-map-for-mobile-system-design-preparation-2m5l</link>
      <guid>https://dev.to/shakibaenur/a-mind-map-for-mobile-system-design-preparation-2m5l</guid>
      <description>&lt;p&gt;Mobile system design interviews can feel overwhelming.&lt;/p&gt;

&lt;p&gt;You're asked to design something that sounds simple — a news feed, chat application, ride-sharing app, video platform, or social network — but suddenly you're expected to reason about much more than UI and API calls.&lt;/p&gt;

&lt;p&gt;You need to think about application architecture, networking, caching, offline support, synchronization, concurrency, storage, performance, security, observability, and sometimes even how the backend interacts with the mobile client.&lt;/p&gt;

&lt;p&gt;The difficult part isn't that any one of these topics is impossible to learn.&lt;/p&gt;

&lt;p&gt;The difficult part is knowing &lt;strong&gt;which topics matter, how they connect, and how to apply them during an interview.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's what this guide is about.&lt;/p&gt;

&lt;p&gt;We'll build a mental model for mobile system design interviews and walk through the major areas you should prepare.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is Mobile System Design?
&lt;/h2&gt;

&lt;p&gt;Mobile system design is an interview format where you're given a product or feature and asked to design the architecture of a mobile application that supports it.&lt;/p&gt;

&lt;p&gt;For example, an interviewer might ask:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How would you design a news feed app?&lt;/li&gt;
&lt;li&gt;How would you build a chat application?&lt;/li&gt;
&lt;li&gt;How would you design a ride-sharing app?&lt;/li&gt;
&lt;li&gt;How would you support offline editing in a mobile application?&lt;/li&gt;
&lt;li&gt;How would you design a video streaming application?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The answer isn't simply:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"I'll create a UI, call an API, and display the response."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A production mobile application has many layers.&lt;/p&gt;

&lt;p&gt;A simplified architecture might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────────────────────┐
│          UI Layer           │
│ SwiftUI / UIKit             │
│ Jetpack Compose / Views     │
└──────────────┬──────────────┘
               │
┌──────────────▼──────────────┐
│       Domain / Business     │
│          Logic              │
└──────────────┬──────────────┘
               │
┌──────────────▼──────────────┐
│          Data Layer         │
│ API • Cache • Database      │
└──────────────┬──────────────┘
               │
        ┌──────▼──────┐
        │   Network   │
        └──────┬──────┘
               │
        ┌──────▼──────┐
        │   Backend   │
        └─────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's only the beginning.&lt;/p&gt;

&lt;p&gt;The application also has to deal with things that are particularly important on mobile:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;unreliable networks&lt;/li&gt;
&lt;li&gt;limited battery&lt;/li&gt;
&lt;li&gt;limited memory&lt;/li&gt;
&lt;li&gt;intermittent connectivity&lt;/li&gt;
&lt;li&gt;background execution restrictions&lt;/li&gt;
&lt;li&gt;application lifecycle changes&lt;/li&gt;
&lt;li&gt;device storage constraints&lt;/li&gt;
&lt;li&gt;different screen sizes and hardware&lt;/li&gt;
&lt;li&gt;application updates&lt;/li&gt;
&lt;li&gt;security of data stored on the device&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is what makes mobile system design different.&lt;/p&gt;




&lt;h1&gt;
  
  
  A Mind Map for Mobile System Design Preparation
&lt;/h1&gt;

&lt;p&gt;Because mobile system design combines many areas of software engineering, preparing topic-by-topic can quickly become confusing.&lt;/p&gt;

&lt;p&gt;You might study caching today, API design tomorrow, and Android lifecycle the next day — without understanding how they fit together.&lt;/p&gt;

&lt;p&gt;A better approach is to organize your preparation into a few major areas.&lt;/p&gt;

&lt;p&gt;This guide divides mobile system design into nine sections:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Mobile Domain&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;API Design &amp;amp; Networking&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Software Architecture &amp;amp; Design Patterns&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data Storage&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Performance &amp;amp; Optimization&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Observability &amp;amp; Testing&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Privacy &amp;amp; Security&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Advanced Topics&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interview Strategy&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Think of these as the major branches of your preparation.&lt;/p&gt;




&lt;h1&gt;
  
  
  1. Mobile Domain
&lt;/h1&gt;

&lt;p&gt;The first area is understanding the mobile platform itself.&lt;/p&gt;

&lt;p&gt;You don't necessarily need to memorize every framework API. Instead, you should understand the constraints and architectural decisions that mobile platforms impose.&lt;/p&gt;

&lt;h2&gt;
  
  
  UI Frameworks
&lt;/h2&gt;

&lt;p&gt;Modern mobile development generally has two broad approaches.&lt;/p&gt;

&lt;h3&gt;
  
  
  Declarative UI
&lt;/h3&gt;

&lt;p&gt;You describe what the UI should look like for a particular state.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SwiftUI on iOS&lt;/li&gt;
&lt;li&gt;Jetpack Compose on Android&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, a screen might conceptually be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;State
  ↓
UI
  ↓
User Action
  ↓
State Update
  ↓
UI Recomposition
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This model is particularly useful when discussing state management and unidirectional data flow.&lt;/p&gt;

&lt;h3&gt;
  
  
  Imperative UI
&lt;/h3&gt;

&lt;p&gt;The application explicitly creates and modifies UI components.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;UIKit on iOS&lt;/li&gt;
&lt;li&gt;XML/code-based Views on Android&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In an interview, the important question isn't usually which framework you prefer.&lt;/p&gt;

&lt;p&gt;It's understanding &lt;strong&gt;how UI state flows through your architecture&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Lifecycle Management
&lt;/h2&gt;

&lt;p&gt;Mobile applications don't run continuously like a server.&lt;/p&gt;

&lt;p&gt;The operating system can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;move your application to the background&lt;/li&gt;
&lt;li&gt;suspend it&lt;/li&gt;
&lt;li&gt;terminate it&lt;/li&gt;
&lt;li&gt;recreate screens&lt;/li&gt;
&lt;li&gt;reclaim memory&lt;/li&gt;
&lt;li&gt;restore state later&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On iOS, you'll encounter concepts such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;UIViewController&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AppDelegate&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SceneDelegate&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;SwiftUI view lifecycle&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On Android, important concepts include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Activity&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Fragment&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Application&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Jetpack Compose lifecycle&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This becomes important when designing features such as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What happens if the user starts uploading a file and leaves the application?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The answer requires much more than UI code.&lt;/p&gt;




&lt;h1&gt;
  
  
  Threading and Concurrency
&lt;/h1&gt;

&lt;p&gt;Mobile applications have a main thread responsible for responsive UI work.&lt;/p&gt;

&lt;p&gt;Blocking that thread with expensive operations can result in a poor user experience or even an application that appears frozen.&lt;/p&gt;

&lt;p&gt;A simplified model is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                ┌───────────────┐
                │   Main Thread │
                │ UI / Events   │
                └───────┬───────┘
                        │
                 Dispatch Work
                        │
              ┌─────────▼─────────┐
              │ Background Workers │
              │ Network / CPU / DB │
              └────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On iOS, you should understand concepts such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Grand Central Dispatch (GCD)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;OperationQueue&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;async/await&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Tasks&lt;/li&gt;
&lt;li&gt;Actors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On Android, important concepts include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Looper&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Handler&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;thread pools&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ThreadPoolExecutor&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Kotlin Coroutines&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;During system design interviews, you should be able to identify work that should &lt;strong&gt;not&lt;/strong&gt; happen on the main thread.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UI Thread
   │
   ├── Render UI
   ├── Handle user interaction
   │
   └── Dispatch expensive work
              │
              ├── Network request
              ├── Database query
              ├── Image processing
              └── File processing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Navigation
&lt;/h1&gt;

&lt;p&gt;Navigation becomes especially interesting when your application supports deep links.&lt;/p&gt;

&lt;p&gt;Imagine a user taps a notification:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Push Notification
       ↓
   Deep Link
       ↓
   Application
       ↓
   Authentication Check
       ↓
   Target Screen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On iOS, common concepts include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;UINavigationController&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;SwiftUI &lt;code&gt;NavigationStack&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Coordinator patterns&lt;/li&gt;
&lt;li&gt;Deep Links&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On Android:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Navigation Component&lt;/li&gt;
&lt;li&gt;Deep Links&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The system design question isn't simply:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"How do I navigate to another screen?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead, consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What happens when the user isn't logged in?&lt;/li&gt;
&lt;li&gt;What happens when the application was terminated?&lt;/li&gt;
&lt;li&gt;How do you restore the navigation state?&lt;/li&gt;
&lt;li&gt;What happens if the target resource no longer exists?&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Data Binding and State Propagation
&lt;/h1&gt;

&lt;p&gt;A mobile application often has several components interested in the same data.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             Repository
                 │
        ┌────────┼────────┐
        │        │        │
      Home    Profile   Details
        │        │        │
        └────────┴────────┘
                 │
                UI
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On iOS, you may encounter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Combine&lt;/li&gt;
&lt;li&gt;KVO&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ObservableObject&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;completion handlers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On Android:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LiveData&lt;/li&gt;
&lt;li&gt;Kotlin Flow&lt;/li&gt;
&lt;li&gt;StateFlow&lt;/li&gt;
&lt;li&gt;Coroutines&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The important system-design concept is &lt;strong&gt;how state moves through the application without creating inconsistent or duplicated state&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  2. API Design and Networking
&lt;/h1&gt;

&lt;p&gt;The mobile client is only one side of a distributed system.&lt;/p&gt;

&lt;p&gt;The application communicates with backend services through a network that may be slow, unreliable, or completely unavailable.&lt;/p&gt;

&lt;p&gt;This makes networking one of the most important areas of mobile system design.&lt;/p&gt;

&lt;h2&gt;
  
  
  Communication Protocols
&lt;/h2&gt;

&lt;p&gt;You should understand the tradeoffs between:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;REST&lt;/li&gt;
&lt;li&gt;GraphQL&lt;/li&gt;
&lt;li&gt;WebSockets&lt;/li&gt;
&lt;li&gt;gRPC&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Protocol&lt;/th&gt;
&lt;th&gt;Common Use Case&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;REST&lt;/td&gt;
&lt;td&gt;General API communication&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GraphQL&lt;/td&gt;
&lt;td&gt;Flexible client-driven data fetching&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;WebSockets&lt;/td&gt;
&lt;td&gt;Persistent real-time communication&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;gRPC&lt;/td&gt;
&lt;td&gt;Efficient service-to-service communication&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The right choice depends on the requirements.&lt;/p&gt;




&lt;h1&gt;
  
  
  Real-Time Updates
&lt;/h1&gt;

&lt;p&gt;Suppose you're designing a chat application.&lt;/p&gt;

&lt;p&gt;How does the client know that a new message has arrived?&lt;/p&gt;

&lt;p&gt;There are several possibilities:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Polling
   ↓
"Do I have new messages?"

Long Polling
   ↓
"Tell me when something changes."

SSE
   ↓
Server → Client stream

WebSocket
   ↓
Persistent bidirectional connection

Push Notification
   ↓
Wake/notify the mobile application
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each approach has different implications for battery usage, latency, infrastructure complexity, and reliability.&lt;/p&gt;

&lt;p&gt;A strong interview answer explains &lt;strong&gt;why&lt;/strong&gt; you choose one.&lt;/p&gt;




&lt;h1&gt;
  
  
  Pagination
&lt;/h1&gt;

&lt;p&gt;Mobile applications rarely download unlimited data at once.&lt;/p&gt;

&lt;p&gt;A news feed containing 100,000 posts should not return all 100,000 records to the device.&lt;/p&gt;

&lt;p&gt;Common pagination strategies include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Limit/Offset&lt;/li&gt;
&lt;li&gt;Page-based pagination&lt;/li&gt;
&lt;li&gt;Keyset pagination&lt;/li&gt;
&lt;li&gt;Cursor-based pagination&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET /posts?cursor=abc123&amp;amp;limit=20

        ↓

20 posts

        ↓

nextCursor = xyz789

        ↓

GET /posts?cursor=xyz789&amp;amp;limit=20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cursor-based pagination is often useful for feeds where data changes while the user is scrolling because it can provide more stable traversal than simple offsets.&lt;/p&gt;




&lt;h1&gt;
  
  
  API Integration
&lt;/h1&gt;

&lt;p&gt;Your mobile application needs an API layer that handles communication with the backend.&lt;/p&gt;

&lt;p&gt;On iOS, common tools include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;URLSession&lt;/li&gt;
&lt;li&gt;Alamofire&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On Android:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;OkHttp&lt;/li&gt;
&lt;li&gt;Retrofit&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You'll also need serialization and deserialization.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JSON Response
     ↓
DTO
     ↓
Mapper
     ↓
Domain Model
     ↓
UI
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keeping API DTOs separate from domain models can prevent backend API changes from leaking throughout the application.&lt;/p&gt;




&lt;h1&gt;
  
  
  Delta Updates
&lt;/h1&gt;

&lt;p&gt;Imagine your application already downloaded 10,000 messages yesterday.&lt;/p&gt;

&lt;p&gt;When the user opens the app today, downloading all 10,000 messages again is wasteful.&lt;/p&gt;

&lt;p&gt;Instead, the client can ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Give me only what changed since my last synchronization."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Last Sync
2026-09-01 10:00:00

        ↓

GET /messages?updated_after=2026-09-01T10:00:00

        ↓

Only changed records
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Delta updates can use mechanisms such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;timestamps&lt;/li&gt;
&lt;li&gt;sequence IDs&lt;/li&gt;
&lt;li&gt;ETags&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Last-Modified&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This reduces bandwidth, latency, and battery consumption.&lt;/p&gt;




&lt;h1&gt;
  
  
  Offline Support and Synchronization
&lt;/h1&gt;

&lt;p&gt;This is one of the areas that separates mobile system design from a simple API design exercise.&lt;/p&gt;

&lt;p&gt;A mobile device can lose connectivity at any time.&lt;/p&gt;

&lt;p&gt;Suppose a user edits a document while offline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User Edit
   ↓
Local Database
   ↓
Pending Sync Queue
   ↓
Network Available
   ↓
Upload Changes
   ↓
Server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you need to answer:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What happens if the same document was modified on another device?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's a conflict-resolution problem.&lt;/p&gt;

&lt;p&gt;You should be prepared to discuss:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;conflict resolution&lt;/li&gt;
&lt;li&gt;retry queues&lt;/li&gt;
&lt;li&gt;background synchronization&lt;/li&gt;
&lt;li&gt;batched requests&lt;/li&gt;
&lt;li&gt;resumable uploads&lt;/li&gt;
&lt;li&gt;resumable downloads&lt;/li&gt;
&lt;li&gt;prefetching&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Offline-first design is fundamentally about treating the local device as an important part of the distributed system.&lt;/p&gt;




&lt;h1&gt;
  
  
  Caching
&lt;/h1&gt;

&lt;p&gt;Caching can dramatically improve perceived performance.&lt;/p&gt;

&lt;p&gt;A mobile application might have several cache layers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UI
 │
 ▼
Memory Cache
 │
 ▼
Disk Cache
 │
 ▼
Network
 │
 ▼
Backend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Common mechanisms include:&lt;/p&gt;

&lt;h3&gt;
  
  
  HTTP caching
&lt;/h3&gt;

&lt;p&gt;Using headers such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Cache-Control&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ETag&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Last-Modified&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Memory caching
&lt;/h3&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;NSCache&lt;/code&gt; on iOS&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;LruCache&lt;/code&gt; on Android&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Disk caching
&lt;/h3&gt;

&lt;p&gt;Useful for larger or persistent data such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;images&lt;/li&gt;
&lt;li&gt;API responses&lt;/li&gt;
&lt;li&gt;downloaded content&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But caching introduces another problem:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;When does cached data become invalid?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is the cache invalidation problem.&lt;/p&gt;

&lt;p&gt;A strong system design answer should explain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what you're caching&lt;/li&gt;
&lt;li&gt;where you're caching it&lt;/li&gt;
&lt;li&gt;how long it remains valid&lt;/li&gt;
&lt;li&gt;how it gets invalidated&lt;/li&gt;
&lt;li&gt;what happens when stale data is displayed&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Authentication
&lt;/h1&gt;

&lt;p&gt;Mobile applications commonly use several authentication mechanisms.&lt;/p&gt;

&lt;p&gt;You should understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;username/password authentication over HTTPS&lt;/li&gt;
&lt;li&gt;Sign in with Apple&lt;/li&gt;
&lt;li&gt;Google Sign-In&lt;/li&gt;
&lt;li&gt;OAuth&lt;/li&gt;
&lt;li&gt;OpenID Connect&lt;/li&gt;
&lt;li&gt;biometric authentication&lt;/li&gt;
&lt;li&gt;multi-factor authentication&lt;/li&gt;
&lt;li&gt;access tokens&lt;/li&gt;
&lt;li&gt;refresh tokens&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A typical token flow looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Login
  ↓
Authorization Server
  ↓
Access Token + Refresh Token
  ↓
Mobile App
  ↓
API Request + Access Token
  ↓
Backend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the access token expires:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;API Request
    ↓
401 Unauthorized
    ↓
Refresh Token
    ↓
New Access Token
    ↓
Retry Original Request
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Token storage is also important.&lt;/p&gt;

&lt;p&gt;Sensitive credentials should not simply be stored in an arbitrary plaintext file or database.&lt;/p&gt;




&lt;h1&gt;
  
  
  Retry Policies
&lt;/h1&gt;

&lt;p&gt;Networks fail.&lt;/p&gt;

&lt;p&gt;A request might fail because of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;temporary connectivity problems&lt;/li&gt;
&lt;li&gt;server overload&lt;/li&gt;
&lt;li&gt;timeout&lt;/li&gt;
&lt;li&gt;DNS issues&lt;/li&gt;
&lt;li&gt;rate limiting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Blindly retrying immediately can make the problem worse.&lt;/p&gt;

&lt;p&gt;Instead, applications often use strategies such as:&lt;/p&gt;

&lt;h3&gt;
  
  
  Exponential Backoff
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Attempt 1 → immediately
Attempt 2 → 1 second
Attempt 3 → 2 seconds
Attempt 4 → 4 seconds
Attempt 5 → 8 seconds
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Jitter can be added to prevent many clients from retrying at exactly the same time.&lt;/p&gt;

&lt;p&gt;You should also understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;linear backoff&lt;/li&gt;
&lt;li&gt;circuit breakers&lt;/li&gt;
&lt;li&gt;retry-after responses&lt;/li&gt;
&lt;li&gt;retrying after token refresh&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The key interview principle is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Retries should be deliberate, bounded, and aware of whether an operation is safe to repeat.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  API Evolution
&lt;/h1&gt;

&lt;p&gt;Mobile applications create a unique API compatibility problem.&lt;/p&gt;

&lt;p&gt;A backend can be updated today, but millions of users may still have an older version of the application installed.&lt;/p&gt;

&lt;p&gt;Therefore, your backend may need to support multiple client versions simultaneously.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                    Backend API
                   /     |      \
                  /      |       \
             v1 Client  v2 Client  v3 Client
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good API evolution practices include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;adding fields without breaking old clients&lt;/li&gt;
&lt;li&gt;avoiding unnecessary breaking changes&lt;/li&gt;
&lt;li&gt;versioning APIs when necessary&lt;/li&gt;
&lt;li&gt;maintaining backward compatibility&lt;/li&gt;
&lt;li&gt;safely deprecating old fields&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is one reason mobile system design is closely connected to distributed systems.&lt;/p&gt;




&lt;h1&gt;
  
  
  CDN
&lt;/h1&gt;

&lt;p&gt;For applications serving large amounts of static or media content, a Content Delivery Network (CDN) can move content closer to users.&lt;/p&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Mobile App
    ↓
Origin Server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Mobile App
    ↓
Nearest CDN Edge
    ↓
Cache Hit → Content
    │
    └── Cache Miss
            ↓
       Origin Server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CDNs are especially useful for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;images&lt;/li&gt;
&lt;li&gt;videos&lt;/li&gt;
&lt;li&gt;static files&lt;/li&gt;
&lt;li&gt;application assets&lt;/li&gt;
&lt;li&gt;large downloads&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But you should still think about cache invalidation, TTLs, authentication, signed URLs, and regional distribution.&lt;/p&gt;




&lt;h1&gt;
  
  
  How to Use This Knowledge Map
&lt;/h1&gt;

&lt;p&gt;The goal isn't to memorize every technology listed above.&lt;/p&gt;

&lt;p&gt;Instead, use the map to identify your weak areas.&lt;/p&gt;

&lt;p&gt;Read through the topics and grade yourself.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Mobile Domain              4/5
API Design                 3/5
Architecture               5/5
Data Storage               2/5
Performance                3/5
Security                   2/5
Observability              2/5
Advanced Topics            1/5
Interview Strategy         3/5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your preparation becomes much more focused.&lt;/p&gt;

&lt;p&gt;Instead of saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"I need to study mobile system design."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"I understand mobile architecture, but I need to improve offline synchronization, caching, and security."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's a much more actionable preparation plan.&lt;/p&gt;




&lt;h1&gt;
  
  
  What Mid-Level Engineers Should Focus On
&lt;/h1&gt;

&lt;p&gt;If you're preparing for a mid-level mobile engineering position, focus first on the foundational concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;mobile application lifecycle&lt;/li&gt;
&lt;li&gt;UI architecture&lt;/li&gt;
&lt;li&gt;threading and concurrency&lt;/li&gt;
&lt;li&gt;navigation&lt;/li&gt;
&lt;li&gt;networking&lt;/li&gt;
&lt;li&gt;API design&lt;/li&gt;
&lt;li&gt;pagination&lt;/li&gt;
&lt;li&gt;caching&lt;/li&gt;
&lt;li&gt;authentication&lt;/li&gt;
&lt;li&gt;offline support&lt;/li&gt;
&lt;li&gt;local storage&lt;/li&gt;
&lt;li&gt;performance&lt;/li&gt;
&lt;li&gt;basic security&lt;/li&gt;
&lt;li&gt;testing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You don't need to master every advanced distributed-systems concept before starting mock interviews.&lt;/p&gt;

&lt;p&gt;Learn the fundamentals, then practice applying them.&lt;/p&gt;




&lt;h1&gt;
  
  
  What Senior and Staff Engineers Should Add
&lt;/h1&gt;

&lt;p&gt;For senior and staff-level interviews, interviewers may expect deeper reasoning around:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;large-scale synchronization&lt;/li&gt;
&lt;li&gt;conflict resolution&lt;/li&gt;
&lt;li&gt;advanced caching&lt;/li&gt;
&lt;li&gt;offline-first architecture&lt;/li&gt;
&lt;li&gt;real-time systems&lt;/li&gt;
&lt;li&gt;observability&lt;/li&gt;
&lt;li&gt;privacy&lt;/li&gt;
&lt;li&gt;security architecture&lt;/li&gt;
&lt;li&gt;scalability&lt;/li&gt;
&lt;li&gt;multi-region systems&lt;/li&gt;
&lt;li&gt;reliability&lt;/li&gt;
&lt;li&gt;advanced performance optimization&lt;/li&gt;
&lt;li&gt;complex data consistency problems&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The difference isn't simply knowing more technologies.&lt;/p&gt;

&lt;p&gt;It's being able to explain &lt;strong&gt;tradeoffs&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Why did you choose WebSockets instead of polling?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;is a much more important question than:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Do you know WebSockets?"&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  How to Practice Mobile System Design Interviews
&lt;/h1&gt;

&lt;p&gt;Reading is only part of the preparation.&lt;/p&gt;

&lt;p&gt;You need to practice designing systems yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solve at Least Five Problems
&lt;/h2&gt;

&lt;p&gt;Choose different types of applications.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Design a News Feed&lt;/li&gt;
&lt;li&gt;Design a Chat Application&lt;/li&gt;
&lt;li&gt;Design a Ride-Sharing App&lt;/li&gt;
&lt;li&gt;Design a Video Streaming App&lt;/li&gt;
&lt;li&gt;Design an Offline Notes Application&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Don't immediately look at someone else's solution.&lt;/p&gt;

&lt;p&gt;Start with a blank page.&lt;/p&gt;




&lt;h2&gt;
  
  
  Use the Same Process Every Time
&lt;/h2&gt;

&lt;p&gt;A useful interview structure is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Clarify Requirements
          ↓
2. Define Core User Flows
          ↓
3. Design Mobile Architecture
          ↓
4. Design API / Networking
          ↓
5. Design Local Storage
          ↓
6. Discuss Caching &amp;amp; Offline
          ↓
7. Discuss Performance
          ↓
8. Discuss Security
          ↓
9. Discuss Failure Cases
          ↓
10. Explain Tradeoffs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives you a repeatable framework rather than trying to improvise every interview.&lt;/p&gt;




&lt;h1&gt;
  
  
  Do Mock Interviews
&lt;/h1&gt;

&lt;p&gt;Try to complete at least &lt;strong&gt;two mock interviews&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;One should happen near the beginning of your preparation.&lt;/p&gt;

&lt;p&gt;The second should happen closer to your actual interview.&lt;/p&gt;

&lt;p&gt;The goal isn't just to test whether you know the material.&lt;/p&gt;

&lt;p&gt;You want to discover whether you can &lt;strong&gt;communicate your design clearly under time pressure&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A candidate might know everything about caching but still struggle if they spend 25 minutes discussing UI architecture and have no time left for scalability or failure scenarios.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Most Important Skill: Connecting the Pieces
&lt;/h1&gt;

&lt;p&gt;The biggest mistake in mobile system design preparation is studying each topic independently.&lt;/p&gt;

&lt;p&gt;Knowing caching is useful.&lt;/p&gt;

&lt;p&gt;Knowing WebSockets is useful.&lt;/p&gt;

&lt;p&gt;Knowing Coroutines is useful.&lt;/p&gt;

&lt;p&gt;Knowing SQLite or Core Data is useful.&lt;/p&gt;

&lt;p&gt;But the interview asks you to combine them.&lt;/p&gt;

&lt;p&gt;Consider a chat application.&lt;/p&gt;

&lt;p&gt;You might need:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                Chat Application
                       │
       ┌───────────────┼────────────────┐
       │               │                │
    WebSocket        Local DB         Push
       │               │                │
 Real-time          Offline           Background
 Messages           History           Notification
       │               │                │
       └───────────────┼────────────────┘
                       │
                  Sync Engine
                       │
                 Conflict Logic
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the real skill.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;System design is about understanding how individual engineering concepts work together to solve a product problem.&lt;/strong&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Final Takeaway
&lt;/h1&gt;

&lt;p&gt;Mobile system design isn't a single technology.&lt;/p&gt;

&lt;p&gt;It's the intersection of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;mobile platform knowledge&lt;/li&gt;
&lt;li&gt;software architecture&lt;/li&gt;
&lt;li&gt;networking&lt;/li&gt;
&lt;li&gt;distributed systems&lt;/li&gt;
&lt;li&gt;data storage&lt;/li&gt;
&lt;li&gt;performance engineering&lt;/li&gt;
&lt;li&gt;security&lt;/li&gt;
&lt;li&gt;reliability&lt;/li&gt;
&lt;li&gt;product requirements&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You don't need to learn everything at once.&lt;/p&gt;

&lt;p&gt;Start with the fundamentals.&lt;/p&gt;

&lt;p&gt;Identify your weak areas.&lt;/p&gt;

&lt;p&gt;Practice applying those concepts to real mobile products.&lt;/p&gt;

&lt;p&gt;Then gradually move into advanced topics and increasingly difficult system-design problems.&lt;/p&gt;

&lt;p&gt;The goal isn't to memorize a perfect architecture.&lt;/p&gt;

&lt;p&gt;The goal is to develop the ability to look at a problem and reason about:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;requirements → architecture → tradeoffs → failure modes → scalability → user experience.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's what makes someone strong at mobile system design interviews.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Complete Mobile System Design Roadmap
&lt;/h2&gt;

&lt;p&gt;This article covers the major concepts you need to understand, but mobile system design is a broad subject. If you want to follow the topics in a structured order, you can use the &lt;strong&gt;complete Mobile System Design roadmap on Algonur&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The roadmap brings the topics together so you can see what to learn first, what to study next, and how the different areas of mobile system design connect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;→ &lt;a href="https://www.algonur.com/#course/mobile-system-design/lesson/mobile-system-design%20" rel="noopener noreferrer"&gt;Explore the complete Mobile System Design roadmap on Algonur&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can use the roadmap alongside this article to identify gaps in your knowledge and build your preparation step by step.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>interview</category>
      <category>mobile</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>How to turn on charge only mode programatically if the mode if file transfer in android?</title>
      <dc:creator>Shakibaenur</dc:creator>
      <pubDate>Sun, 08 Oct 2023 07:20:23 +0000</pubDate>
      <link>https://dev.to/shakibaenur/how-to-turn-on-charge-only-mode-programatically-if-the-mode-if-file-transfer-in-android-56oe</link>
      <guid>https://dev.to/shakibaenur/how-to-turn-on-charge-only-mode-programatically-if-the-mode-if-file-transfer-in-android-56oe</guid>
      <description>&lt;p&gt;I will have to show this type of dialog if my device is connected to usb and I want to access the app. Here this dialog shows -Click to Charge Only will automatically set the USB protocol to Charge only mode and i can not access files from my phone&lt;/p&gt;

&lt;p&gt;Can Anyone help me to achieve this feature?&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmm578cfldl142q6v4hy.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmm578cfldl142q6v4hy.jpeg" alt=" " width="718" height="615"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have used to get device list but device list is empty. But still i dont thing this will solve my problem&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (entry in usbDeviceList.entries) {
            val usbDevice = entry.value

            // Check the USB device's connection mode
            val usbConfiguration = usbDevice.getConfiguration(0)
            val usbInterface = usbConfiguration.getInterface(0)
            val usbInterfaceClass = usbInterface.interfaceClass

            // Check if the interface class indicates MTP (Media Transfer Protocol)
            if (usbInterfaceClass == 6) {
                // The device is in MTP mode (File Transfer mode)
                // You can perform file transfer operations here
                Toast.makeText(applicationContext, "(File Transfer mode)", Toast.LENGTH_LONG).show()
            } else {
                // The device is not in MTP mode
                // Handle the mode accordingly
                Toast.makeText(applicationContext, "MTP mode", Toast.LENGTH_LONG).show()
            }
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Android — Drag and Drop</title>
      <dc:creator>Shakibaenur</dc:creator>
      <pubDate>Fri, 17 Feb 2023 17:21:00 +0000</pubDate>
      <link>https://dev.to/shakibaenur/android-drag-and-drop-3k3e</link>
      <guid>https://dev.to/shakibaenur/android-drag-and-drop-3k3e</guid>
      <description>&lt;p&gt;Today i want to talk about my one of the most favourite features in android development that is &lt;strong&gt;"Drag and Drop"&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In modern user interfaces, drag and drop has become an essential interaction pattern for enabling users to move objects around a screen with a touch gesture.&lt;/p&gt;

&lt;p&gt;The drag and drop framework in Android Kotlin provides a simple and flexible way to implement drag and drop functionality in your app. It allows users to drag a view or item from one location to another by simply tapping and holding the item, and then dragging it to its desired destination. The drag and drop framework is based on three key components: the &lt;strong&gt;DragEvent&lt;/strong&gt;, the &lt;strong&gt;DragShadow&lt;/strong&gt;, and the &lt;strong&gt;ClipData object&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DragEvent:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The DragEvent class is responsible for managing the entire drag and drop lifecycle. It provides a set of constants that represent different states of the drag and drop operation. These states include &lt;strong&gt;ACTION_DRAG_STARTED&lt;/strong&gt;, &lt;strong&gt;ACTION_DRAG_ENTERED&lt;/strong&gt;, &lt;strong&gt;ACTION_DRAG_LOCATION&lt;/strong&gt;, &lt;strong&gt;ACTION_DRAG_EXITED&lt;/strong&gt;, &lt;strong&gt;ACTION_DROP&lt;/strong&gt;, and &lt;strong&gt;ACTION_DRAG_ENDED&lt;/strong&gt;. These states are used to identify when a drag operation is starting, when it's in progress, and when it's completed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DragShadow:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The DragShadow class is responsible for creating a visual representation of the item being dragged. It is an optional component that can be used to provide a custom visual representation of the item being dragged. It's also possible to use the default drag shadow, which is a simple opaque image of the view being dragged.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ClipData:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;ClipData&lt;/strong&gt; class is responsible for storing the data associated with the item being dragged. This data can be anything that you want to associate with the item, such as text, images, or other types of data. You can use the &lt;strong&gt;ClipData.newPlainText()&lt;/strong&gt; method to create a new &lt;strong&gt;ClipData object&lt;/strong&gt; that contains plain text data, or you can create a custom ClipData object that contains any type of data.&lt;/p&gt;

&lt;p&gt;These were the fundamental parts. Now let's think about a Drag and Drop game you want to built for kids. To built this you must know about &lt;strong&gt;View.setOnTouchListener()&lt;/strong&gt; and &lt;strong&gt;View.OnDragEventListener()&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Note that i am just sharing the basic concept. Assume i want to match a shape. Let's call it &lt;strong&gt;"Match the Shape"&lt;/strong&gt;. I this application I have a triangle shaped object and want to match it with the shape. &lt;/p&gt;

&lt;p&gt;Now proceed of making this little application.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Click on &lt;strong&gt;File&lt;/strong&gt;, then &lt;strong&gt;New&lt;/strong&gt; =&amp;gt; &lt;strong&gt;New Project&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;After that include the Kotlin support and click on next.&lt;/li&gt;
&lt;li&gt;Select the minimum SDK as per convenience and click the next button.&lt;/li&gt;
&lt;li&gt;Then select the &lt;strong&gt;Empty&lt;/strong&gt; activity =&amp;gt; &lt;strong&gt;next&lt;/strong&gt; =&amp;gt; &lt;strong&gt;finish&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;View Binding&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I have used view binding in this project. To enable view binding in a module, set the &lt;strong&gt;viewBinding&lt;/strong&gt; build option to true in the module-level &lt;strong&gt;build.gradle&lt;/strong&gt; file, as shown in the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;buildFeatures {
    viewBinding = true
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;activity_main.xml file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the xml file we have two ImageView one is for showing the shape &lt;strong&gt;ivTriangle&lt;/strong&gt; and another one &lt;strong&gt;ivTriangleDrag&lt;/strong&gt; to drag it on the shape.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"&amp;gt;

    &amp;lt;TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:text="Match the Shape"
        android:textAlignment="center"
        android:textSize="24sp" /&amp;gt;

    &amp;lt;ImageView
        android:id="@+id/ivTriangle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="12dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:src="@drawable/ic_triangle" /&amp;gt;

    &amp;lt;ImageView
        android:id="@+id/ivTriangleDrag"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="12dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:src="@drawable/ic_triangle_drag" /&amp;gt;

&amp;lt;/LinearLayout&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;MainActivity.kt&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will implement the &lt;strong&gt;onTouch&lt;/strong&gt; and &lt;strong&gt;onDrag&lt;/strong&gt; functions of &lt;strong&gt;View.OnTouchListener&lt;/strong&gt; and &lt;strong&gt;View.OnDragListener&lt;/strong&gt; correspondingly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MainActivity : AppCompatActivity(), View.OnTouchListener, View.OnDragListener{

..........
 override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        ........
  }

 override fun onDrag(v: View?, event: DragEvent?): Boolean {
        .........
  }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here in &lt;strong&gt;onCreate&lt;/strong&gt; function we will initialise the &lt;strong&gt;setOnTouchListener&lt;/strong&gt; to &lt;strong&gt;ivTriangleDrag&lt;/strong&gt; and &lt;strong&gt;setOnDragListener&lt;/strong&gt; to &lt;strong&gt;ivTriangle&lt;/strong&gt;&amp;nbsp;. As we want to touch &lt;strong&gt;ivTriangleDrag&lt;/strong&gt; then drag it over &lt;strong&gt;ivTriangle&lt;/strong&gt; and drop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    mViewBinding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(mViewBinding.root)

    mViewBinding.ivTriangleDrag.setOnTouchListener(this)
    mViewBinding.ivTriangle.setOnDragListener(this)

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can see the &lt;strong&gt;Motion.ACTION_DOWN&lt;/strong&gt; event of &lt;strong&gt;ivTriangleDrag&lt;/strong&gt; will trigger when we will touch it and drop it in the screen anywhere.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;override fun onTouch(v: View?, event: MotionEvent?): Boolean {
    return if (event!!.action == MotionEvent.ACTION_DOWN) {
        val data = ClipData.newPlainText("", "")
        val shadowBuilder = DragShadowBuilder(v)
        v?.startDragAndDrop(data, shadowBuilder, v, 0)
        true
    } else {
        false
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the onDrag function will trigger the DragEvent callbacks. Now when we will drop &lt;strong&gt;ivTriangleDrag&lt;/strong&gt; over &lt;strong&gt;ivTriangle&lt;/strong&gt; then &lt;strong&gt;ACTION_DROP&lt;/strong&gt; will trigger and as we wanted we will change the background &lt;strong&gt;ivTriangle&lt;/strong&gt; and will &lt;strong&gt;INVISIBLE&lt;/strong&gt; &lt;strong&gt;ivTriangleDrag&lt;/strong&gt; Imageview.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;override fun onDrag(v: View?, event: DragEvent?): Boolean {
    when (event?.action) {
        ACTION_DRAG_STARTED -&amp;gt; {
            Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED");
        }
        ACTION_DRAG_ENTERED -&amp;gt; {
            Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED")
        }
        ACTION_DRAG_EXITED -&amp;gt; {
            Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED")
        }
        ACTION_DRAG_LOCATION -&amp;gt; {
            Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION")
        }
        ACTION_DRAG_ENDED -&amp;gt; {
            Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENDED")
        }
        ACTION_DROP -&amp;gt; {
            Log.d(msg, "ACTION_DROP event")
            mViewBinding.ivTriangle.setImageResource(R.drawable.ic_triangle_drag)
            mViewBinding.ivTriangleDrag.visibility = INVISIBLE
            Toast.makeText(this@MainActivity,"You have matched the shape!!",Toast.LENGTH_SHORT).show()
        }

    }
    return true
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a preview of "Match the Shape" App. And it's Ready.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqzkml4j7sefsx0vxova.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqzkml4j7sefsx0vxova.gif" alt="" width="222" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope this article helps you. To check out Full Code, here is the &lt;em&gt;&lt;strong&gt;&lt;a href="https://github.com/Shakibaenur/Drag_Drop.git" rel="noopener noreferrer"&gt;Repository&lt;/a&gt;&lt;/strong&gt;&lt;/em&gt;&amp;nbsp;.&lt;/p&gt;

&lt;p&gt;If you like the article do &lt;strong&gt;follow&lt;/strong&gt; me for more interesting topics on &lt;strong&gt;Android Development&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Happy Coding!&amp;nbsp;:)&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
