SRE & Performance Testing··2 min read

Automated k6 Load Testing in CI/CD: Catching P95 Latency and Lock Regressions Before Production

Shafikul Islam

Shafikul Islam

Backend & Cloud Infrastructure Engineer · Infrastructure & Cloud Team Lead

Automated k6 Load Testing in CI/CD: Catching P95 Latency and Lock Regressions Before Production

Why Functional Tests Never Catch Concurrency or Cardinality Bugs

A SQL query missing a composite index executes in 0.4 milliseconds when your unit test database contains 10 fixture rows. The exact same query takes 3.2 seconds when table cardinality reaches 10 million rows or when 200 concurrent virtual users contend for the same row lock.

To catch tail-latency regressions before code reaches production, we treat k6 performance thresholds as binary pass/fail CI gates.


1. Declarative SLO Thresholds in k6

Every critical API route has a reproducible k6 scenario that ramps up concurrent virtual users (ramping-arrival-rate) and asserts strict p(95) and p(99) latency bounds:

import http from "k6/http";
import { check } from "k6";

export const options = {
  scenarios: {
    flash_burst: {
      executor: "ramping-arrival-rate",
      startRate: 100,
      timeUnit: "1s",
      preAllocatedVUs: 200,
      maxVUs: 1000,
      stages: [
        { target: 1500, duration: "30s" },
        { target: 1500, duration: "60s" },
        { target: 0, duration: "15s" },
      ],
    },
  },
  thresholds: {
    http_req_failed: ["rate==0.00"],
    http_req_duration: ["p(95)<25", "p(99)<60"],
  },
};

export default function () {
  const res = http.post(
    `${__ENV.BASE_URL}/api/v1/reservations`,
    JSON.stringify({ sku_id: "sku_hot_01", qty: 1 }),
    { headers: { "Content-Type": "application/json" } }
  );
  check(res, {
    "status is 200 or 409 sold out": (r) => r.status === 200 || r.status === 409,
  });
}

If a PR introduces an unindexed join or widens a database transaction lock that pushes p(95) above 25ms, k6 exits with code 99 and blocks the release automatically.

Infrastructure & PostgreSQL Advisory

Scaling Infrastructure or Diagnosing P95 Latency?

I help engineering teams diagnose PostgreSQL indexing bottlenecks, concurrency limits, and K3s/Docker deployments. Book a 20-minute call with me to review your setup.

Related Technical Articles