Console Login

Bun vs Node.js on a VPS: Check CPU Steal and Disk I/O First

The problem

A small HTTP service, such as a webhook receiver written with Express on Node.js, shows high time to first byte (TTFB) and uneven latency at busy times. The usual reaction is to blame the event loop and switch runtime, for example from Node.js to Bun. After the switch the improvement is small or does not last, and bun install or the container start still feels slow on the VPS.

Typical symptoms on the server:

  • Latency spikes while average CPU use inside the guest looks moderate.
  • Requests that touch the disk (logs, queues, SQLite, uploaded payloads, templates) are much slower than requests served from memory.
  • Install and build steps vary a lot between runs on the same machine.
  • A trivial "hello world" handler is fast, but the real handler is not.

Why it happens

A runtime can only use the resources the virtual machine really gets. Three host-level effects commonly cancel a runtime upgrade:

  • CPU steal. On an oversubscribed hypervisor the guest is ready to run but the host schedules another guest. The guest kernel counts this as steal time. A faster JavaScript engine does not help when the vCPU is not scheduled.
  • Slow or shared storage. Synchronous writes (fsync), log flushes and file reads wait on the block device. On network-attached or heavily shared storage, the latency of each small I/O is high and varies with other tenants.
  • Memory pressure. Swapping on a small VPS turns every allocation spike into disk I/O.

Some claims in the usual runtime comparison also need correcting:

  • Bun uses JavaScriptCore and Node.js uses V8. Both are mature engines. Which one is faster depends on the workload, so a general claim about one being better for containers is not reliable.
  • Bun.file() does not read a file or map it into memory. It returns a lazy BunFile reference, which is a Blob. Data is read only when a method such as .text() or .json() is awaited. It is not a thin wrapper around a single read() call.
  • The fio test often quoted for this, with posixaio at queue depth 1 and no direct I/O, mostly measures page cache and one outstanding request. It does not show what the device can do, and a fixed IOPS threshold is not a valid verdict on a provider.
  • Node.js 18 is past its end of life, so any comparison should use a currently supported Node.js LTS release, not the oldest one still deployed.

Source of truth

The fix relies on documentation of how the tools measure things, not on anecdotes. These references are the basis for the steps below:

  • Linux kernel documentation for /proc. It defines the per-CPU counters in /proc/stat, including the steal field. That field is time spent in other operating systems when running virtualized. It is the figure that vmstat (column st), top (st) and mpstat (%steal) report.
  • fio documentation. It describes ioengine, direct, iodepth, fsync and runtime/time_based. It confirms that buffered I/O goes through the page cache unless direct=1 is set, and that queue depth only matters with an asynchronous engine such as libaio.
  • Bun file I/O documentation. It confirms that Bun.file() returns a lazily loaded BunFile (a Blob) and that content is read on demand.
  • Bun lockfile documentation and the Bun 1.2 release notes. They cover the lockfile format. Older Bun releases wrote a binary bun.lockb. Bun 1.2 (2025) switched the default to a text-based bun.lock.
  • Bun Docker guide. It documents the official oven/bun image and the multi-stage build pattern used below.

Fix options

Option 1: Change the runtime only

Move the service from Node.js to Bun and keep the host. This is cheap when the code is compatible. It helps only if the process is CPU-bound in user space inside the guest. It does nothing for steal or slow storage.

Option 2: Reduce the I/O the service does

Keep hot data in memory, batch writes, make logs asynchronous or send them to a local buffer, and avoid fsync on the request path. This works on any host, but it changes durability behaviour and needs care.

Option 3: Fix the host

Move to a plan with dedicated or non-oversubscribed vCPUs, locally attached SSD or NVMe storage, and enough RAM to avoid swap. Full virtualization such as KVM isolates the guest kernel, but it does not by itself guarantee CPU time. The scheduling policy of the provider decides that. Container-based virtualization such as OpenVZ shares one kernel and its I/O scheduler among all tenants.

Option 4: Add a reverse proxy and tune the edge

Terminate TLS and set security headers in Nginx or Caddy, keep-alive to the upstream, and let the proxy serve static files. This offloads work from the application but does not fix a starved host.

Why this fix

The order that follows from the cause is: measure the host, fix the host or the I/O pattern if it is the limit, and only then compare runtimes. A runtime comparison on a starved VM measures the noise of the host, not the runtime. So the plan is Option 3 when measurements show steal or slow storage, Option 2 for I/O that is unnecessary, and Option 1 as a separate, controlled experiment afterwards. Option 4 is applied in all cases because it is independent of the others and is the standard place for TLS and headers.

The measurements are also the evidence needed to talk to a provider. Steal percentages and fio latency percentiles are concrete figures, unlike "the server feels slow".

Step by step

The commands below are the procedure. Run them on the affected VPS and record the output. This article does not include measured results, because the numbers depend entirely on the host.

