<?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: Hưng Trần Minh</title>
    <description>The latest articles on DEV Community by Hưng Trần Minh (@hungga1711).</description>
    <link>https://dev.to/hungga1711</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%2F3458425%2F2ddca742-5b42-428c-853f-babe1cb6a6b5.jpg</url>
      <title>DEV Community: Hưng Trần Minh</title>
      <link>https://dev.to/hungga1711</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hungga1711"/>
    <language>en</language>
    <item>
      <title>I built a work app on Kafka, Debezium, Keycloak and nine services. Then I deleted them one by one.</title>
      <dc:creator>Hưng Trần Minh</dc:creator>
      <pubDate>Thu, 03 Dec 2026 14:32:10 +0000</pubDate>
      <link>https://dev.to/hungga1711/i-built-a-work-app-on-kafka-debezium-keycloak-and-nine-services-then-i-deleted-them-one-by-one-1mb1</link>
      <guid>https://dev.to/hungga1711/i-built-a-work-app-on-kafka-debezium-keycloak-and-nine-services-then-i-deleted-them-one-by-one-1mb1</guid>
      <description>&lt;p&gt;&lt;a href="https://github.com/hungga1711/workspace" rel="noopener noreferrer"&gt;Workspace&lt;/a&gt; is a self-hosted work app: tasks, chat, meetings and docs in one install. One installation serves one company, and it has to run on a small server with &lt;strong&gt;2 CPU cores and 2 GB of RAM&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The first version did not fit on 2 GB. The code started out as a multi-tenant SaaS design, and the architecture showed it: &lt;strong&gt;nine Node processes&lt;/strong&gt;, &lt;strong&gt;eight Postgres databases&lt;/strong&gt;, row-level security on every table, an outbox read by &lt;strong&gt;Debezium&lt;/strong&gt; into &lt;strong&gt;Kafka&lt;/strong&gt;, &lt;strong&gt;Keycloak&lt;/strong&gt; for sign-in and &lt;strong&gt;OpenTelemetry&lt;/strong&gt;. Kafka and Kafka Connect alone took about 1.1 GB of RAM, Keycloak 0.6–0.8 GB, and the nine Node processes another 1.1 GB.&lt;/p&gt;

&lt;p&gt;Today a fresh install runs &lt;strong&gt;three containers&lt;/strong&gt; (Postgres, the app, Caddy) and idles at &lt;strong&gt;about 300 MB&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I wrote down every step as an Architecture Decision Record. There are &lt;a href="https://github.com/hungga1711/workspace/tree/main/docs/adr" rel="noopener noreferrer"&gt;27 of them in the repo&lt;/a&gt;, including the ones that were later reversed. Here is what each removal taught me.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Kafka and Debezium: the outbox was the valuable part, not the broker
&lt;/h2&gt;

&lt;p&gt;Services learned about each other's changes through a transactional outbox: the event row is written in the same transaction as the business change, so you never publish an event for a rolled-back write and never lose one when the process crashes after commit.&lt;/p&gt;

&lt;p&gt;The first version had a neat trick. The service inserted the outbox row &lt;strong&gt;and deleted it in the same transaction&lt;/strong&gt;, so the row only existed in the WAL. Debezium read it through logical replication and published it to Kafka.&lt;/p&gt;

&lt;p&gt;For one company on one small server, that was the heaviest part of the stack: two JVMs, &lt;code&gt;wal_level=logical&lt;/code&gt; with a replication slot per database (a stopped connector makes Postgres keep WAL until the disk fills), and connectors to register again after every install or restore.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step one&lt;/strong&gt; replaced Kafka with Valkey Streams and moved the relay inside each service:&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="c1"&gt;-- claim unpublished rows; replicas cooperate instead of fighting&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;outbox&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;published_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;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;100&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;A trigger fires &lt;code&gt;NOTIFY&lt;/code&gt; on commit, so the relay wakes up right away and polls once per second as a fallback. Consumers dedupe through a &lt;code&gt;processed_events&lt;/code&gt; table, which turns at-least-once delivery into effectively-once database effects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step two&lt;/strong&gt; removed Valkey too. Once every module ran in one process, the relay could hand committed rows straight to the consumers in memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I learned:&lt;/strong&gt; the broker was only transport. Correctness came from the outbox, the dedupe table and the dead letters, and all three are plain Postgres. I kept them, so going back to a real broker would mean changing one adapter, not every producer and handler.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Keycloak: we already owned everything except the passwords
&lt;/h2&gt;

