Skip to content
System design · Chapter 2 of 21 Day 0: The training ground

Day 0: The training ground

Goal: Three empty Ubuntu VMs become the ground on which your URL shortener grows over the next days: database, cache, queue, load balancer and load generator · Duration: approx. 75 min · Prerequisite: three VMs with Ubuntu 24.04, SSH access and sudo

Briefing

  • System design is usually practiced at the whiteboard: boxes, arrows, “a cache goes here”. In this course, a real service stands behind every box, and every decision is measured.
  • The example is a URL shortener called kurz: it turns a long address into a short code and redirects when the code is called. Small enough to understand in one file, and large enough for almost every topic: lots of reads, few writes, unique IDs, statistics.
  • Today you only build the ground. You write the app on Day 1.
             Management network 172.16.0.0/24
 ──────┬────────────────────┬────────────────────┬──────
 s1 .126                s2 .228              s3 .83
 HAProxy                kurz (from Day 3)    PostgreSQL (Primary)
 kurz                   PostgreSQL (from Day 8, replica)
 k6, Go                 NATS with JetStream  Valkey
 = workstation
Service Version VM Port
PostgreSQL 18.7 s3 (from Day 8 also s2) 5432
Valkey 9.2.0 s3 6379
NATS Server 2.15.3 s2 4222, monitoring 8222
HAProxy 3.2.27 (LTS) s1 from Day 3
Go 1.27.4 s1 –
k6 2.4.0 s1 –

Drill 1: The starting point

The book was tested on three VMs under Proxmox: s1 with 4 vCPU and 11 GiB RAM, s2 and s3 with 2 vCPU and 3.8 GiB each. On each VM:

lsb_release -ds
nproc
free -h | head -2
dpkg --print-architecture
sudo ufw status

Expected (on s3):

Ubuntu 24.04.5 LTS
2
               total        used        free      shared  buff/cache   available
Mem:           3.8Gi       507Mi       2.4Gi        41Mi       1.2Gi       3.3Gi
amd64
Status: inactive

If ufw is active, you need rules for 5432 and 6379 on s3 (and 5432 on s2), 4222 and 8222 on s2, and 8080 on s1 and s2, each from the network 172.16.0.0/24.

Names instead of addresses: on each VM, set its own hostname and add all three.

# on s1 (auf s2 und s3 mit s2 bzw. s3)
sudo hostnamectl set-hostname s1
sudo sed -i 's/^127.0.1.1.*/127.0.1.1 s1/' /etc/hosts
printf '172.16.0.126 s1\n172.16.0.228 s2\n172.16.0.83  s3\n' | sudo tee -a /etc/hosts >/dev/null
hostname
grep -E ' s[123]$' /etc/hosts

Expected:

s1
127.0.1.1 s1
172.16.0.126 s1
172.16.0.228 s2
172.16.0.83  s3

Save your progress? Sign up for free

Drill 2: PostgreSQL on s3

Ubuntu 24.04 ships PostgreSQL 16. The official package archive (PGDG) provides the current version:

# on s3
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -fsSo /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc \
  https://www.postgresql.org/media/keys/ACCC4CF8.asc
echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] \
https://apt.postgresql.org/pub/repos/apt noble-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
sudo apt-get update -qq
sudo apt-get install -y postgresql-18
psql --version
pg_lsclusters

Expected (end):

psql (PostgreSQL) 18.7 (Ubuntu 18.7-1.pgdg24.04+5)
Ver Cluster Port Status Owner    Data directory              Log file
18  main    5432 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log

Out of the box, PostgreSQL listens only on localhost. The app runs on s1 and s2, so open it up, but only for the lab network and only with a password:

# on s3
sudo -u postgres psql -c "ALTER SYSTEM SET listen_addresses = '*';"
echo "host  all  all  172.16.0.0/24  scram-sha-256" | sudo tee -a /etc/postgresql/18/main/pg_hba.conf
sudo systemctl restart postgresql@18-main
sudo -u postgres psql -c "CREATE ROLE kurz LOGIN PASSWORD 'lab-kurz-123';" \
  -c "CREATE DATABASE kurz OWNER kurz;"
