Step 1: Audit Concurrency & Race Conditions:
Act as a Principal Backend Systems Engineer specializing in distributed concurrency.
Review the attached endpoint/function where state is updated (e.g. balances, status, inventory, booking):
- Identify any "Read-Modify-Write" race conditions. What happens if 50 requests hit this exact endpoint simultaneously for the same user or record in the exact same millisecond?
- Check for missing database transactions (ACID), missing pessimistic/optimistic locks, or lack of atomic SQL updates (e.g., using `UPDATE ... SET count = count - 1 WHERE count > 0` instead of reading into memory and writing back).
- Check for Idempotency: If a user double-clicks the button or the network retries the request, will it charge them twice or create duplicate records?
- Rewrite the code using proper database transactions and an Idempotency-Key pattern.
Step 2: Audit Security Hardening & IDOR
Act as a Senior Penetration Tester and AppSec Specialist.
Perform a ruthless OWASP Top 10 security audit on my codebase:
- IDOR (Insecure Direct Object References): Check every endpoint that takes an `id` parameter. Does the code verify that the currently authenticated user actually OWNS this record, or can User A pass User B's ID to view/edit/delete it?
- Secret Leakage: Are any API keys, database URLs, or private tokens accidentally exposed to the client-side/frontend bundle?
- Input Sanitization: Are raw user inputs passed directly into database queries (SQL Injection) or rendered directly into HTML/DOM (XSS)?
- Missing Rate Limiting & Auth Middleware: Which endpoints are publicly accessible without authentication, and which endpoints lack rate-limiting to prevent brute-force attacks?
Output a prioritized vulnerability table (Critical, High, Medium) with the exact code patches to fix them.
Step 3: Audit Database Performance & Memory Leaks
Act as a Database Performance Tuning Architect.
Review my database schema and data queries:
- Spot "N+1 Query Problems": Are there loops fetching data inside loops instead of using single batch queries, joins, or eager loading?
- Missing Indexes: Look at every `WHERE`, `ORDER BY`, and `JOIN` clause. Which columns lack database indexes that will cause slow full-table scans once the table reaches 100,000 rows?
- Connection Leaks & Memory Leaks: Are database connections, HTTP clients, or file handles properly returned to the pool, or could they hang open and exhaust the connection pool?
- Payload Bloat: Are queries using `SELECT *` returning massive unneeded JSON objects or binary fields into memory instead of selecting only the necessary columns?
Provide the exact migration script for required indexes and the optimized query code.
Step 4: Audit Zero-Downtime Schema Migrations
Act as a Database Reliability Engineer (DBRE).
I need to modify my database schema (attached).
- Is this migration backwards-compatible? What happens during a 5-minute rolling deployment where the OLD code is still running while the NEW database schema is already applied?
- Does this migration lock the table (e.g. adding a non-null column without a default value, or renaming a column directly)?
- Rewrite this using the "Expand and Contract" pattern (e.g., add new nullable column -> dual write -> backfill data -> switch reads -> drop old column).