1. Check CPU steal and memory under real load

Install the sysstat package, start the load, and watch for 60 seconds. In vmstat, look at the st and si/so columns. Steal that stays above a few percent, or any steady swapping, points at the host or at too little RAM.

vmstat 1 60
mpstat -P ALL 1 60
free -m

2. Check the disk the service really uses

Watch per-device latency (await, %util) while the service handles traffic:

iostat -x 1 60

Then test the device itself. Use a path on the same filesystem as the application data. The first test measures random 4k writes with direct I/O and a realistic queue depth:

fio --name=randwrite --filename=/var/tmp/fio-test --size=1g \
    --ioengine=libaio --direct=1 --rw=randwrite --bs=4k \
    --iodepth=32 --numjobs=1 --runtime=60 --time_based \
    --group_reporting

The second test measures what a request pays when it waits for durable writes: queue depth 1 with an fsync after every write. Compare the completion latency percentiles, not only the average:

fio --name=fsync-lat --filename=/var/tmp/fio-test --size=256m \
    --ioengine=sync --rw=write --bs=4k --fsync=1 \
    --runtime=60 --time_based --group_reporting
rm /var/tmp/fio-test

3. Measure TTFB and throughput of the service

A single request shows connection time and time to first byte separately:

curl -o /dev/null -s -w 'connect=%{time_connect} ttfb=%{time_starttransfer} total=%{time_total}\n' http://127.0.0.1:3000/

For sustained load, use a tool such as wrk from a different machine, so that the load generator does not compete for the same CPU:

wrk -t2 -c64 -d30s --latency http://SERVER_ADDRESS:3000/

4. After the host is sound, compare the runtimes

Use the same handler, the same host, the same load command and a supported Node.js LTS. A minimal Bun server with a health endpoint:

// index.ts
const port = 3000;

Bun.serve({
  port,
  fetch(req) {
    const url = new URL(req.url);
    if (url.pathname === "/") return new Response("Hello");
    if (url.pathname === "/health") return new Response("OK");
    return new Response("Not found", { status: 404 });
  },
});
console.log(`Listening on port ${port}`);
bun run index.ts

Reading a file is lazy. Check the size first if the file may be large, and read it once at startup instead of per request:

// file-ops.ts
const file = Bun.file("heavy-data.json");
console.log(`File size: ${file.size} bytes`);
const data = await file.json(); // read once, keep in memory

5. Time the dependency install in CI

Compare installs from a lockfile on a clean checkout, with and without a warm cache, on the same runner:

time bun install --frozen-lockfile
time npm ci

6. Container build

A multi-stage build following the official image pattern. With Bun 1.2 or later the lockfile is bun.lock. With older Bun 1.x it is bun.lockb, so copy the file that exists in the repository. Tests run before NODE_ENV=production is set, because the production setting is meant for the final build and runtime:

FROM oven/bun:1 AS base
WORKDIR /usr/src/app

FROM base AS install
COPY package.json bun.lock ./
RUN bun install --frozen-lockfile

FROM base AS build
COPY --from=install /usr/src/app/node_modules node_modules
COPY . .
RUN bun test
ENV NODE_ENV=production
RUN bun build ./index.ts --outdir ./out --target bun

FROM base AS release
COPY --from=build /usr/src/app/out/index.js .
USER bun
EXPOSE 3000
ENTRYPOINT [ "bun", "run", "index.js" ]

7. Headers and TLS at the edge

Set the headers once in the TLS-terminating proxy. The Strict-Transport-Security header is only honoured over HTTPS, and preload should be added only when the domain is deliberately submitted to the preload list. An Nginx example:

add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "DENY" always;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;

Review

What the procedure achieves

It separates three questions that are often mixed: is the host delivering the CPU and disk it promises, is the application doing avoidable I/O, and is the runtime the limit. Each has a measurement with a defined meaning in the kernel and fio documentation.

Limits

  • Steal is visible only from inside the guest as a symptom. The cause (oversubscription) can be confirmed only by the provider or by moving to another host.
  • fio on a file in a shared filesystem still includes filesystem overhead. That is intended, but results from different filesystems are not comparable.
  • Short tests miss periodic neighbour activity. Repeat the measurements at the hours when the problem appears.
  • Microbenchmarks of a trivial handler do not predict the performance of a handler that uses a database, a network call or heavy JSON work.
  • Compatibility matters more than speed when moving to Bun. Check native modules and the Node.js APIs the application uses before switching.

When to choose another method

If steal is near zero, disk latency is low and the CPU of the service is saturated in user space, the runtime or the code is the real limit. Profile the application, then consider Bun as an experiment. If the service waits on a database or an external API, tune those first, with connection pooling and caching. If data location is a requirement, choose the hosting region for that reason, since it has no effect on runtime speed and should be treated as a separate legal and architectural decision.