&lt;p&gt;When I listed what Keycloak actually did for us, the list was short. The sign-in screen, recovery, invitations and authorization were already our own code. Keycloak only stored credentials and issued tokens, and for that it used 0.55–0.8 GB of JVM heap and took a minute to start.&lt;/p&gt;

&lt;p&gt;I replaced it with code in the identity module built on &lt;code&gt;bcrypt&lt;/code&gt;, &lt;code&gt;jose&lt;/code&gt; and &lt;code&gt;node:crypto&lt;/code&gt;: bcrypt passwords, short-lived Ed25519 JWTs, and refresh tokens that rotate on every use, where reusing a spent one revokes the whole session.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I learned:&lt;/strong&gt; owning security-critical code is only acceptable if you test the attacks, not just the happy path. The tests cover forged tokens, &lt;code&gt;alg: none&lt;/code&gt;, HS256 confusion, refresh-token reuse, timing on unknown emails and TOTP replay.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Eight databases → one database, a schema and a role per module
&lt;/h2&gt;

&lt;p&gt;A database per service is supposed to give isolation: no service reads another's tables. On one small server, eight databases also meant eight catalogs, eight sets of autovacuum workers, a mandatory PgBouncer, and every backup and role change done eight times.&lt;/p&gt;

&lt;p&gt;Postgres gives the same isolation for much less: one database, one &lt;strong&gt;schema&lt;/strong&gt; per module, and one &lt;strong&gt;login role&lt;/strong&gt; per module that only has rights on its own schema. A module that reads another module's table gets &lt;code&gt;permission denied&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;At the same time I squashed &lt;strong&gt;139 migration files&lt;/strong&gt; into one baseline per schema, then compared &lt;code&gt;pg_dump --schema-only&lt;/code&gt; of the old and new layouts to prove nothing had changed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I learned:&lt;/strong&gt; decide what you actually want from a pattern. "Database per service" really meant "nobody reads my tables", and Postgres privileges already provide that.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Nine processes → four → one: keep the boundaries, drop the network
&lt;/h2&gt;

&lt;p&gt;Each Node process cost 110–130 MB of RAM at idle, whatever it did. Grouping nine processes into four took the backend from 1,087 MB to 506 MB. Then I went down to one.&lt;/p&gt;

