Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,12 @@ to be real values, with no dev fallback. See `.env` for the full list; the main
`GET /health` checks Mongo, Postgres, RabbitMQ, and Redis in parallel and returns `503` if any of
the first three are down — Redis failing is non-fatal, since rate limiting is designed to fail
open. In production the response omits per-connection detail; non-production environments get the
full breakdown.
full breakdown.

## Load testing

Ingestion path load-tested with [k6](https://k6.io) (`server/scripts/k6/`). Validated **3,200
req/s sustained for 2 minutes**: p95 290ms, p99 470ms, 0% errors, 0 rate-limited, 376,868 requests
total. Reached after tuning consumer prefetch (20→50), Postgres pool size (10→50), and switching
the RabbitMQ producer to fire-and-forget publishing instead of waiting on a per-message broker
confirm.
1 change: 1 addition & 0 deletions server/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ logs
docs
*.md
npm-debug.log
*.log
3 changes: 3 additions & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ logs
dist
!/docs
!/docs/**
*.log
*.csv
*.err
53 changes: 0 additions & 53 deletions server/scripts/backfillApiHitExpiry.ts

This file was deleted.

24 changes: 24 additions & 0 deletions server/scripts/backfillEmailVerified.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import mongoConnection from "../src/shared/infra/db/mongo/mongoConnection";
import { UserModel } from "../src/shared/infra/db/mongo/models/user.model";
import logger from "../src/shared/config/logger.config";

// One-off backfill: `isEmailVerified` was added after accounts already existed,
// defaulting to false. Since login is now blocked for unverified accounts, this
// would retroactively lock out every pre-existing user (including admins who have
// no way to receive a verification email for an account created before this
// feature existed). Grandfather them in as verified. Safe to re-run — only ever
// touches documents where the field is still unset.
async function run() {
await mongoConnection.connect();

const result = await UserModel.updateMany({ isEmailVerified: { $exists: false } }, { $set: { isEmailVerified: true } });

logger.info(`[BackfillEmailVerified] Grandfathered ${result.modifiedCount} pre-existing user(s) as verified.`);
await mongoConnection.disconnect();
process.exit(0);
}

run().catch((error) => {
logger.error("[BackfillEmailVerified] Failed", { error: error instanceof Error ? error.message : error });
process.exit(1);
});
80 changes: 80 additions & 0 deletions server/scripts/k6/capacity-breaking-point.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import http from "k6/http";
import { check } from "k6";
import { Counter } from "k6/metrics";

// True breaking-point test: no abortOnFail, ramps well past every ceiling
// found so far, so it runs to completion and we read the real degradation
// curve off the end-of-test summary instead of guessing from an early abort.
const BASE_URL = __ENV.BASE_URL || "http://localhost:5000";

Check failure on line 8 in server/scripts/k6/capacity-breaking-point.js

View workflow job for this annotation

GitHub Actions / Lint, typecheck, build

'__ENV' is not defined
const API_KEY = __ENV.API_KEY || "8197381c4f8cdeb988f1eeaa0efd492599041b27fe1aba2261464452834a31a2";

Check failure on line 9 in server/scripts/k6/capacity-breaking-point.js

View workflow job for this annotation

GitHub Actions / Lint, typecheck, build

'__ENV' is not defined

const rateLimited = new Counter("rate_limited_429");
const serverErrors = new Counter("server_errors_5xx");

const SERVICES = ["checkout-service", "auth-service", "payments-api", "inventory-service", "notifications-worker"];
const ENDPOINTS = ["/api/orders", "/api/orders/:id", "/api/users/login", "/api/users/:id", "/api/payments/charge"];
const METHODS = ["GET", "GET", "GET", "POST", "PUT"];
const STATUS_CODES = [200, 200, 200, 201, 204, 400, 404, 500];

function pick(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}

function randomIp() {
return `${1 + Math.floor(Math.random() * 254)}.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}.${1 + Math.floor(Math.random() * 254)}`;
}

export const options = {
scenarios: {
capacity_ramp: {
executor: "ramping-arrival-rate",
startRate: 1000,
timeUnit: "1s",
preAllocatedVUs: 500,
maxVUs: 8000,
stages: [
{ target: 2500, duration: "15s" },
{ target: 4000, duration: "15s" },
{ target: 5500, duration: "15s" },
{ target: 7000, duration: "15s" },
{ target: 8500, duration: "15s" },
{ target: 10000, duration: "15s" },
{ target: 0, duration: "10s" },
],
},
},
// No abortOnFail here on purpose - let it run the full curve so the
// summary shows exactly where latency/errors bend, not just the first
// moment a threshold trips.
thresholds: {
http_req_duration: ["p(95)<100000"],
http_req_failed: ["rate<1.0"],
},
};

export default function () {
const payload = JSON.stringify({
serviceName: pick(SERVICES),
endpoint: pick(ENDPOINTS),
method: pick(METHODS),
statusCode: pick(STATUS_CODES),
latencyMs: Math.floor(15 + Math.random() * 250),
ipInIpv4: randomIp(),
userAgent: "k6-breaking-point/1.0",
});

const res = http.post(`${BASE_URL}/api/v1/ingest`, payload, {
headers: {
"Content-Type": "application/json",
"x-api-key": API_KEY,
},
timeout: "30s",
});

if (res.status === 429) rateLimited.add(1);
if (res.status >= 500) serverErrors.add(1);

check(res, {
"status is 202": (r) => r.status === 202,
});
}
74 changes: 74 additions & 0 deletions server/scripts/k6/capacity-ceiling-retest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import http from "k6/http";
import { check } from "k6";
import { Counter } from "k6/metrics";

// Same methodology as capacity-run-4..9 (strict p95<300ms, abortOnFail) but
// pushed to a much higher target now that CONSUMER_PREFETCH_COUNT and
// POSTGRES_MAX_POOL_SIZE have been raised from 20/10 to 50/50.
const BASE_URL = __ENV.BASE_URL || "http://localhost:5000";

Check failure on line 8 in server/scripts/k6/capacity-ceiling-retest.js

View workflow job for this annotation

GitHub Actions / Lint, typecheck, build

'__ENV' is not defined
const API_KEY = __ENV.API_KEY || "8197381c4f8cdeb988f1eeaa0efd492599041b27fe1aba2261464452834a31a2";

Check failure on line 9 in server/scripts/k6/capacity-ceiling-retest.js

View workflow job for this annotation

GitHub Actions / Lint, typecheck, build

'__ENV' is not defined

const rateLimited = new Counter("rate_limited_429");
const serverErrors = new Counter("server_errors_5xx");

const SERVICES = ["checkout-service", "auth-service", "payments-api", "inventory-service", "notifications-worker"];
const ENDPOINTS = ["/api/orders", "/api/orders/:id", "/api/users/login", "/api/users/:id", "/api/payments/charge"];
const METHODS = ["GET", "GET", "GET", "POST", "PUT"];
const STATUS_CODES = [200, 200, 200, 201, 204, 400, 404, 500];

function pick(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}

function randomIp() {
return `${1 + Math.floor(Math.random() * 254)}.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}.${1 + Math.floor(Math.random() * 254)}`;
}

export const options = {
scenarios: {
capacity_ramp: {
executor: "ramping-arrival-rate",
startRate: 500,
timeUnit: "1s",
preAllocatedVUs: 200,
maxVUs: 3000,
stages: [
{ target: 1500, duration: "15s" },
{ target: 3000, duration: "20s" },
{ target: 4500, duration: "20s" },
{ target: 6000, duration: "20s" },
{ target: 0, duration: "10s" },
],
},
},
thresholds: {
http_req_duration: [{ threshold: "p(95)<300", abortOnFail: true, delayAbortEval: "10s" }],
http_req_failed: [{ threshold: "rate<0.5", abortOnFail: false, delayAbortEval: "10s" }],
},
};

export default function () {
const payload = JSON.stringify({
serviceName: pick(SERVICES),
endpoint: pick(ENDPOINTS),
method: pick(METHODS),
statusCode: pick(STATUS_CODES),
latencyMs: Math.floor(15 + Math.random() * 250),
ipInIpv4: randomIp(),
userAgent: "k6-capacity-ceiling-retest/1.0",
});

const res = http.post(`${BASE_URL}/api/v1/ingest`, payload, {
headers: {
"Content-Type": "application/json",
"x-api-key": API_KEY,
},
});

if (res.status === 429) rateLimited.add(1);
if (res.status >= 500) serverErrors.add(1);

check(res, {
"status is 202": (r) => r.status === 202,
});
}
77 changes: 77 additions & 0 deletions server/scripts/k6/capacity-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import http from "k6/http";
import { check } from "k6";
import { Counter } from "k6/metrics";

// Finds the real throughput ceiling of POST /api/v1/ingest by ramping the
// arrival rate up in steps until errors/latency blow up. Requires the
// server-side rate limiter to be raised/disabled first (see RATE_LIMIT_MAX
// in .env) - otherwise this just measures the rate limiter, not the server.
const BASE_URL = __ENV.BASE_URL || "http://localhost:5000";

Check failure on line 9 in server/scripts/k6/capacity-test.js

View workflow job for this annotation

GitHub Actions / Lint, typecheck, build

'__ENV' is not defined
const API_KEY = __ENV.API_KEY || "8197381c4f8cdeb988f1eeaa0efd492599041b27fe1aba2261464452834a31a2";

Check failure on line 10 in server/scripts/k6/capacity-test.js

View workflow job for this annotation

GitHub Actions / Lint, typecheck, build

'__ENV' is not defined

const rateLimited = new Counter("rate_limited_429");
const serverErrors = new Counter("server_errors_5xx");

const SERVICES = ["checkout-service", "auth-service", "payments-api", "inventory-service", "notifications-worker"];
const ENDPOINTS = ["/api/orders", "/api/orders/:id", "/api/users/login", "/api/users/:id", "/api/payments/charge"];
const METHODS = ["GET", "GET", "GET", "POST", "PUT"];
const STATUS_CODES = [200, 200, 200, 201, 204, 400, 404, 500];

function pick(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}

function randomIp() {
return `${1 + Math.floor(Math.random() * 254)}.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}.${1 + Math.floor(Math.random() * 254)}`;
}

export const options = {
scenarios: {
capacity_ramp: {
executor: "ramping-arrival-rate",
startRate: 400,
timeUnit: "1s",
preAllocatedVUs: 200,
maxVUs: 3000,
stages: [
{ target: 800, duration: "10s" },
{ target: 1600, duration: "15s" },
{ target: 2400, duration: "15s" },
{ target: 0, duration: "10s" },
],
},
},
thresholds: {
// Abort as soon as things genuinely degrade, on EITHER errors or
// latency creeping up - a server that queues everything and still
// returns 202 after 30s isn't "handling" that rate.
http_req_failed: [{ threshold: "rate<0.95", abortOnFail: false, delayAbortEval: "10s" }],
http_req_duration: [{ threshold: "p(95)<5000", abortOnFail: false, delayAbortEval: "10s" }],
},
};

export default function () {
const payload = JSON.stringify({
serviceName: pick(SERVICES),
endpoint: pick(ENDPOINTS),
method: pick(METHODS),
statusCode: pick(STATUS_CODES),
latencyMs: Math.floor(15 + Math.random() * 250),
ipInIpv4: randomIp(),
userAgent: "k6-capacity-test/1.0",
});

const res = http.post(`${BASE_URL}/api/v1/ingest`, payload, {
headers: {
"Content-Type": "application/json",
"x-api-key": API_KEY,
},
});

if (res.status === 429) rateLimited.add(1);
if (res.status >= 500) serverErrors.add(1);

check(res, {
"status is 202": (r) => r.status === 202,
});
}
Loading
Loading