LazyLayersv9.0.0
Guides

Load testing and sizing

One million HTTP requests with and without LazyLayers: requests per second, latency, CPU, memory and database queries, and how to turn them into an instance size.

benchmarks/api-stress.mjs sends 1,000,000 HTTP requests to a small user API twice: once through LazyLayers and once straight to the database. It reports requests per second, latency percentiles, server CPU and memory, and how many queries reached the database. This page shows the 8.0.0 result and how to turn it into an instance size.

One million callers, one query

Before the API test, the simplest stampede there is: 1,000,000 callers ask for the same missing key at the same moment.

RunLoader callsCallers served that valueAll served in
10 processes sharing Redis, 100,000 callers each1999,999206.4 ms
One process1999,999293.8 ms

Inside a process, callers join the load already running. Across processes, the Redis lease lets one server run the query, and the others are woken over the event bus when the value is published. inflight.maxWaiters was raised to the callers per process for this run; the default of 10,000 bounds memory and rejects callers beyond it with InflightOverloadError. Run it with npm run bench:herd-million.

The setup

  • API: one node:http process, GET /users/:id through getOrSet, plus 1% PUT /users/:id that writes and then calls delete.
  • Cache: setupCache with Redis 7 in Docker, capped at 1 CPU. L1 holds up to 20,000 entries or 64 MB.
  • Database: simulated, so the cost is fixed and known: a pool of 10 connections at 2 ms per query.
  • Traffic: four load-generator processes, 256 keep-alive connections, user ids on a Zipf 0.99 curve over 100,000 users. Both runs start cold.
  • Machine: Apple M5 (10 logical CPUs, 16 GB), Node 24.21. CPU and memory are sampled inside the server process every 250 ms.

Results

1,000,000 requestsLazyLayers 8.0.0No cache
Requests per second47,0584,394
Latency p502.01 ms58.02 ms
Latency p9944.72 ms66.02 ms
Database queries98,5151,000,000
Server CPU per request16 µs50.1 µs
Server CPU, average75.5% of one core22% of one core
Peak RSS286.5 MB170.2 MB
Peak heap128.2 MB62.6 MB
Failed requests3690

Reads were answered by L1 (726,388), Redis L2 (104,384), by joining a load already in flight (70,683), and by the database (88,363).

Raw output: benchmarks/results/8.0.0/api-stress.json. Every figure is also in BENCHMARKS.md.

Read this before quoting it

  • 369 requests (0.04%) failed. Each was a read whose load raced a PUT that deleted the same key. The lease correctly refused to publish the old value, but the read threw DistributedLockLostError instead of returning a value. With writes off, 300,000 requests had no failures. The fix is tracked for 8.1.0 in #28. Until then, if your reads can race writes to the same key, catch DistributedLockLostError and call getOrSet again.
  • The p99 is the cold start. The first read of each user waits for the 10-connection pool. Set originLoad.maxConcurrent at or below your pool size so the queue sits in the cache, where it is bounded, rather than in the driver.
  • Production L1 entries live 10 seconds, so some reads go back to Redis even for values this process already loaded.
  • One machine. The load generator shares the CPUs with the server and Redis, and the database is simulated. The numbers describe the cache and a bare HTTP handler, not your queries or your framework.

Sizing an instance

Scale the measured CPU per request to the machine you plan to use:

requests/s ≈ vCPUs × (1,000,000 ÷ CPU µs per request) × core factor × 0.7
  • Run one Node.js process per vCPU (cluster mode, PM2, or one ECS task per vCPU).
  • CPU µs per request: 16 µs for the cache and a bare handler. Add your route's own cost: authentication, logging, the framework, serialisation.
  • Core factor: how much of an Apple M5 core one cloud vCPU does. We assume 0.5 for an Arm vCPU (Graviton, Axion) and 0.4 for an x86 vCPU, which is one hyperthread. These are assumptions, not measurements.
  • 0.7 keeps 30% of the CPU in reserve for spikes and garbage collection.
  • Burstable machines (t3, t4g, e2-medium) count their sustained baseline, not their burst.
  • Memory: about 290 MB per process with a 64 MB L1. Memory is rarely the limit; CPU is.

With the 16 µs figure alone, the estimates come out as below. The benchmarks page lists more machines.

InstancevCPU · RAMRequests / sDatabase queries / sMemory
AWS EC2 t3.medium (20% baseline)2 · 4 GB~7,000~690580 MB
AWS EC2 m7g.large2 · 8 GB~44,000~4,300580 MB
AWS EC2 c7i.2xlarge8 · 16 GB~140,000~14,0002,320 MB
AWS ECS Fargate1 vCPU · 2 GB~18,000~1,700290 MB
Google Cloud e2-standard-22 · 8 GB~35,000~3,400580 MB
Google Cloud Run1 vCPU · 512 MiB~18,000~1,700290 MB

The database column is what still reaches your database at that rate, using the 9.9% of requests that reached it in this run. Without a cache the database usually sets the limit, not the instance: 10 connections at 2 ms is about 5,000 queries a second on any machine.

Run it yourself

Run it on the instance you plan to use; that number replaces every estimate above.

git clone https://github.com/Amon20044/LazyLayers
cd LazyLayers && npm install
docker run -d --rm --name ll-stress-redis -p 127.0.0.1:6390:6379 --cpus 1 --memory 256m redis:7-alpine
npm run bench:api-stress

Change the run with environment variables:

VariableDefaultMeaning
STRESS_REQUESTS1000000Requests per mode
STRESS_MODESlazylayers,no-cacheWhich runs to make
STRESS_KEYS100000Distinct users
STRESS_ZIPF0.99Skew of the id distribution
STRESS_WRITE_RATIO0.01Share of PUT requests
STRESS_LOADERS · STRESS_CONNECTIONS4 · 64Generator processes, connections each
STRESS_DB_POOL · STRESS_DB_MS10 · 2Simulated database pool and query time
STRESS_L1_ENTRIES20000L1 entry limit
STRESS_REDIS_URLredis://127.0.0.1:6390Redis for L2
STRESS_OUTPUTbenchmarks/results/8.0.0/api-stress.jsonWhere the JSON goes

Related: Prevent stampedes, Production setup, Eviction and memory.

Questions

How many requests per second can one Node.js process serve with LazyLayers?

In our run, one process served 1,000,000 requests at 47,058 per second, using 16 µs of server CPU per request and 75.5% of one Apple M5 core on average. Without a cache, the same API managed 4,394 per second because every request waited for a 10-connection database pool. Your route's own CPU adds to the 16 µs.

How much memory does LazyLayers use under load?

Peak RSS was 286.5 MB with an L1 capped at 20,000 entries and 64 MB, against 170.2 MB without a cache. Peak heap was 128.2 MB against 62.6 MB. Budget about 290 MB per process at that L1 size.

How many database queries does the cache save?

From a cold start, 1,000,000 requests made 98,515 database queries instead of 1,000,000: 90% fewer. 73.4% of reads were answered from L1, 10.5% from Redis, 7.1% by joining a load already in flight, and 8.9% by the database.

How many database queries do one million callers on one cold key cause?

One. In our run, 1,000,000 concurrent callers across 10 processes sharing Redis made one loader call, and the other 999,999 received its value within 206.4 ms. Raise inflight.maxWaiters above the default of 10,000 per process for herds that large.

Which instance size do I need?

Estimate requests per second as vCPUs × (1,000,000 ÷ CPU µs per request) × core factor × 0.7, with one Node.js process per vCPU. Measure your own route's CPU per request first, then confirm by running benchmarks/api-stress.mjs on the instance you plan to use.

On this page