&lt;p&gt;The modules stayed modules, each with its own folder, schema, role, outbox and HTTP API. Two things keep them honest:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;An ESLint rule&lt;/strong&gt; refuses any import from one module's folder into another's.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Modules still call each other through a URL.&lt;/strong&gt; When the target lives in the same process, the request goes to its handler in memory: a real &lt;code&gt;IncomingMessage&lt;/code&gt; and &lt;code&gt;ServerResponse&lt;/code&gt;, no socket, the same middleware and authorization. Moving a module back out is a configuration change.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;(I first tried &lt;code&gt;light-my-request&lt;/code&gt; for this. It re-parents Express's shared request prototype, which broke every other Express app in the process.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I learned:&lt;/strong&gt; a microservice boundary is mostly a code boundary and a data boundary. The network hop is the expensive part, and you can remove it without losing the other two.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Two smaller removals
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Row-level security.&lt;/strong&gt; Every table had a &lt;code&gt;tenant_id&lt;/code&gt; and forced RLS, and every query first set the tenant in its transaction. A self-hosted install has exactly one company, so there is no second tenant to leak data to. Meanwhile my dev database had collected about &lt;strong&gt;1,900 test workspaces&lt;/strong&gt;. I removed it, and queries are plain SQL again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OpenTelemetry.&lt;/strong&gt; It cost a preload in every process and fifteen packages, and it was off in production by default. What owners actually ask is "who did what?", which the audit log answers, and "why did this request fail?", which one log line with a request id answers.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I gave up
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No horizontal scaling.&lt;/strong&gt; Events, caches and WebSocket rooms live in one process's memory. I kept the outbox and the URL indirection so that scaling out again stays a contained change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One crash restarts everything&lt;/strong&gt;, and a CPU-heavy request delays the other modules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No latency dashboards&lt;/strong&gt;, and no SSO, SAML or passkeys until someone needs them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For one company on one server, I'll take that deal.&lt;/p&gt;

&lt;h2&gt;
  
  
  The numbers
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Before&lt;/th&gt;
&lt;th&gt;After&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Backend processes&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Postgres databases&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;1 (a schema and a role per module)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Message broker&lt;/td&gt;
&lt;td&gt;Kafka + Kafka Connect + Debezium&lt;/td&gt;
&lt;td&gt;none (outbox in Postgres)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Identity provider&lt;/td&gt;
&lt;td&gt;Keycloak&lt;/td&gt;
&lt;td&gt;own module (bcrypt + jose)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Migrations on a fresh install&lt;/td&gt;
&lt;td&gt;139&lt;/td&gt;
&lt;td&gt;one baseline per module&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Docker image&lt;/td&gt;
&lt;td&gt;523 MB&lt;/td&gt;
&lt;td&gt;~290 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Idle RAM, whole install&lt;/td&gt;
&lt;td&gt;~3 GB (estimated)&lt;/td&gt;
&lt;td&gt;~300 MB (measured)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;The code, all 27 ADRs and a live demo (no sign-up, it runs in your browser):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Repo:&lt;/strong&gt; &lt;a href="https://github.com/hungga1711/workspace" rel="noopener noreferrer"&gt;https://github.com/hungga1711/workspace&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Live demo:&lt;/strong&gt; &lt;a href="https://workspace.hungga1711.dev/live-demo" rel="noopener noreferrer"&gt;https://workspace.hungga1711.dev/live-demo&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's MIT-licensed. If this was useful, a ⭐ helps more people find it, and I'm happy to answer questions about any of these decisions in the comments.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclosure: I built this project with heavy help from AI coding tools (Claude Code); the architecture decisions and the ADRs behind this post are mine.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>postgres</category>
      <category>node</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Optimizing Software for Extremely Slow Networks (1KB/s)</title>
      <dc:creator>Hưng Trần Minh</dc:creator>
      <pubDate>Sun, 02 Nov 2025 10:32:32 +0000</pubDate>
      <link>https://dev.to/hungga1711/optimizing-software-for-extremely-slow-networks-1kbs-1hfo</link>
      <guid>https://dev.to/hungga1711/optimizing-software-for-extremely-slow-networks-1kbs-1hfo</guid>
      <description>&lt;p&gt;&lt;em&gt;Learn how I made software feel fast even on an extremely slow 1KB/s network.&lt;br&gt;&lt;br&gt;
I share practical strategies for optimizing frontend, API calls, and data handling.&lt;/em&gt;&lt;/p&gt;



&lt;p&gt;📹 &lt;strong&gt;Real Vessel Network Test&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;

  &lt;iframe src="https://www.youtube.com/embed/ATTS6nS_WyA"&gt;
  &lt;/iframe&gt;


&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Recorded on a vessel (using VSAT) while testing the real network speed for an old project (~1KB/s)&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;The network was extremely slow, and even small amounts of data took a long time to load.&lt;/p&gt;

&lt;p&gt;At this speed, the system I built for normal internet could not work properly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Frontend&lt;/strong&gt; loaded very slowly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API requests&lt;/strong&gt; took a long time or sometimes failed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;User interactions&lt;/strong&gt; felt unresponsive and laggy.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most parts of the system assumed a normal network speed.&lt;/p&gt;

&lt;p&gt;I realized I needed to think carefully about &lt;strong&gt;how the frontend requests data&lt;/strong&gt; and &lt;strong&gt;how the API responds&lt;/strong&gt;, so the system could still &lt;strong&gt;feel usable&lt;/strong&gt; even at 1 KB/s.&lt;/p&gt;




&lt;h2&gt;
  
  
  Speeding Up the Software
&lt;/h2&gt;

&lt;p&gt;After seeing how the software struggled at 1 KB/s, I focused on improving &lt;strong&gt;every layer&lt;/strong&gt; that affects speed and responsiveness.&lt;br&gt;&lt;br&gt;
Here’s how I approached it:&lt;/p&gt;
&lt;h2&gt;
  
  
  Frontend
&lt;/h2&gt;

&lt;p&gt;I built the frontend with &lt;strong&gt;Preact&lt;/strong&gt; and &lt;strong&gt;Vite&lt;/strong&gt;, prioritizing fast loading and responsiveness even on extremely slow networks.&lt;/p&gt;
&lt;h3&gt;
  
  
  1. Why Preact?
&lt;/h3&gt;

&lt;p&gt;On a 1 KB/s connection, every kilobyte matters. Using a large framework like React would make even a simple page feel heavy. That’s why I switched to Preact, it offers the same React-style API (hooks, JSX, context) but with a core size of only ~3 KB gzipped.&lt;/p&gt;

&lt;p&gt;My project uses a &lt;strong&gt;microfrontend architecture&lt;/strong&gt;, where each part of the system is built and &lt;strong&gt;deployed independently&lt;/strong&gt;.&lt;br&gt;
That means every app ships its own JavaScript runtime — and with React, those runtimes quickly &lt;strong&gt;add up to hundreds of kilobytes&lt;/strong&gt;.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F159o24bnhwftywm58zts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F159o24bnhwftywm58zts.png" alt="Microfrontend"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By using &lt;strong&gt;Preact&lt;/strong&gt;, that overhead shrinks to just &lt;strong&gt;a few kilobytes per app&lt;/strong&gt;.&lt;br&gt;
This makes the overall system load faster and reduces memory usage on low-end devices, while still &lt;strong&gt;keeping the same React-style&lt;/strong&gt; development experience.&lt;/p&gt;
&lt;h3&gt;
  
  
  Comparing Bundle Size
&lt;/h3&gt;

&lt;p&gt;To see how much the choice of framework really matters, I ran a simple test.&lt;br&gt;
I created three minimal “Hello World” apps, one with &lt;strong&gt;React&lt;/strong&gt;, one with &lt;strong&gt;Next.js&lt;/strong&gt;, and one with &lt;strong&gt;Preact&lt;/strong&gt;, all stripped of CSS, assets, and icons. Then I built each project using their respective analyzers:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next.js&lt;/strong&gt; with &lt;code&gt;next/bundle-analyzer&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;React&lt;/strong&gt; and &lt;strong&gt;Preact&lt;/strong&gt; with &lt;code&gt;nonzzz/vite-bundle-analyzer&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The results below show how much smaller Preact’s bundle is compared to React and Next.js.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvog1nwajwf3qz37olell.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvog1nwajwf3qz37olell.png" alt="Comparing Bundle Size"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Advanced Frontend Optimizations
&lt;/h3&gt;

&lt;p&gt;Choosing Preact gave me a smaller starting point, but that was only the beginning.&lt;br&gt;
To make the app truly usable on a 1 KB/s network, I applied several architectural and build-level optimizations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.1. Bundle Splitting (Dynamic Import + Suspense)&lt;/strong&gt;&lt;br&gt;
Instead of shipping one large JavaScript bundle, I split the app into smaller chunks and load only what’s needed, using dynamic &lt;code&gt;import()&lt;/code&gt;, &lt;code&gt;lazy()&lt;/code&gt;, and &lt;code&gt;Suspense&lt;/code&gt; to coordinate loading and fallback UI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Suspense&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;preact&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;lazy&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;preact/compat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Heavy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./HeavyComponent&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&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="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Suspense&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Loading&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;}&amp;gt; /&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;shown&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="nx"&gt;HeavyComponent&lt;/span&gt; &lt;span class="nx"&gt;loads&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Heavy&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Suspense&lt;/span&gt;&lt;span class="err"&gt;&amp;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;strong&gt;2.2. Server-Side Rendering (SSR)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;SSR speeds up perceived performance by sending ready-to-render HTML directly from the server.&lt;br&gt;
Users see content immediately, even long before JavaScript finishes loading.&lt;/p&gt;

&lt;p&gt;However, that HTML is static at first.&lt;br&gt;
The page still needs to download and hydrate JavaScript before it becomes interactive.&lt;br&gt;
That’s why the size of the client bundle still matters, and this is where Preact shines.&lt;/p&gt;

&lt;p&gt;In short: &lt;strong&gt;SSR gives instant content, and Preact makes it interactive faster&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.3. Asset Optimization &amp;amp; Caching&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Even with SSR and small bundles, the network is often the slowest part of the system.&lt;br&gt;
To minimize loading time, I focused on reducing asset size and making browser caching more effective.&lt;/p&gt;

&lt;p&gt;All static assets, JavaScript, CSS, and images, are deployed with &lt;strong&gt;hashed filenames&lt;/strong&gt; (e.g., main.[hash].js) so browsers can cache them indefinitely.&lt;br&gt;
When a new build is deployed, only the changed files are downloaded again.&lt;/p&gt;

&lt;p&gt;Assets are served through a &lt;strong&gt;CDN&lt;/strong&gt;, which delivers them from the nearest edge location, greatly improving latency, especially important on slow or unstable connections.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0eyz5j19vwuz8nfiyf9f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0eyz5j19vwuz8nfiyf9f.png" alt="CDN"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each asset is served with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Cache-Control: public, max-age=31536000, immutable&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;while HTML remains lightly cached so users always get the latest version.&lt;/p&gt;

&lt;p&gt;Images are converted to &lt;strong&gt;WebP&lt;/strong&gt; to reduce file size without visible quality loss, and placeholders are used for lazy loading so content appears immediately.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8bevnsc3qggmi8xy25u5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8bevnsc3qggmi8xy25u5.png" alt="PNG and WebP"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Backend
&lt;/h2&gt;

&lt;p&gt;Even with a lightweight frontend, the app can still feel slow if the API is heavy.&lt;br&gt;
To optimize both speed and payload size, I focused on several key areas:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Zstd Compression&lt;/strong&gt;&lt;br&gt;
The backend uses &lt;strong&gt;Zstd&lt;/strong&gt; at &lt;strong&gt;level 21&lt;/strong&gt; for maximum compression, but only if the client requests it.&lt;br&gt;
Responses include a header indicating the compression method (&lt;code&gt;Content-Encoding: zstd&lt;/code&gt;), so clients that don’t support it still receive plain data.&lt;br&gt;
This dramatically reduces payload size while remaining compatible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: The client sends the following request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="nf"&gt;GET&lt;/span&gt; &lt;span class="nn"&gt;/api/data&lt;/span&gt; &lt;span class="k"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt;
&lt;span class="na"&gt;Host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;example.com&lt;/span&gt;
&lt;span class="na"&gt;Accept-Encoding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;gzip, zstd&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Accept-Encoding&lt;/code&gt; header tells the server that the client can accept compressed data in the gzip or zstd formats, and prefers the server to send compressed data if possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: The server sends the following response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="k"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt; &lt;span class="m"&gt;200&lt;/span&gt; &lt;span class="ne"&gt;OK&lt;/span&gt;
&lt;span class="na"&gt;Content-Encoding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;zstd&lt;/span&gt;
&lt;span class="na"&gt;Content-Length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;12345&lt;/span&gt;

&amp;lt;compressed data in zstd&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Binary Serialization with Protobuf&lt;/strong&gt;&lt;br&gt;
To minimize payload size and speed up parsing, API responses are encoded using Protobuf instead of JSON.&lt;br&gt;
&lt;strong&gt;Protobuf&lt;/strong&gt; is compact, fast, and easy to maintain, making it ideal for low-bandwidth scenarios.&lt;/p&gt;

&lt;p&gt;I also considered alternatives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;JSON&lt;/strong&gt;: human-readable, but verbose and slower to parse.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FlatBuffers&lt;/strong&gt;: extremely compact and fast, but complex to maintain and harder to evolve over time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Protobuf&lt;/strong&gt; hits the sweet spot: small payloads, fast parsing, and maintainable schema evolution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight protobuf"&gt;&lt;code&gt;&lt;span class="na"&gt;syntax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"proto3"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;message&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="na"&gt;name&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="k"&gt;repeated&lt;/span&gt; &lt;span class="n"&gt;Address&lt;/span&gt; &lt;span class="na"&gt;address&lt;/span&gt; &lt;span class="o"&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="kd"&gt;message&lt;/span&gt; &lt;span class="nc"&gt;Address&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="na"&gt;line&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For real-world performance benchmarks, see &lt;a href="https://github.com/protobufjs/protobuf.js/?tab=readme-ov-file#performance" rel="noopener noreferrer"&gt;protobuf.js performance&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. HTTP/3 Transport&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;All API requests use &lt;strong&gt;HTTP/3&lt;/strong&gt;, taking advantage of &lt;strong&gt;QUIC’s&lt;/strong&gt; multiplexed streams and improved loss recovery.&lt;br&gt;
Compared to HTTP/2 or HTTP/1.1, HTTP/3 reduces latency, avoids head-of-line blocking, and handles packet loss more efficiently, especially beneficial on &lt;strong&gt;unstable&lt;/strong&gt; or &lt;strong&gt;slow networks&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Optimizing for slow networks requires attention to every layer of the stack, from frontend rendering to API transport.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Frontend optimizations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Preact + Vite&lt;/strong&gt; for minimal bundle size and fast hydration&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bundle splitting&lt;/strong&gt; with &lt;code&gt;lazy()&lt;/code&gt; + &lt;code&gt;Suspense&lt;/code&gt; for component and route-level lazy loading&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SSR&lt;/strong&gt; to deliver instant HTML, with Preact’s small runtime enabling much faster hydration than Next.js&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Asset optimization &amp;amp; caching&lt;/strong&gt; with hashed filenames, CDN delivery, and WebP images&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;API optimizations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Zstd compression&lt;/strong&gt; (level 21) applied conditionally for maximum payload reduction&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Protobuf&lt;/strong&gt; for compact, fast, and maintainable serialization&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HTTP/3 transport&lt;/strong&gt; for low-latency, reliable delivery on unstable networks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Combined, these strategies allowed the app to remain fast, responsive, and usable even at 1 KB/s, while keeping the total payload minimal.&lt;/p&gt;

&lt;p&gt;Thanks for reading! &lt;/p&gt;

&lt;p&gt;I hope these insights give you practical ideas for building fast and responsive apps, even on extremely slow networks.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>backend</category>
      <category>api</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
