<?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: duong vu</title>
    <description>The latest articles on DEV Community by duong vu (@duong_vu_849ad2e23c665cff).</description>
    <link>https://dev.to/duong_vu_849ad2e23c665cff</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%2F4267245%2F27bb7f28-d642-4a8d-bb47-3487b1d84f39.jpg</url>
      <title>DEV Community: duong vu</title>
      <link>https://dev.to/duong_vu_849ad2e23c665cff</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/duong_vu_849ad2e23c665cff"/>
    <language>en</language>
    <item>
      <title>Android app architecture: ViewModel, UDF and modules</title>
      <dc:creator>duong vu</dc:creator>
      <pubDate>Thu, 26 Nov 2026 16:19:23 +0000</pubDate>
      <link>https://dev.to/duong_vu_849ad2e23c665cff/android-app-architecture-viewmodel-udf-and-modules-hgf</link>
      <guid>https://dev.to/duong_vu_849ad2e23c665cff/android-app-architecture-viewmodel-udf-and-modules-hgf</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://shippedcode.dev/android-app-architecture-viewmodel-udf-modules/" rel="noopener noreferrer"&gt;Shipped Code&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A screen usually starts with a ViewModel that loads a list, and 6 months later the same ViewModel formats dates, retries network calls and holds 3 booleans that contradict each other.&lt;/p&gt;

&lt;p&gt;An Android app architecture that holds up after release needs few rules. Each screen gets one immutable UI state, exposed as a &lt;code&gt;StateFlow&lt;/code&gt; by its ViewModel. User actions go up as function calls, and data sits behind a repository. Split into &lt;code&gt;app&lt;/code&gt;, &lt;code&gt;core&lt;/code&gt; and &lt;code&gt;feature&lt;/code&gt; modules only when build time or team boundaries ask for it, with every dependency pointing one way.&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="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ExpensesUiState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ExpenseRow&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;emptyList&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="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;isLoading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;errorMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ExpenseRow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&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="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One &lt;code&gt;data class&lt;/code&gt; per screen, exposed as &lt;code&gt;StateFlow&amp;lt;UiState&amp;gt;&lt;/code&gt;, collected with &lt;code&gt;collectAsStateWithLifecycle()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Events are plain ViewModel functions. Never expose &lt;code&gt;MutableStateFlow&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The ViewModel talks to a repository interface and never sees Retrofit or Room types.&lt;/li&gt;
&lt;li&gt;State the user typed survives rotation for free, but survives process death only through &lt;code&gt;SavedStateHandle&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;What you'll learn&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to model one screen as a UI state, events and a ViewModel, and test it on the JVM.&lt;/li&gt;
&lt;li&gt;Where the data layer starts and what the ViewModel must not know.&lt;/li&gt;
&lt;li&gt;When splitting into &lt;code&gt;app&lt;/code&gt;, &lt;code&gt;core&lt;/code&gt; and &lt;code&gt;feature&lt;/code&gt; modules pays off, and how Gradle keeps the graph one-way.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This post assumes Kotlin, Compose basics and some ViewModel use. Read through "Handling one-off events" for the screen pattern; the modules section matters once build time or team size becomes a problem.&lt;/p&gt;

&lt;p&gt;I build Android apps such as &lt;a href="https://shippedcode.dev/portfolio/pdfizi/" rel="noopener noreferrer"&gt;PDFizi&lt;/a&gt;, a Jetpack Compose PDF reader and editor.&lt;/p&gt;

&lt;h2&gt;
  
  
  The UDF loop
&lt;/h2&gt;

&lt;p&gt;Unidirectional data flow (UDF) means data moves in one direction around a loop. The screen renders state and reports what the user did. The ViewModel decides what that means, asks the repository for data, and publishes a new state. That is why the diagram below has events going up from the screen and state coming down from the ViewModel.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fshippedcode.dev%2F_astro%2Fudf-loop.DVmFl4eL_BGCnS.svg" 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%2Fshippedcode.dev%2F_astro%2Fudf-loop.DVmFl4eL_BGCnS.svg" alt="Vertical UDF loop: the Compose screen at the bottom sends events up to the ViewModel, the ViewModel calls refresh() on the repository at the top, the repository returns Flow&lt;List&lt;Expense&gt;&gt; down to the ViewModel, and the ViewModel sends StateFlow of UiState down to the screen" width="258" height="345"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The point of the loop is that there is exactly one place where screen state changes. When a bug report says "the spinner never stops", you look in one class.&lt;/p&gt;

&lt;p&gt;The example is smaller than a store app on purpose. It is an expense list split into 4 Gradle modules, built with Kotlin 2.4.21, AGP 9.5.2, Gradle 9.7.1, Compose BOM 2026.11.01 and Lifecycle 2.11.1, with &lt;code&gt;minSdk&lt;/code&gt; 24, &lt;code&gt;targetSdk&lt;/code&gt; 37 and &lt;code&gt;compileSdk&lt;/code&gt; 37.1.&lt;/p&gt;