sudo ss -ltnp | grep 5432 | cut -c1-70

Expected:

ALTER SYSTEM
host  all  all  172.16.0.0/24  scram-sha-256
CREATE ROLE
CREATE DATABASE
LISTEN 0      200          0.0.0.0:5432      0.0.0.0:*
LISTEN 0      200             [::]:5432         [::]:*

Save your progress? Sign up for free

Drill 3: Valkey on s3

Valkey is the open source successor of Redis, developed further under the Linux Foundation and protocol-compatible. The project page offers ready-made binaries for Ubuntu 24.04:

# on s3
ARCH=$(dpkg --print-architecture); [ "$ARCH" = amd64 ] && VA=x86_64 || VA=arm64
cd /tmp && curl -fsSLO https://download.valkey.io/releases/valkey-9.2.0-noble-$VA.tar.gz
tar xzf valkey-9.2.0-noble-$VA.tar.gz
sudo install valkey-9.2.0-noble-$VA/bin/valkey-server valkey-9.2.0-noble-$VA/bin/valkey-cli \
  /usr/local/bin/
valkey-server --version

Expected:

Valkey server v=9.2.0 sha=d23684e3:0 malloc=jemalloc-5.3.0 bits=64 build=13ef4e9195f3619c

A dedicated user, a configuration and a systemd unit:

# on s3
sudo useradd --system --home /var/lib/valkey --create-home --shell /usr/sbin/nologin valkey
sudo mkdir -p /etc/valkey
sudo tee /etc/valkey/valkey.conf >/dev/null <<'EOF'
bind 0.0.0.0
port 6379
protected-mode yes
requirepass lab-valkey-123
dir /var/lib/valkey
maxmemory 256mb
maxmemory-policy allkeys-lru
save ""
appendonly no
EOF
sudo chown valkey /etc/valkey/valkey.conf && sudo chmod 600 /etc/valkey/valkey.conf
sudo tee /etc/systemd/system/valkey.service >/dev/null <<'EOF'
[Unit]
Description=Valkey
After=network-online.target
[Service]
User=valkey
ExecStart=/usr/local/bin/valkey-server /etc/valkey/valkey.conf
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now valkey
valkey-cli -a lab-valkey-123 --no-auth-warning ping

Expected:

Created symlink /etc/systemd/system/multi-user.target.wants/valkey.service → /etc/systemd/system/valkey.service.
PONG

Three lines of the configuration are decisions, not formalities:

Setting Meaning
maxmemory 256mb Upper limit. Without it, a cache grows until the kernel kills it
allkeys-lru When memory is full, the least recently used key is evicted
save "", appendonly no No persistence. A cache may be empty after a restart

Save your progress? Sign up for free

Drill 4: NATS on s2

NATS is a lean messaging service. With JetStream it also stores messages durably, which the click statistics need from Day 10 on.

# on s2
ARCH=$(dpkg --print-architecture)
cd /tmp && curl -fsSLO \
  https://github.com/nats-io/nats-server/releases/download/v2.15.3/nats-server-v2.15.3-linux-$ARCH.tar.gz
tar xzf nats-server-v2.15.3-linux-$ARCH.tar.gz
sudo install nats-server-v2.15.3-linux-$ARCH/nats-server /usr/local/bin/
curl -fsSLO https://github.com/nats-io/natscli/releases/download/v0.5.2/nats-0.5.2-linux-$ARCH.zip
sudo apt-get install -y unzip >/dev/null
unzip -oq nats-0.5.2-linux-$ARCH.zip && sudo install nats-0.5.2-linux-$ARCH/nats /usr/local/bin/
nats-server --version
nats --version

Expected:

