# Senior Backend & DevOps Work Sample: PR Review and Incident Triage Rubric

Published under CC-BY-4.0 for a 60-minute assessment window (30 minutes async review plus 30 minutes live walkthrough) targeting Senior Backend, Full-Stack, and DevOps or SRE engineers.

## Scenario Overview

A multi-tenant B2B SaaS API runs on AWS ECS Fargate behind an Application Load Balancer, backed by PostgreSQL 16 (RDS Multi-AZ) and Redis 7. After deploying release `v2.48.0` at 14:10 UTC, p99 latency on `POST /v1/exports` jumped from 320 ms to 11.4 seconds, RDS CPU climbed from 24% to 96%, and 4.2% of webhook deliveries began returning HTTP 502.

## Artifacts Provided to the Candidate

### 1. Pull Request Diff Excerpt (`v2.48.0`)

```sql
-- Migration 0194_add_export_audit.sql
ALTER TABLE export_jobs ADD COLUMN tenant_region VARCHAR(32) NOT NULL DEFAULT 'us-east';
CREATE INDEX idx_export_jobs_created_at ON export_jobs (created_at);
```

```typescript
// src/services/exportService.ts
export async function listPendingExports(tenantId: string) {
  const jobs = await db.query(
    `SELECT * FROM export_jobs WHERE tenant_id = $1 ORDER BY created_at DESC`,
    [tenantId]
  );
  // Enrich each job with latest audit event
  for (const job of jobs.rows) {
    const latest = await db.query(
      `SELECT payload FROM export_audit_events WHERE job_id = $1 ORDER BY id DESC LIMIT 1`,
      [job.id]
    );
    job.latestEvent = latest.rows[0]?.payload ?? null;
  }
  return jobs.rows;
}
```

```yaml
# infra/terraform/ecs_service.tf excerpt
deployment_minimum_healthy_percent = 100
deployment_maximum_percent         = 200
health_check_grace_period_seconds  = 15
# Connection pool per task: pg.Pool({ max: 40 })
# Task count scaled from 6 to 18 during alert storm
```

### 2. Incident Telemetry Snapshot (14:10 to 14:28 UTC)

- `rds_active_connections`: rose from 140 to 695 (instance `max_connections` = 700).
- `pg_stat_activity`: 380 queries waiting on `ClientRead` or `LWLock:BufferMapping`, plus blocked DDL lock waits during migration window.
- `alb_http_502_count`: spikes correlate with ECS task restarts triggered by failed health checks at 15s grace period.
- `export_jobs` table size: 42 million rows.

## Candidate Prompts (4 Tasks)

1. **Immediate Stabilization (First 10 Minutes):** Write the exact sequence of actions to restore p99 latency and eliminate 502s without causing data loss. State what you would roll back and what you would NOT roll back.
2. **Root-Cause Breakdown:** Identify all four distinct engineering defects visible in the SQL migration, TypeScript query code, and Terraform/pool config.
3. **Production-Safe Fix:** Rewrite the migration and query path so they deploy with zero table lock downtime and bounded database connections.
4. **Post-Incident Guardrails:** Specify 3 concrete CI/CD, observability, or Terraform controls that prevent this class of failure from merging again.

## Answer Key (Core Defects to Catch)

1. **Locking Index Creation on 42M-Row Table:** `CREATE INDEX` without `CONCURRENTLY` acquires a `SHARE` lock on `export_jobs`, blocking writes for minutes during deploy.
2. **N+1 Query Loop:** `listPendingExports` fetches unbounded rows (`SELECT *` with no `LIMIT` or status filter) and runs one query per row against `export_audit_events`.
3. **Connection Pool Exhaustion Math:** 18 ECS tasks x 40 `pg.Pool` connections = 720 potential connections, exceeding the RDS instance `max_connections` limit of 700 and starving new queries.
4. **Missing Composite Index & Aggressive Health Check:** Queries filter by `tenant_id` and sort by `created_at DESC`, so an index on `(created_at)` alone misses the predicate. A composite `(tenant_id, created_at DESC)` index is required, and a 15-second `health_check_grace_period_seconds` causes churn loops during slow startup or DB saturation.

## Anchored Evaluation Rubric

| Dimension | Strong Signal | Weak Signal |
| --- | --- | --- |
| Incident Containment | Stabilizes DB connections first (caps autoscaling or rolls back app image `v2.48.0`), keeps schema column intact rather than running destructive `DROP COLUMN` mid-incident. | Suggests `DROP COLUMN` or rebooting RDS immediately without checking active locks and connection math. |
| SQL & Query Diagnosis | Catches both the missing `CONCURRENTLY` lock hazard and the unbounded N+1 loop, replacing them with `LIMIT`, `WHERE status = 'pending'`, and a `LATERAL` join or `DISTINCT ON`. | Mentions "optimize SQL" generically without spotting the N+1 loop or lock mode. |
| Infrastructure Math | Computes `18 x 40 = 720 > 700` explicitly and recommends PgBouncer or RDS Proxy, a lower per-task pool cap, and a realistic health-check grace period. | Misses connection pool math completely or only recommends vertically upsizing RDS. |
| Guardrails & Tradeoffs | Proposes SQL migration linter (`squawk` or `strong_migrations`), staging load check, and connection saturation alerting at 75% of `max_connections`. | Lists generic advice like "add more unit tests" or "do better code reviews." |