&lt;p&gt;The screen reads only &lt;code&gt;ExpensesUiState&lt;/code&gt;, and the ViewModel's public surface is &lt;code&gt;uiState&lt;/code&gt; plus &lt;code&gt;onQueryChange()&lt;/code&gt;, &lt;code&gt;onRefresh()&lt;/code&gt; and &lt;code&gt;onDelete(id)&lt;/code&gt;. These are the pieces, followed by a diagram of how they call each other:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;Responsibility&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;Expense&lt;/code&gt; (&lt;code&gt;:core:model&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;The app's own model, with the amount stored in cents&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ExpenseDto&lt;/code&gt;, &lt;code&gt;ExpenseApi&lt;/code&gt; (&lt;code&gt;:core:data&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;What the network returns; only the data layer sees these&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ExpenseRepository&lt;/code&gt; + &lt;code&gt;DefaultExpenseRepository&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Exposes &lt;code&gt;Flow&amp;lt;List&amp;lt;Expense&amp;gt;&amp;gt;&lt;/code&gt;, refreshes and deletes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ExpensesUiState&lt;/code&gt;, &lt;code&gt;ExpenseRow&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Everything the screen draws, already formatted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ExpensesViewModel&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Combines data and screen state, handles events&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ExpensesRoute&lt;/code&gt;, &lt;code&gt;ExpensesScreen&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Collects state, renders it, forwards clicks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ExpensesApp&lt;/code&gt; (&lt;code&gt;:app&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Builds the repository and wires it in&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fshippedcode.dev%2F_astro%2Fcomponents.Dtgax2ab_Z110Trl.svg" 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%2Fshippedcode.dev%2F_astro%2Fcomponents.Dtgax2ab_Z110Trl.svg" alt="Component relationships: ExpensesApp builds the repository and passes it to ExpensesViewModel; ExpensesRoute/ExpensesScreen calls the ViewModel's event functions and gets StateFlow&lt;ExpensesUiState&gt; back; the ViewModel calls refresh() and delete() and collects expenses from ExpenseRepository; the repository calls fetchExpenses() on ExpenseApi, gets List&lt;ExpenseDto&gt; and maps it to Expense" width="741" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Read the diagram from the ViewModel outwards: the screen only calls event functions and receives state, the repository is the only thing the ViewModel calls down into, and &lt;code&gt;ExpenseApi&lt;/code&gt; and &lt;code&gt;ExpenseDto&lt;/code&gt; stay hidden behind the repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  One UI state class per screen
&lt;/h2&gt;

&lt;p&gt;The UI state is one immutable object that holds everything the screen needs to draw. You can model loading and errors as fields of a &lt;code&gt;data class&lt;/code&gt; (as above) or as a &lt;code&gt;sealed interface&lt;/code&gt; with &lt;code&gt;Loading&lt;/code&gt;, &lt;code&gt;Success&lt;/code&gt; and &lt;code&gt;Error&lt;/code&gt; cases.&lt;/p&gt;

&lt;p&gt;I pick the &lt;code&gt;data class&lt;/code&gt; for list screens. A list often has to show old rows, a refresh spinner and an error message at the same time, and a sealed interface forces you to choose one. A sealed interface fits better when the states really exclude each other, like a detail screen that has nothing to show until the first load finishes.&lt;/p&gt;

&lt;p&gt;The ViewModel builds the state by combining the repository's data with the small pieces of state the screen owns. Imports are omitted in the Kotlin snippets.&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ExpensesViewModel&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="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ExpenseRepository&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="n"&gt;savedState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SavedStateHandle&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="nc"&gt;ViewModel&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;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;savedState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;KEY_QUERY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&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="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MutableStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LoadStatus&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="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StateFlow&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ExpensesUiState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expenses&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;query&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;expenses&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="nc"&gt;ExpensesUiState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;expenses&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ignoreCase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&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;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toRow&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="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;isLoading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isLoading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;errorMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errorMessage&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stateIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;SharingStarted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WhileSubscribed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ExpensesUiState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isLoading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&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;private&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LoadStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;isLoading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;errorMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&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;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;Expense&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toRow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nc"&gt;ExpenseRow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Locale&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;US&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$%.2f"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amountCents&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;100.0&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;&lt;code&gt;stateIn(..., WhileSubscribed(5_000))&lt;/code&gt; keeps the upstream flows running while the screen is collecting and for 5 seconds after it stops. A rotation takes well under 5 seconds, so the combine is not restarted on every configuration change. Formatting the amount here, and not in the composable, keeps the screen dumb and makes the formatting testable on the JVM.&lt;/p&gt;

&lt;p&gt;On the Compose side, &lt;code&gt;collectAsStateWithLifecycle()&lt;/code&gt; from &lt;code&gt;lifecycle-runtime-compose&lt;/code&gt; stops collecting when the app goes to the background, which plain &lt;code&gt;collectAsState()&lt;/code&gt; does not do.&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="nd"&gt;@Composable&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;ExpensesRoute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ExpensesViewModel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;collectAsStateWithLifecycle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;ExpensesScreen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;onQueryChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;onQueryChange&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;onRefresh&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;onRefresh&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;onDelete&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;onDelete&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ExpensesScreen&lt;/code&gt; itself takes the state and 3 lambdas, so a preview or a UI test can call it without a ViewModel at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to send events to the ViewModel
&lt;/h2&gt;

&lt;p&gt;There are 2 common ways to send events up. One is a single &lt;code&gt;onEvent(event: ExpensesEvent)&lt;/code&gt; with a sealed class of events, the style MVI libraries such as Orbit and Mavericks use. The other is one plain function per action.&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="n"&gt;init&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;refresh&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;onQueryChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&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;savedState&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;KEY_QUERY&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&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;onRefresh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;refresh&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;onDelete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&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;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;refresh&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;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;launch&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="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LoadStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isLoading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&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="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;refresh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nc"&gt;LoadStatus&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;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;LoadStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;errorMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Couldn't refresh. Tap Refresh to retry."&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I recommend plain functions for most screens. "Find usages" works per action, the composable passes &lt;code&gt;viewModel::onRefresh&lt;/code&gt; directly, and you skip a &lt;code&gt;when&lt;/code&gt; that only dispatches. A single &lt;code&gt;onEvent&lt;/code&gt; starts to pay off when you log or replay every event, or when a screen has so many actions that the parameter list of the composable gets silly.&lt;/p&gt;

&lt;p&gt;Notice that a failed refresh only changes &lt;code&gt;status&lt;/code&gt;. The rows still come from the repository, so the user keeps seeing the last list with an error under the search field. A unit test confirms that the rows survive a failed refresh.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ExpensesViewModelTest &amp;gt; failedRefreshKeepsRowsAndShowsError STANDARD_OUT
    rows=4 isLoading=false error=Couldn't refresh. Tap Refresh to retry.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; expose &lt;code&gt;StateFlow&lt;/code&gt;, never &lt;code&gt;MutableStateFlow&lt;/code&gt;. If &lt;code&gt;uiState&lt;/code&gt; is mutable from outside, any composable can write &lt;code&gt;viewModel.uiState.value = ...&lt;/code&gt;, and the one-place-for-state-changes rule is gone. Keep the mutable flows &lt;code&gt;private&lt;/code&gt; and expose a read-only flow, built with &lt;code&gt;stateIn&lt;/code&gt; as above or with &lt;code&gt;asStateFlow()&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Where the data layer starts
&lt;/h2&gt;

&lt;p&gt;The data layer starts at the repository interface. Above it, the ViewModel knows &lt;code&gt;Expense&lt;/code&gt; and &lt;code&gt;Flow&lt;/code&gt;. Below it are DTOs, Retrofit services, Room entities and caching.&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="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;ExpenseRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;expenses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Flow&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Expense&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;refresh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DefaultExpenseRepository&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="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ExpenseApi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ExpenseRepository&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="n"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MutableStateFlow&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Expense&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;emptyList&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;override&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;expenses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Flow&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Expense&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;refresh&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;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchExpenses&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;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toModel&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="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filterNot&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;id&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="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;ExpenseDto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nc"&gt;Expense&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;note&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amountCents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toBigDecimal&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;movePointRight&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLong&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The API sends the amount as a string such as &lt;code&gt;"3.50"&lt;/code&gt; and calls the title &lt;code&gt;note&lt;/code&gt;. The mapping sits in one private function, so a renamed JSON field changes one line and no ViewModel. In a real app &lt;code&gt;ExpenseApi&lt;/code&gt; would be a Retrofit interface and the in-memory cache would be a Room table (Room gets its own post later in this series).&lt;/p&gt;

&lt;p&gt;The repository's shape also follows from the backend's API contract, which is where an Android app and its server have to agree. The &lt;a href="https://shippedcode.dev/system-design-for-app-developers/" rel="noopener noreferrer"&gt;system design post for app developers&lt;/a&gt; covers that side.&lt;/p&gt;

&lt;p&gt;Should there be a use-case layer between ViewModel and repository? For simple screens I call the repository directly. I add a use case when the same logic is shared by 2 or more ViewModels, or when it is complex enough that I want to test it on its own. A &lt;code&gt;GetExpensesUseCase&lt;/code&gt; that only forwards one call is an extra file to maintain, with no logic of its own.&lt;/p&gt;

&lt;p&gt;The ViewModel gets its repository through the constructor. Here &lt;code&gt;ExpensesApp&lt;/code&gt; builds it by hand and passes it to a &lt;code&gt;viewModelFactory&lt;/code&gt;. A DI framework like Hilt would generate that wiring, and Hilt gets its own post later in the series.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling one-off events
&lt;/h2&gt;

&lt;p&gt;Navigation after a save, or a snackbar, does not fit neatly into state, because it should happen once. The simplest approach that survives rotation is to keep the event in the state, for example a &lt;code&gt;snackbarMessage: String?&lt;/code&gt; field, and let the screen call something like &lt;code&gt;onSnackbarShown()&lt;/code&gt; after it displays it, so the ViewModel clears it. (&lt;code&gt;errorMessage&lt;/code&gt; above is different: it is meant to stay visible until the next refresh.) Channels and &lt;code&gt;SharedFlow&lt;/code&gt; are the other options, each with its own way of losing an event, and a later post compares them.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to split into feature modules (advanced)
&lt;/h2&gt;

&lt;p&gt;Everything above works in a single &lt;code&gt;app&lt;/code&gt; module. Modularization pays off in 2 cases: Gradle can then skip recompiling modules you didn't touch, or teams can own separate parts of the code with clear boundaries. If neither applies yet, the extra build files cost more than they give back. The &lt;a href="https://developer.android.com/topic/modularization" rel="noopener noreferrer"&gt;modularization guide&lt;/a&gt; lists the patterns in more depth, and the &lt;a href="https://github.com/android/nowinandroid" rel="noopener noreferrer"&gt;Now in Android&lt;/a&gt; sample shows them at a larger scale.&lt;/p&gt;

&lt;p&gt;The example uses 4 modules, and every arrow points down.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fshippedcode.dev%2F_astro%2Fmodule-graph.CW3vLbUh_nAiM.svg" 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%2Fshippedcode.dev%2F_astro%2Fmodule-graph.CW3vLbUh_nAiM.svg" alt="Module graph: :app depends on :feature:expenses and :core:data; :feature:expenses depends on :core:data; :core:data depends on :core:model" width="336" height="464"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;:core:model&lt;/code&gt; and &lt;code&gt;:core:data&lt;/code&gt; are plain Kotlin/JVM modules, not Android libraries, because nothing in them needs Android. That choice enforces the direction for you. When I added a dependency from &lt;code&gt;:core:data&lt;/code&gt; to &lt;code&gt;:feature:expenses&lt;/code&gt;, Gradle refused to build, though not with a friendly message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; Could not resolve project ':feature:expenses'.
     Required by:
         project ':core:data'
      &amp;gt; ... However we cannot choose between the following variants of project ':feature:expenses':
          - debugApiElements
          - releaseApiElements
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A JVM module cannot consume an Android library at all. A real cycle between 2 modules of the same kind gets a clearer message, as I saw when I made &lt;code&gt;:core:model&lt;/code&gt; depend on &lt;code&gt;:core:data&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* What went wrong:
Circular dependency between the following tasks:
:core:data:classes
\--- :core:data:compileJava
     +--- :core:data:compileKotlin
     |    \--- :core:model:jar
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A data layer that uses Room or &lt;code&gt;Context&lt;/code&gt; has to be an Android library, so you lose this guard there. In that case the dependency graph is the thing to review in pull requests.&lt;/p&gt;

&lt;p&gt;The other surprise is that &lt;code&gt;implementation&lt;/code&gt; dependencies are not passed on. The feature module declares &lt;code&gt;lifecycle-viewmodel-compose&lt;/code&gt;, yet &lt;code&gt;MainActivity&lt;/code&gt; in &lt;code&gt;:app&lt;/code&gt; failed to compile with &lt;code&gt;Unresolved reference 'viewModel'&lt;/code&gt; until &lt;code&gt;:app&lt;/code&gt; declared that library too. This is intended, since it keeps each module's classpath small. In practice it means &lt;code&gt;:app&lt;/code&gt; lists what it uses, or the feature module exposes an entry point that hides the ViewModel creation.&lt;/p&gt;

&lt;p&gt;For these versions, 2 details of the build setup are worth knowing. AGP 9 compiles Kotlin itself, so Android modules apply only &lt;code&gt;com.android.library&lt;/code&gt; (or &lt;code&gt;application&lt;/code&gt;) and the Compose compiler plugin, with no &lt;code&gt;kotlin-android&lt;/code&gt; plugin. And Compose BOM 2026.11.01 requires &lt;code&gt;compileSdk&lt;/code&gt; 37.1, which the build reports as a failed AAR metadata check. The minor API level is set like this.&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="n"&gt;android&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"dev.shippedcode.feature.expenses"&lt;/span&gt;
    &lt;span class="n"&gt;compileSdk&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;release&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;37&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;minorApiLevel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;defaultConfig&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;minSdk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;buildFeatures&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;compose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&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;h2&gt;
  
  
  Common traps
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Rotation is not process death.&lt;/strong&gt; I typed "co" into the search field on an API 37 emulator, rotated, then sent the app to the background and killed its process with &lt;code&gt;adb shell am kill&lt;/code&gt;. With the query in a plain &lt;code&gt;MutableStateFlow("")&lt;/code&gt;, rotation kept "co", but after the process came back the field was empty and all 4 rows were back. Moving the query into &lt;code&gt;SavedStateHandle&lt;/code&gt; (the version shown above) kept it through both.&lt;/p&gt;

&lt;p&gt;In the log below, the activity is recreated 3 times (rotate, rotate back, new process), but the ViewModel's &lt;code&gt;init&lt;/code&gt; runs again only in the new process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;6418 D ExpensesVM: Activity onCreate restored=false
6418 D ExpensesVM: init 2973980
6418 D ExpensesVM: Activity onCreate restored=true
6418 D ExpensesVM: Activity onCreate restored=true
6602 D ExpensesVM: Activity onCreate restored=true
6602 D ExpensesVM: init 244388133
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first column is the process id, and it changes only after the kill.&lt;/p&gt;

&lt;p&gt;Put only small, user-entered state in &lt;code&gt;SavedStateHandle&lt;/code&gt; (a query, a selected tab, an id). The list itself should be reloaded from the repository.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; &lt;code&gt;init { refresh() }&lt;/code&gt; runs once per ViewModel instance, which is what you want only if the instance is created by &lt;code&gt;viewModel()&lt;/code&gt; or a DI helper. When I created it with &lt;code&gt;remember { ExpensesViewModel(...) }&lt;/code&gt; instead, every rotation logged a new &lt;code&gt;init&lt;/code&gt;, refreshed again and lost the query. &lt;code&gt;remember&lt;/code&gt; does not survive a configuration change, and a ViewModel built that way is never cleared by the system.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Reading &lt;code&gt;uiState.value&lt;/code&gt; in a test without collecting it.&lt;/strong&gt; Because of &lt;code&gt;WhileSubscribed&lt;/code&gt;, the combine only runs while someone collects. A test that waited for the refresh and then read the value got the initial state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;no collector: rows=0 isLoading=true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start a collector first with &lt;code&gt;backgroundScope.launch { vm.uiState.collect {} }&lt;/code&gt;, then the same test sees 4 rows. Tests also need &lt;code&gt;Dispatchers.setMain(StandardTestDispatcher())&lt;/code&gt;, because &lt;code&gt;viewModelScope&lt;/code&gt; runs on the main dispatcher, which doesn't exist on the JVM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;android.util.Log&lt;/code&gt; in a ViewModel breaks JVM unit tests.&lt;/strong&gt; All 3 tests failed with &lt;code&gt;Method d in android.util.Log not mocked&lt;/code&gt; because of one &lt;code&gt;Log.d&lt;/code&gt; call in &lt;code&gt;init&lt;/code&gt; (the one that printed the &lt;code&gt;init&lt;/code&gt; lines in the log above). Setting &lt;code&gt;testOptions { unitTests.isReturnDefaultValues = true }&lt;/code&gt; makes them pass. Keeping Android calls out of the ViewModel works too, and is the better long-term fix.&lt;/p&gt;

&lt;p&gt;It is time to change the structure when a ViewModel imports Retrofit or Room types, or when the screen needs 3 booleans to know what to draw. The same goes for a fix in one feature that forces a rebuild of every module, and for 2 ViewModels that copy the same block of logic, which is the moment to add a use case. A long ViewModel on its own tells you less than any of these.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next step
&lt;/h2&gt;

&lt;p&gt;The screen in this post recomposes every time &lt;code&gt;uiState&lt;/code&gt; emits. The next post in the series looks at what Compose actually redraws when that happens, and how to keep a long list from doing more work than it should.&lt;/p&gt;

&lt;p&gt;Where this structure sits among other mobile decisions, including native versus Flutter, is the subject of the mobile pillar post.&lt;/p&gt;

&lt;p&gt;More posts in this area are under &lt;a href="https://shippedcode.dev/category/android-development/" rel="noopener noreferrer"&gt;Android development&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>programming</category>
    </item>
    <item>
      <title>System design for mobile developers: what matters</title>
      <dc:creator>duong vu</dc:creator>
      <pubDate>Thu, 26 Nov 2026 16:18:37 +0000</pubDate>
      <link>https://dev.to/duong_vu_849ad2e23c665cff/system-design-for-mobile-developers-what-matters-3ehl</link>
      <guid>https://dev.to/duong_vu_849ad2e23c665cff/system-design-for-mobile-developers-what-matters-3ehl</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://shippedcode.dev/system-design-for-app-developers/" rel="noopener noreferrer"&gt;Shipped Code&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most advice on system design for mobile developers starts with "design Twitter", which does not help much with a backend for a few thousand users. At that size, the decisions that make or break the product are the API contract with the app, safe retries and offline sync, a sane relational data model, and some basic caching, background jobs and monitoring. Sharding, multi-region and microservices can wait until measured load forces them, and one well-run server with a backed-up PostgreSQL goes a long way.&lt;/p&gt;

&lt;p&gt;As a concrete example of the shape, my own app &lt;a href="https://shippedcode.dev/portfolio/mockly/" rel="noopener noreferrer"&gt;Mockly&lt;/a&gt; has a Flutter client talking to a Flask backend and a MySQL database. Everything below applies to a backend like that.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do the arithmetic first. With 100 API calls per user per day, a 10k-DAU app averages about 12 requests per second and peaks near 60.&lt;/li&gt;
&lt;li&gt;Spend your design time on the API contract, idempotent writes and the data model. These are hard to change once apps are installed.&lt;/li&gt;
&lt;li&gt;Use the database you already have for job queues and deduplication before adding Redis or Kafka.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What you'll learn:&lt;/strong&gt; how to estimate load from your own numbers, which API, retry and data decisions to make before launch, and when sharding, Kafka or Kubernetes start to be worth it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The post assumes you have shipped an app or API. Each section after the estimate summarises a topic that gets its own post later; the "what can wait" table is near the end.&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture of a typical small app backend
&lt;/h2&gt;

&lt;p&gt;Before the details, here is the mobile app backend architecture most small products end up with, whatever the language.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fshippedcode.dev%2F_astro%2Fsmall-product.MQqh02ea_yjqyc.svg" 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%2Fshippedcode.dev%2F_astro%2Fsmall-product.MQqh02ea_yjqyc.svg" alt="Architecture of a small app product: the mobile app keeps a local database and a sync queue, sends HTTPS requests with an Idempotency-Key to one API service, which uses PostgreSQL for data and a jobs table, an optional Redis cache, and a background worker that sends push notifications and writes to object storage" width="521" height="672"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The app writes to its local database first and syncs through a queue, so a flaky network does not lose user actions. One API service owns all the business logic. PostgreSQL holds the data and also the queue of background jobs. A separate worker process sends push notifications, resizes images and talks to slow third parties, so API requests stay fast. Redis is dotted because it is optional; add it when a measurement says the database is the bottleneck for a specific read.&lt;/p&gt;

&lt;h2&gt;
  
  
  Estimate the load before you design
&lt;/h2&gt;

&lt;p&gt;The point of an estimate is to find out which order of magnitude you are in, so you stop designing for the wrong one. The numbers below are an &lt;strong&gt;example estimate for a 10k-DAU app&lt;/strong&gt;, with every assumption visible. Swap in your own and rerun it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;dau&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10_000&lt;/span&gt;             &lt;span class="c1"&gt;# daily active users&lt;/span&gt;
&lt;span class="n"&gt;requests_per_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;  &lt;span class="c1"&gt;# API calls per user per day (5 sessions x 20 calls)&lt;/span&gt;
&lt;span class="n"&gt;peak_factor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;          &lt;span class="c1"&gt;# busiest minute vs. the daily average&lt;/span&gt;
&lt;span class="n"&gt;rows_per_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;       &lt;span class="c1"&gt;# rows written per user per day&lt;/span&gt;
&lt;span class="n"&gt;bytes_per_row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1_000&lt;/span&gt;    &lt;span class="c1"&gt;# row + index overhead, rough&lt;/span&gt;
&lt;span class="n"&gt;response_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5_000&lt;/span&gt;   &lt;span class="c1"&gt;# average JSON response&lt;/span&gt;

&lt;span class="n"&gt;per_day&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dau&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;requests_per_user&lt;/span&gt;
&lt;span class="n"&gt;avg_rps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;per_day&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;86_400&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"requests/day : {per_day:,}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"average req/s: {avg_rps:.1f}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"peak req/s   : {avg_rps * peak_factor:.0f}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"DB growth/yr : {dau * rows_per_user * bytes_per_row * 365 / 1e9:.1f} GB"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"egress/month : {per_day * response_bytes * 30 / 1e9:.0f} GB"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;requests/day : 1,000,000
average req/s: 11.6
peak req/s   : 58
DB growth/yr : 36.5 GB
egress/month : 150 GB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A million requests a day sounds like a lot until you divide by 86,400 seconds. The peak factor is the shakiest assumption here. Daily traffic curves are usually gentle, but a push notification sent to every user at once can produce a much sharper spike than 5×, so look at your own request logs once you have them.&lt;/p&gt;

&lt;p&gt;Is 58 requests per second a lot for one PostgreSQL? You can get a rough feel with &lt;code&gt;pgbench&lt;/code&gt;, which ships with PostgreSQL. I ran its default read/write test against &lt;code&gt;postgres:17-alpine&lt;/code&gt; in Docker on a 10-core laptop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pgbench -U postgres -i -s 10 postgres
pgbench -U postgres -c 8 -j 4 -T 30 postgres
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;number of transactions actually processed: 288669
latency average = 0.831 ms
tps = 9622.001076 (without initial connection time)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That number flatters the database. The data fits in memory, there is no network hop, and the queries are trivial. Your production box with real queries will do far less. But you could be off by a factor of 100 and still be above 58. At this scale the database rarely runs out of capacity. Slow, unindexed queries and design mistakes in the API cause the trouble instead.&lt;/p&gt;

&lt;p&gt;Storage has the same kind of headroom: about 37 GB a year fits comfortably on one instance, and the &lt;a href="https://www.postgresql.org/docs/current/limits.html" rel="noopener noreferrer"&gt;PostgreSQL limits page&lt;/a&gt; puts the maximum table size at 32 TB. The egress figure is the one worth checking against your hosting plan, especially if images go through the API instead of object storage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Design the API contract carefully from day one
&lt;/h2&gt;

&lt;p&gt;Once an app version is in the stores, its API calls are frozen on every phone that does not update. You can redeploy the backend 10 times a day. You cannot redeploy the phones. That makes the contract between app and API the most expensive part of the system to get wrong. The decisions worth making on day one are these.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pagination.&lt;/strong&gt; Use cursor pagination (&lt;code&gt;?after=&amp;lt;cursor&amp;gt;&lt;/code&gt;, built from a sortable key such as &lt;code&gt;created_at&lt;/code&gt; plus &lt;code&gt;id&lt;/code&gt;) for feeds and lists. Offset pagination shows duplicates or skips items when new rows arrive while the user scrolls, which on a phone happens all the time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Versioning.&lt;/strong&gt; Prefer additive changes. New fields are fine; renaming or removing a field that an old app reads is a breaking change. Have the app send its version in a header, and decide early how you will tell a too-old app to update (a minimum supported version and a force-update screen).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Errors.&lt;/strong&gt; Return a stable, machine-readable error format, so the app can branch on a code instead of parsing a message. &lt;a href="https://datatracker.ietf.org/doc/html/rfc9457" rel="noopener noreferrer"&gt;RFC 9457 (Problem Details)&lt;/a&gt; is a good default, and Spring Boot 3 supports it through &lt;code&gt;ProblemDetail&lt;/code&gt;:&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="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"https://api.example.com/errors/plan-limit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Plan limit reached"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;403&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"code"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"PLAN_LIMIT"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next post in this series, on API design for mobile clients, works through all 3 in detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Retries, idempotency and offline sync
&lt;/h2&gt;

&lt;p&gt;Mobile networks drop responses, not just requests. The app sends "place order", the server saves it, the response is lost in a tunnel, and the app retries. Without protection, you now have 2 orders. So duplicates have to be handled in the API design; the client cannot prevent them.&lt;/p&gt;

&lt;p&gt;The standard fix is an &lt;strong&gt;idempotency key&lt;/strong&gt;. The app generates a UUID when the user taps the button, sends it with every retry of that action, and the server stores it with a unique constraint. There is an &lt;a href="https://datatracker.ietf.org/doc/draft-ietf-httpapi-idempotency-key-header/" rel="noopener noreferrer"&gt;IETF draft for an &lt;code&gt;Idempotency-Key&lt;/code&gt; header&lt;/a&gt; that describes the convention. In the database it can be as small as this (PostgreSQL 17):&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;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;              &lt;span class="kt"&gt;bigserial&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;user_id&lt;/span&gt;         &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;idempotency_key&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt;   &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;total_cents&lt;/span&gt;     &lt;span class="kt"&gt;int&lt;/span&gt;    &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;UNIQUE&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;idempotency_key&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;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders&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;idempotency_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total_cents&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'6f1c2a9e-3b7d-4e55-9a0c-2d8f1b4e7a10'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1999&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;CONFLICT&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;idempotency_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DO&lt;/span&gt; &lt;span class="k"&gt;NOTHING&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running the same &lt;code&gt;INSERT&lt;/code&gt; twice shows what a retry 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;-- first request
 id
----
  1
INSERT 0 1

-- retry with the same key
 id
----
(0 rows)
INSERT 0 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the insert returns no row, the API looks up the existing order by key and returns it, so the app gets the same answer both times. A real implementation also stores a hash of the request body and rejects a reused key with a different body. The unique constraint does the heavy lifting under concurrency too, since 2 simultaneous retries cannot both insert.&lt;/p&gt;

&lt;p&gt;Offline sync is the same idea, scaled up. The app records each change in a local queue with its own UUID and replays the queue when the network returns, and the server deduplicates by that UUID. Conflicts appear when 2 devices edit the same record. For data that belongs to one user, last-write-wins is usually enough; keep a server-side version number so you can detect the rare stale write. Shared data needs a deliberate strategy. Either way, compare server versions, not device timestamps, because phone clocks drift.&lt;/p&gt;

&lt;p&gt;If you want the deeper version, conflict strategies and idempotency keys each have a post of their own in this series.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start with a relational data model
&lt;/h2&gt;

&lt;p&gt;The database pillar and a later post on schema design for mobile backends cover this in depth; the short version is to start relational. Most app data is users, things they own, and relations between them, and a relational database enforces the rules you would otherwise have to remember in code. The idempotency example above works only because of a unique constraint. Foreign keys, &lt;code&gt;NOT NULL&lt;/code&gt; and check constraints catch bugs from every client version at once.&lt;/p&gt;

&lt;p&gt;A few habits pay off early. Generate IDs on the client (UUIDs) for anything created offline, so the app can reference a record before the server has seen it. Keep schema changes in versioned migrations (Flyway or Liquibase on the JVM, Alembic with Flask). Denormalise on purpose and only after measuring. A &lt;code&gt;comment_count&lt;/code&gt; column is a fine optimisation once counting comments shows up in a slow query log, not before.&lt;/p&gt;

&lt;h2&gt;
  
  
  Caching, background jobs and push notifications
&lt;/h2&gt;

&lt;p&gt;Cache reads that are expensive and change rarely, such as a product catalogue or app configuration. In Spring Boot, &lt;code&gt;@Cacheable&lt;/code&gt; with an in-process Caffeine cache is often enough for a single server, and you avoid running Redis at all. Move to Redis when you run more than one API instance and need a shared cache, or when you need it for rate limiting. The hard part of caching is invalidation, so start with short TTLs on data where slightly stale is acceptable.&lt;/p&gt;

&lt;p&gt;Anything slow or unreliable belongs outside the request. That covers emails, push, image processing and third-party calls. You do not need Kafka or RabbitMQ for a few thousand jobs a day. A jobs table in PostgreSQL with &lt;code&gt;FOR UPDATE SKIP LOCKED&lt;/code&gt; lets several workers pull jobs without taking the same one:&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="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;jobs&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;done_at&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;failed_at&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;run_after&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;run_after&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="no"&gt;SKIP&lt;/span&gt; &lt;span class="no"&gt;LOCKED&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I tested it with 2 workers. Worker A claimed job 1 and held its transaction open for 3 seconds; worker B, starting a second later, got job 2 immediately instead of waiting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- worker A
 id |   kind
----+-----------
  1 | send_push

-- worker B, while A still holds job 1
 id |     kind
----+---------------
  2 | resize_avatar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Spring, a &lt;code&gt;@Scheduled&lt;/code&gt; method that runs this query, does the work and sets &lt;code&gt;done_at&lt;/code&gt; in the same transaction is enough for a first worker.&lt;/p&gt;

&lt;p&gt;For failures, add &lt;code&gt;attempts&lt;/code&gt; and &lt;code&gt;failed_at&lt;/code&gt; columns. When the work fails, catch the error, roll back the work, then increment &lt;code&gt;attempts&lt;/code&gt; and push &lt;code&gt;run_after&lt;/code&gt; forward in a separate transaction that commits. Once &lt;code&gt;attempts&lt;/code&gt; reaches a limit, set &lt;code&gt;failed_at&lt;/code&gt; instead; the &lt;code&gt;failed_at IS NULL&lt;/code&gt; filter in the query parks the job, so it stops blocking the queue and waits for someone to look at it.&lt;/p&gt;

&lt;p&gt;A side benefit of a jobs table is that the job is inserted in the same transaction as the business data, so you never get an order without its confirmation job. MySQL 8 supports &lt;code&gt;SKIP LOCKED&lt;/code&gt; as well.&lt;/p&gt;

&lt;p&gt;Push notifications are best-effort. Send them from the worker, store device tokens per user and device, and delete tokens that FCM or APNs report as invalid. Never make push the only way the app learns about a change; the app should still fetch fresh data when it opens.&lt;/p&gt;

&lt;p&gt;If you are on the JVM, the &lt;a href="https://shippedcode.dev/java-backend-roadmap/" rel="noopener noreferrer"&gt;Java backend roadmap&lt;/a&gt; covers where these Spring pieces fit in the bigger picture.&lt;/p&gt;

&lt;h2&gt;
  
  
  Observability and cost
&lt;/h2&gt;

&lt;p&gt;You need to know something broke before your users email you. The minimum that covers most incidents at this scale is short:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;structured logs with a request ID, user ID, and the &lt;strong&gt;app version and platform&lt;/strong&gt; on every line, so you can see that a bug only affects app version 2.3.1 on Android;&lt;/li&gt;
&lt;li&gt;metrics for request rate, error rate, p95 latency, database connections and the age of the oldest pending job;&lt;/li&gt;
&lt;li&gt;alerts on 5xx rate and job queue age, sent somewhere you will actually see them;&lt;/li&gt;
&lt;li&gt;backups that you have restored at least once, because you only learn whether a backup works when you restore it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cost follows from the estimate. The example above fits on one mid-size VPS plus either a managed PostgreSQL or a self-hosted one with automated backups. Check current prices with your provider rather than trusting a number from a blog post, and check the egress allowance in particular. Serving images straight from object storage or a CDN usually moves most of the bandwidth off the API server.&lt;/p&gt;

&lt;p&gt;Deployment, CI and running that one server are covered in the &lt;a href="https://shippedcode.dev/devops-for-app-developers/" rel="noopener noreferrer"&gt;DevOps pillar&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What can wait until later: sharding, Kafka, Kubernetes
&lt;/h2&gt;

&lt;p&gt;These topics dominate system design content. Each of them solves a problem you will see in your metrics before it hurts, so you can adopt it when that happens. Use the table to decide when one starts to matter.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Topic&lt;/th&gt;
&lt;th&gt;Matters when&lt;/th&gt;
&lt;th&gt;Main trade-off&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Sharding&lt;/td&gt;
&lt;td&gt;One primary cannot keep up with writes after indexing, tuning and a bigger instance&lt;/td&gt;
&lt;td&gt;Cross-shard queries and transactions get hard; every query must know the shard key&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Microservices&lt;/td&gt;
&lt;td&gt;Several teams need to deploy independently&lt;/td&gt;
&lt;td&gt;Network calls replace function calls; distributed debugging and data consistency&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multi-region&lt;/td&gt;
&lt;td&gt;Users on other continents need low latency, or an outage of one region is unacceptable&lt;/td&gt;
&lt;td&gt;Data replication, conflict handling and a much bigger bill&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Kafka&lt;/td&gt;
&lt;td&gt;You need durable event streams with many consumers or replay&lt;/td&gt;
&lt;td&gt;Another cluster to run; a jobs table covers simple background work&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Kubernetes&lt;/td&gt;
&lt;td&gt;You run many services and need scheduling, scaling and self-healing across machines&lt;/td&gt;
&lt;td&gt;A platform to learn and operate; overkill for one or two containers&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Service meshes, event sourcing and CQRS belong in the same bucket. They are useful patterns for specific problems, but you are unlikely to meet those problems at the load in the example estimate.&lt;/p&gt;

&lt;h2&gt;
  
  
  If I started a new backend today
&lt;/h2&gt;

&lt;p&gt;The usual advice is to "design for scale" from the start. For a new product backend I would do the opposite in most places. I would start with one relational database and a monolith, on a stack like Mockly's Flask + MySQL. I would add a queue or a cache only when a measurement shows a need for it. The exception is the API contract, which I would design for old app versions from the first release, with versioning and additive changes, because that is the one decision the backend cannot fix on its own later.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; The costly mistakes at this scale are rarely about load. Duplicate requests from retries, old app versions still calling endpoints you changed, and sync conflicts between 2 devices are the common ones, and each is much cheaper to design for before launch than to clean up afterwards.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;In this series:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;API design for mobile clients: pagination, versioning, errors&lt;/li&gt;
&lt;li&gt;Offline sync design: conflict resolution strategies that work&lt;/li&gt;
&lt;li&gt;Idempotency keys: making retries safe for payments and orders&lt;/li&gt;
&lt;li&gt;System design interview resources worth paying for (and the free ones)&lt;/li&gt;
&lt;li&gt;Case study: the backend behind a voice translator app&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The next step is the API contract, since it is the hardest thing to change later. Published system design posts collect in the &lt;a href="https://shippedcode.dev/category/system-design/" rel="noopener noreferrer"&gt;system design category&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>architecture</category>
      <category>postgres</category>
      <category>database</category>
    </item>
    <item>
      <title>Java backend roadmap: core Java to production Spring Boot</title>
      <dc:creator>duong vu</dc:creator>
      <pubDate>Thu, 26 Nov 2026 16:14:08 +0000</pubDate>
      <link>https://dev.to/duong_vu_849ad2e23c665cff/java-backend-roadmap-core-java-to-production-spring-boot-5d8j</link>
      <guid>https://dev.to/duong_vu_849ad2e23c665cff/java-backend-roadmap-core-java-to-production-spring-boot-5d8j</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://shippedcode.dev/java-backend-roadmap/" rel="noopener noreferrer"&gt;Shipped Code&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Many Java backend roadmaps list dozens of tools and leave you to guess the order. This one has 6 stages, and each stage ends with a test you can run on yourself before moving on.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR.&lt;/strong&gt; Learn modern core Java first, then a build tool, then a Spring Boot REST API with validation and consistent errors. After that come persistence (JPA, transactions, Flyway), then security and integration tests, and only then production concerns like Docker, metrics and caching. Skip application servers, XML config, microservices and Kubernetes until one well-tested Spring Boot service runs in production. For a new project in late 2026, that means Spring Boot 4.x on Java 21 or 25.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The 6 stages, in order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Modern core Java&lt;/strong&gt;: collections, streams, records, sealed types, enough concurrency not to fear it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build tool&lt;/strong&gt;: Maven or Gradle, well enough to build and test from the command line.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A Spring Boot REST API&lt;/strong&gt;: controllers, validation, ProblemDetail errors, configuration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Persistence&lt;/strong&gt;: JPA, the N+1 problem, &lt;code&gt;@Transactional&lt;/code&gt;, Flyway migrations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security and tests&lt;/strong&gt;: JWT for a mobile client, integration tests on a real database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Production&lt;/strong&gt;: Docker, health checks, metrics, structured logs, caching.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This roadmap is for you if you already know basic Java or Kotlin syntax, including Android developers who want to learn Spring Boot after Java or Kotlin to write their app's backend. If you are short on time, read the stage list above and the section What to skip for now. Each stage section says what "done" looks like and which post on this site goes deeper.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fshippedcode.dev%2F_astro%2Froadmap.yhmOcZnZ_Z1yjpIG.svg" 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%2Fshippedcode.dev%2F_astro%2Froadmap.yhmOcZnZ_Z1yjpIG.svg" alt="Java backend roadmap as a top-to-bottom path: modern core Java, build tool, Spring Boot REST API, persistence, security and tests, production. Persistence links to the database pillar and production links to the DevOps pillar." width="434" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Which versions to learn
&lt;/h2&gt;

&lt;p&gt;Learn on Spring Boot 4.x and Java 21 or 25. The Spring Boot 3.x line is out of open-source support. According to the &lt;a href="https://spring.io/projects/spring-boot#support" rel="noopener noreferrer"&gt;Spring Boot support table&lt;/a&gt;, open-source support for 3.5 ended on 30 June 2026, and only paid commercial support continues. &lt;a href="https://start.spring.io/" rel="noopener noreferrer"&gt;start.spring.io&lt;/a&gt; now generates 4.0, 4.1 and 4.2 projects only. Spring Boot 4.2.0 is the current release, and its &lt;a href="https://docs.spring.io/spring-boot/system-requirements.html" rel="noopener noreferrer"&gt;system requirements&lt;/a&gt; say it needs Java 17 or later and works up to Java 27.&lt;/p&gt;

&lt;p&gt;On the Java side, the &lt;a href="https://www.oracle.com/java/technologies/java-se-support-roadmap.html" rel="noopener noreferrer"&gt;Oracle support roadmap&lt;/a&gt; lists Java 21 and Java 25 as the current LTS releases, with premier support until September 2028 and September 2030. Pick Java 25 for a new project unless your hosting platform or company standard pins you to 21. Nothing in this roadmap depends on the difference. The next LTS, Java 29, is planned for September 2027. Move from one LTS to the next once your Spring Boot version supports it, and skip the 6-month releases in between.&lt;/p&gt;

&lt;p&gt;You will still find many tutorials written for Spring Boot 3, and some earlier code on this site runs on Spring Boot 3.5 and Java 21. The concepts carry over. What changes most often is setup detail. A new 4.x project, for example, uses &lt;code&gt;spring-boot-starter-webmvc&lt;/code&gt; where 3.x tutorials say &lt;code&gt;spring-boot-starter-web&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage 1: Modern core Java
&lt;/h2&gt;

&lt;p&gt;Backend Java code today is mostly collections, streams and small immutable data types, so that is where to spend your time. You need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;List&lt;/code&gt;, &lt;code&gt;Set&lt;/code&gt; and &lt;code&gt;Map&lt;/code&gt;, and which implementation to pick (&lt;code&gt;ArrayList&lt;/code&gt;, &lt;code&gt;HashSet&lt;/code&gt;, &lt;code&gt;HashMap&lt;/code&gt;, &lt;code&gt;LinkedHashMap&lt;/code&gt;, &lt;code&gt;TreeMap&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;streams and &lt;code&gt;Optional&lt;/code&gt;, including when a plain loop reads better;&lt;/li&gt;
&lt;li&gt;records, sealed interfaces and pattern matching in &lt;code&gt;switch&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;enough concurrency to know what a thread pool is, why shared mutable state is dangerous, and what &lt;code&gt;CompletableFuture&lt;/code&gt; is for.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can skip GUI libraries (Swing, JavaFX), applets, and the details of the class loader and garbage collector for now.&lt;/p&gt;

&lt;p&gt;Here is a quick self-check. The file below runs as-is with &lt;code&gt;java Stage1Check.java&lt;/code&gt; on Java 21 or later, with no build tool needed. If you can read it without looking anything up, and predict the output, stage 1 is done.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;util&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;util&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Collectors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Stage1Check&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Payment&lt;/span&gt; &lt;span class="n"&gt;permits&lt;/span&gt; &lt;span class="nc"&gt;Card&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Wallet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Failed&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;userId&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="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Card&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;cents&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Payment&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Wallet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;cents&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Payment&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Failed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Payment&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Payment&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Card&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100_000&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"large card payment"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Card&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"card"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Wallet&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"wallet"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Failed&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"failed: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reason&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="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;static&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nf"&gt;cents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Payment&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Card&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cents&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;case&lt;/span&gt; &lt;span class="nc"&gt;Wallet&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cents&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;case&lt;/span&gt; &lt;span class="nc"&gt;Failed&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&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="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&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;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Payment&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;payments&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Card&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"u1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2_500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Wallet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"u2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;900&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Card&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"u1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;150_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Failed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"u3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"card declined"&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;payments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&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="p"&gt;;&lt;/span&gt;

        &lt;span class="kt"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;centsByUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stream&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;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;Failed&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;collect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;groupingBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Payment&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TreeMap&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;summingLong&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Stage1Check&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cents&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="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="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;centsByUser&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;card
wallet
large card payment
failed: card declined
{u1=152500, u2=900}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The check comes with 2 questions. Why does the map print &lt;code&gt;u1&lt;/code&gt; before &lt;code&gt;u2&lt;/code&gt; every time? (&lt;code&gt;TreeMap&lt;/code&gt; keeps its keys sorted; a &lt;code&gt;HashMap&lt;/code&gt; makes no promise about order.) And what happens if you delete the line &lt;code&gt;case Failed f -&amp;gt; 0;&lt;/code&gt;? I tried it, and the file no longer compiles:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Stage1Check.java:20: error: the switch expression does not cover all possible input values
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That error is the reason sealed types are worth learning. When someone adds a new kind of payment, the compiler lists every &lt;code&gt;switch&lt;/code&gt; that has to handle it.&lt;/p&gt;

&lt;p&gt;You can leave this stage when you can pick a collection for a task and explain why. You should also be able to read stream and &lt;code&gt;switch&lt;/code&gt; code like the file above without help.&lt;/p&gt;

&lt;p&gt;In the "Modern Java foundations" series:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Java collections for backend developers: choosing List, Set and Map&lt;/li&gt;
&lt;li&gt;Java Stream API and Optional: patterns to keep and traps to avoid&lt;/li&gt;
&lt;li&gt;Records, sealed interfaces and pattern matching in Java 21+ backend code&lt;/li&gt;
&lt;li&gt;Java books I would still buy after 10 years of backend work&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Stage 2: Build tool
&lt;/h2&gt;

&lt;p&gt;You need one build tool well enough to add a dependency, run the tests and build a runnable jar from the command line. If you come from Android, you already know Gradle, so keep using it. Everyone else should start with Maven, because a &lt;code&gt;pom.xml&lt;/code&gt; is declarative and offers fewer ways to do the same thing. You are done with this stage when you can explain where a dependency's version comes from (the Spring Boot parent or BOM). You should also be able to run &lt;code&gt;./mvnw verify&lt;/code&gt; or &lt;code&gt;./gradlew build&lt;/code&gt; without an IDE.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage 3: A Spring Boot REST API
&lt;/h2&gt;

&lt;p&gt;The Spring Boot part of the roadmap starts here, and this is the stage where you build something a client app can call. Write a small CRUD API and learn these pieces on the way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@RestController&lt;/code&gt;, request mapping, and separate request/response DTOs instead of exposing entities;&lt;/li&gt;
&lt;li&gt;Bean Validation (&lt;code&gt;@Valid&lt;/code&gt;, &lt;code&gt;@NotBlank&lt;/code&gt;, &lt;code&gt;@Size&lt;/code&gt;) on request bodies;&lt;/li&gt;
&lt;li&gt;errors in one shape, using &lt;code&gt;ProblemDetail&lt;/code&gt; (the RFC 9457 format Spring supports out of the box);&lt;/li&gt;
&lt;li&gt;configuration from &lt;code&gt;application.yaml&lt;/code&gt;, environment variables and &lt;code&gt;@ConfigurationProperties&lt;/code&gt;, with profiles for local and production.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You are done when invalid input returns a 400 with the same JSON shape on every endpoint, and no response ever contains a stack trace. The database URL and secrets come from the environment.&lt;/p&gt;

&lt;p&gt;Posts in this stage:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Consistent API errors in Spring Boot with ProblemDetail and Bean Validation&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Stage 4: Persistence
&lt;/h2&gt;

&lt;p&gt;Many of the bugs that only appear with real data start in this stage. Learn JPA entities and Spring Data repositories, then the parts that bite:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;lazy loading and the N+1 query problem, where one list endpoint quietly sends one SQL query per row;&lt;/li&gt;
&lt;li&gt;where a transaction starts and ends, and why &lt;code&gt;@Transactional&lt;/code&gt; is ignored when another method in the same class calls it;&lt;/li&gt;
&lt;li&gt;schema changes as versioned Flyway migrations, with Hibernate set to &lt;code&gt;ddl-auto: validate&lt;/code&gt; so it checks the schema and never changes it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You are done when you can predict the SQL a repository call sends, and then confirm it by turning on SQL logging.&lt;/p&gt;

&lt;p&gt;The SQL and indexing you need day to day belong to the database pillar.&lt;/p&gt;

&lt;p&gt;Posts in this stage:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring Data JPA N+1 queries: find them, fix them, test them&lt;/li&gt;
&lt;li&gt;@Transactional pitfalls: self-invocation, rollback rules, propagation&lt;/li&gt;
&lt;li&gt;Zero-downtime schema migrations with Flyway&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Stage 5: Security and tests
&lt;/h2&gt;

&lt;p&gt;A backend for a mobile app usually authenticates with tokens. In practice, you need Spring Security's filter chain, hashed passwords and a short-lived access token, often a JWT. A refresh token lets the app get a new access token without asking the user to log in again. The server can trust a token's claims only after it has checked the signature and the expiry.&lt;/p&gt;

&lt;p&gt;For tests, learn the difference between slice tests (&lt;code&gt;@WebMvcTest&lt;/code&gt;, &lt;code&gt;@DataJpaTest&lt;/code&gt;) and full &lt;code&gt;@SpringBootTest&lt;/code&gt; runs. Then run your integration tests against the same database you use in production, started by Testcontainers. An in-memory H2 database accepts SQL that PostgreSQL rejects, and the reverse, so green H2 tests prove less than they seem to.&lt;/p&gt;

&lt;p&gt;Before moving on, make sure CI runs at least one integration test per endpoint against a real database. The tests should check the 401 and 403 cases as well as the happy path.&lt;/p&gt;

&lt;p&gt;Posts in this stage:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JWT authentication for a mobile app backend with Spring Security&lt;/li&gt;
&lt;li&gt;Integration tests with Testcontainers in Spring Boot&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Stage 6: Production
&lt;/h2&gt;

&lt;p&gt;Production work starts once the service is correct and tested. Package it as a Docker image, then make it observable with Actuator health checks, Micrometer metrics and structured logs. Add caching only where you have measured a slow, repeated read, because every cache brings an invalidation problem with it.&lt;/p&gt;

&lt;p&gt;Virtual threads also belong here. Spring Boot turns them on with one property, &lt;code&gt;spring.threads.virtual.enabled=true&lt;/code&gt;, and they mainly help services that spend most of their time waiting on I/O. Learn them after the basics, and measure before and after.&lt;/p&gt;

&lt;p&gt;If you need to run several remote calls in parallel and combine the results, read CompletableFuture in practice: composing async calls without callback hell.&lt;/p&gt;

&lt;p&gt;Deployment, CI/CD and servers get their own pillar, &lt;a href="https://shippedcode.dev/devops-for-app-developers/" rel="noopener noreferrer"&gt;DevOps for app developers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Posts in this stage:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Caching in Spring Boot with Redis: what to cache and when to evict&lt;/li&gt;
&lt;li&gt;Observability for Spring Boot: Actuator, Micrometer, Prometheus&lt;/li&gt;
&lt;li&gt;Virtual threads in Spring Boot: a benchmark and when they help&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What to skip for now
&lt;/h2&gt;

&lt;p&gt;Each of these costs weeks, and each makes more sense after you have shipped one service.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Application servers and Jakarta EE deployment (WAR files on WildFly or WebLogic) can wait, because Spring Boot embeds Tomcat and runs as a plain jar. That is also why this roadmap uses Spring Boot rather than Jakarta EE, Quarkus or Micronaut: it is the most widely used Java backend framework, so help is easy to find, and the concepts transfer if you switch later.&lt;/li&gt;
&lt;li&gt;XML bean configuration is something you will mostly meet in old codebases, and you can read it when that day comes.&lt;/li&gt;
&lt;li&gt;Writing custom auto-configuration or studying how AOP proxies are built can wait. Knowing that &lt;code&gt;@Transactional&lt;/code&gt; works through a proxy is enough for stage 4.&lt;/li&gt;
&lt;li&gt;Reactive programming with WebFlux changes how you write every layer, and virtual threads now cover much of the "many concurrent waiting requests" case. Learn it when a project needs it.&lt;/li&gt;
&lt;li&gt;Microservices, Spring Cloud, Kafka and Kubernetes solve problems of many teams and many services. One service with one database does not have those problems yet.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; The most expensive mistake on this roadmap is starting with microservices. You pay for network calls, distributed transactions and deployment of several services before you can build and test one. Splitting a well-structured monolith later is ordinary refactoring work; merging services back is rarely done.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Configuring the service to run well in production
&lt;/h2&gt;

&lt;p&gt;When a service that works on a laptop fails its first production deployment, the cause is often one of a few things. Configuration or secrets are baked into the jar. Hibernate changes the schema on its own. The connection pool doesn't match what the database allows. Nobody can tell whether the service is healthy, and its logs can't be searched.&lt;/p&gt;

&lt;p&gt;The configuration below covers those points for a Spring Boot 4.2 service with PostgreSQL. It assumes the starters for web MVC, data JPA, Flyway and Actuator, plus &lt;code&gt;micrometer-registry-prometheus&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;spring&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;datasource&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DB_URL}&lt;/span&gt;
    &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DB_USER}&lt;/span&gt;
    &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DB_PASSWORD}&lt;/span&gt;
    &lt;span class="na"&gt;hikari&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;maximum-pool-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DB_POOL_SIZE:10}&lt;/span&gt;
  &lt;span class="na"&gt;jpa&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;hibernate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;ddl-auto&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;validate&lt;/span&gt;
    &lt;span class="na"&gt;open-in-view&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;