nats-server: v2.15.3
0.5.2
# on s2
sudo useradd --system --home /var/lib/nats --create-home --shell /usr/sbin/nologin nats
sudo mkdir -p /etc/nats
sudo tee /etc/nats/nats.conf >/dev/null <<'EOF'
listen: 0.0.0.0:4222
http: 0.0.0.0:8222
server_name: s2
jetstream {
  store_dir: /var/lib/nats
  max_memory_store: 64MB
  max_file_store: 1GB
}
authorization {
  user: kurz
  password: lab-nats-123
}
EOF
sudo tee /etc/systemd/system/nats.service >/dev/null <<'EOF'
[Unit]
Description=NATS Server
After=network-online.target
[Service]
User=nats
ExecStart=/usr/local/bin/nats-server -c /etc/nats/nats.conf
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now nats
nats --server nats://kurz:lab-nats-123@localhost:4222 server check jetstream | cut -c1-60
curl -s localhost:8222/healthz; echo

Expected:

Created symlink /etc/systemd/system/multi-user.target.wants/nats.service → /etc/systemd/system/nats.service.
OK JetStream | memory=0B memory_pct=0%;75;90 storage=0B stor
{"status":"ok"}

PostgreSQL is installed on s2 today already, so that Day 8 only has to set up replication. Until then it stays off:

# on s2
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -fsSo /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc \
  https://www.postgresql.org/media/keys/ACCC4CF8.asc
echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] \
https://apt.postgresql.org/pub/repos/apt noble-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
sudo apt-get update -qq
sudo apt-get install -y postgresql-18 >/dev/null
sudo systemctl disable --now postgresql@18-main
pg_lsclusters

Expected (end):

Ver Cluster Port Status Owner    Data directory              Log file
18  main    5432 down   postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log

Save your progress? Sign up for free

Drill 5: The workstation s1

s1 gets Go for building the app, k6 for measuring, HAProxy for Day 3 and the clients for all services.

# on s1
ARCH=$(dpkg --print-architecture)
curl -fsSL https://go.dev/dl/go1.27.4.linux-$ARCH.tar.gz | sudo tar -C /usr/local -xz
echo 'export PATH=$PATH:/usr/local/go/bin:$HOME/go/bin' >> ~/.profile && . ~/.profile
go version
cd /tmp && curl -fsSL \
  https://github.com/grafana/k6/releases/download/v2.4.0/k6-v2.4.0-linux-$ARCH.tar.gz | tar xz
sudo install k6-v2.4.0-linux-$ARCH/k6 /usr/local/bin/
k6 version

Expected:

go version go1.27.4 linux/amd64
k6 v2.4.0 (commit/dd9395906d, go1.26.11, linux/amd64)

HAProxy: Ubuntu 24.04 ships 2.8. The archive of Vincent Bernat, the Debian maintainer of HAProxy, provides current LTS versions. For 3.4 there are no packages for Ubuntu 24.04 there, so LTS 3.2 it is:

# on s1
sudo add-apt-repository -y ppa:vbernat/haproxy-3.2 >/dev/null
sudo apt-get install -y 'haproxy=3.2.*' >/dev/null
haproxy -v | head -1

Expected:

HAProxy version 3.2.27-1ppa1~noble 2026/11/18 - https://haproxy.org/

The clients for PostgreSQL, Valkey and NATS:

# on s1
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -fsSo /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc \
  https://www.postgresql.org/media/keys/ACCC4CF8.asc
echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] \
https://apt.postgresql.org/pub/repos/apt noble-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
sudo apt-get update -qq && sudo apt-get install -y postgresql-client-18 unzip >/dev/null
[ "$ARCH" = amd64 ] && VA=x86_64 || VA=arm64
curl -fsSL https://download.valkey.io/releases/valkey-9.2.0-noble-$VA.tar.gz | tar xz
sudo install valkey-9.2.0-noble-$VA/bin/valkey-cli /usr/local/bin/
curl -fsSLO https://github.com/nats-io/natscli/releases/download/v0.5.2/nats-0.5.2-linux-$ARCH.zip
unzip -oq nats-0.5.2-linux-$ARCH.zip && sudo install nats-0.5.2-linux-$ARCH/nats /usr/local/bin/
psql --version; valkey-cli --version; nats --version