&lt;span class="na"&gt;management&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;endpoints&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;web&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;exposure&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;include&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;health,info,prometheus&lt;/span&gt;
  &lt;span class="na"&gt;endpoint&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;health&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;probes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;enabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

&lt;span class="na"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;structured&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;console&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ecs&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each block answers one of those failures. The datasource reads its URL and credentials from environment variables, so the same jar runs in every environment. &lt;code&gt;ddl-auto: validate&lt;/code&gt; makes Hibernate check the schema that Flyway created instead of changing it. &lt;code&gt;open-in-view: false&lt;/code&gt; closes the persistence context when the service method returns, so lazy loading in a controller fails loudly instead of sending hidden queries. The health probes and the Prometheus endpoint tell a load balancer or a monitoring system whether the service is alive, and the &lt;code&gt;ecs&lt;/code&gt; format writes each log line as JSON that a log tool can search.&lt;/p&gt;

&lt;p&gt;With a first migration in &lt;code&gt;src/main/resources/db/migration/V1__create_note.sql&lt;/code&gt;, the service starts, applies the migration and answers on the health endpoint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Successfully applied 1 migration to schema "public", now at version v1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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="nl"&gt;"groups"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"liveness"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"readiness"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"UP"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This setup has 3 details that can cost you an afternoon, and I ran into each of them on Spring Boot 4.2.0.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Listing &lt;code&gt;prometheus&lt;/code&gt; in &lt;code&gt;exposure.include&lt;/code&gt; is not enough. Without the &lt;code&gt;micrometer-registry-prometheus&lt;/code&gt; dependency, &lt;code&gt;/actuator/prometheus&lt;/code&gt; returns 404 and the startup log says it exposes only 2 endpoints. With the dependency it returns metrics, including &lt;code&gt;hikaricp_connections_max{pool="HikariPool-1"} 10.0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In Spring Boot 4, &lt;code&gt;flyway-core&lt;/code&gt; alone does not run migrations. I replaced &lt;code&gt;spring-boot-starter-flyway&lt;/code&gt; with &lt;code&gt;org.flywaydb:flyway-core&lt;/code&gt;, as many 3.x tutorials show. Nothing migrated, and &lt;code&gt;ddl-auto: validate&lt;/code&gt; stopped the start with &lt;code&gt;Schema validation: missing table [note]&lt;/code&gt;. That failure is the reason to keep &lt;code&gt;validate&lt;/code&gt; on, because it catches the problem at startup and not on the first request.&lt;/li&gt;
&lt;li&gt;A missing environment variable does not fail with a clear message. Without &lt;code&gt;DB_PASSWORD&lt;/code&gt;, Spring did not complain about the placeholder. The app started, Flyway tried to connect, and the error was &lt;code&gt;password authentication failed for user&lt;/code&gt;. When that error shows up in a new environment, check the variables before the database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The pool size of 10 is HikariCP's default, written out so you can change it per environment. The number that matters is the total. Multiply the pool size by the number of service instances, and keep the result below the database's connection limit, with room left for migrations and admin tools.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which post to read first
&lt;/h2&gt;

&lt;p&gt;If you are starting stage 1 now, the next post to read is Java collections for backend developers: choosing List, Set and Map.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Series on this site.&lt;/strong&gt; "Modern Java foundations" (4 posts) covers stage 1. "Spring Boot in production" (10 posts) covers stages 3 to 6 (N+1 queries, &lt;code&gt;@Transactional&lt;/code&gt; pitfalls, ProblemDetail errors, JWT for mobile apps, Testcontainers, Redis caching, observability, virtual threads), plus IDE setup and a learning path. Posts are added weekly; published ones appear under &lt;a href="https://shippedcode.dev/category/java/" rel="noopener noreferrer"&gt;Java&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>java</category>
      <category>springboot</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