Expected:

psql (PostgreSQL) 18.7 (Ubuntu 18.7-1.pgdg24.04+5)
valkey-cli 9.2.0 (git:d23684e3)
0.5.2

Save your progress? Sign up for free

Drill 6: Everything reachable?

From s1, address each service once:

# on s1
PGPASSWORD=lab-kurz-123 psql -h s3 -U kurz -d kurz -tAc "select current_user, inet_server_addr()"
valkey-cli -h s3 -a lab-valkey-123 --no-auth-warning ping
nats --server nats://kurz:lab-nats-123@s2:4222 server check connection | cut -c1-80

Expected:

kurz|172.16.0.83
PONG
OK Connection OK:connected to nats://kurz:lab-nats-123@s2:4222 in 7.465807ms OK:rt

The round-trip time between the VMs (rtt) was below one millisecond in the lab. Remember the order of magnitude: every network call you add over the next days costs at least that much.

Save your progress? Sign up for free

Exercise: Speed drills

  1. Show which services are listening on s3. Target time: 30 s
  2. Show the memory limit and the eviction policy of Valkey. Target time: 1 min
  3. Show the state of JetStream via the monitoring interface. Target time: 1 min

Solution

# on s3
sudo ss -ltnp | grep -E ':(5432|6379) '
valkey-cli -a lab-valkey-123 --no-auth-warning config get maxmemory*
# on s1
curl -s http://s2:8222/jsz | jq '{memory, storage, streams}'

Save your progress? Sign up for free

Checkpoint

# on s3
systemctl is-active postgresql@18-main valkey

Expected: active twice

# on s2
systemctl is-active nats

Expected: active

# on s1
PGPASSWORD=lab-kurz-123 psql -h s3 -U kurz -d kurz -tAc "select 1"

Expected: 1

Challenge

A colleague asks why Valkey runs without persistence. What happens to the data after a restart of Valkey, and why is that fine for a cache, but not for a click counter?

Hint: What is only in the cache, and what is also in PostgreSQL?

Solution

After a restart, Valkey is empty. For a cache, that is intended: every entry is a copy from PostgreSQL and is reloaded on the next access. A click counter that lives only in Valkey, on the other hand, would be lost. Data that exists nowhere else belongs in a store with persistence (for us from Day 10: NATS JetStream and PostgreSQL) or in a Valkey with AOF. Check:

# on s3
valkey-cli -a lab-valkey-123 --no-auth-warning set probe 1
sudo systemctl restart valkey
valkey-cli -a lab-valkey-123 --no-auth-warning get probe

Expected:

OK

The empty line is the answer to get: the key no longer exists. In an interactive terminal, valkey-cli prints (nil) instead.

Save your progress? Sign up for free

Clean-up

rm -rf /tmp/valkey-9.2.0-* /tmp/nats-* /tmp/k6-*

Interview note

  • Why Valkey instead of Redis? Redis changed its license in 2024. Valkey is the fork under the Linux Foundation with a BSD license, protocol-compatible. In a system design interview, the pattern (in-memory cache) counts, not the name.
  • Why three machines and not one? Because network calls, partial failures and network partitions should be real. On one machine, everything always fails together.

Enterprise note

  • In the cloud, these building blocks are called RDS, ElastiCache, Amazon MQ or Load Balancer. The patterns are the same, only the knobs are different. If you have operated them yourself once, you understand the limits of managed services better.
  • Passwords like lab-kurz-123 are in plain text here so that every command stays traceable. In production, they come from a secret store such as OpenBao.

Contents

More