Topic 3 of 5 · 5 min read
Concurrency, security and performance hot spots
Goal: Flag the parts of a change that need hand-written or line-by-line reviewed code because of concurrency, security or performance.
Some code fails in ways a quick read and a green test run will not show: concurrency, authorisation, cryptography and hot paths. Write these by hand, or review them line by line with tooling, never skim them. AI output in these areas looks right because the happy path is right.
Check yourself · 1 of 3
Which change deserves line-by-line review rather than a skim?
Why it matters
A Stanford user study found that participants with an AI assistant wrote less secure code than those without one, and were more confident that it was secure. That mix is the danger: the code is flawed and the author feels safe. Race conditions and slow loops share the trait. Tests pass on a laptop with one user and break in production with a thousand.
The four hot spots
- Concurrency. Shared state, check-then-act sequences, locks, queues, retries. Ask "what if two requests run this at the same moment?"
- Authorisation. Every route that reads or changes data: is the caller allowed to touch this specific record, not just logged in? Missing ownership checks are a classic.
- Cryptography and secrets. Hashing, tokens, randomness, comparing secrets. Use the platform's vetted library and never let the model invent a scheme.
- Hot paths. Code that runs per request or per row: queries inside loops, work repeated in each iteration, unbounded memory.
How to handle them
- Write the core of the risky part yourself, or read generated code one line at a time, saying what each line guarantees.
- Add tests that attack it: two parallel calls, a second user's id, a huge input.
- Use tools that humans cannot match by reading: a race detector, a static analyser, a profiler, a load test.
- Keep these changes small so review stays careful. Google's engineering guidance notes that small changes are reviewed faster and more thoroughly.
Before and after
# Generated: looks fine, passes a single-user test
def withdraw(account_id, amount):
balance = db.get_balance(account_id)
if balance >= amount:
db.set_balance(account_id, balance - amount)
Two simultaneous calls can both read 100 and both withdraw 100. The fix moves the check into one atomic step:
def withdraw(account_id, amount):
updated = db.execute(
"UPDATE accounts SET balance = balance - %s "
"WHERE id = %s AND balance >= %s",
(amount, account_id, amount),
)
return updated == 1
What goes wrong
- Trusting a passing test. A single-threaded test cannot find a race.
- Checking login but not ownership. Any user can fetch another user's record by changing an id.
- Premature tuning. Measure first. Only hot paths need hand-tuning.
Examples
Missing ownership check
app.get("/invoices/:id", requireLogin, async (req, res) => {
const invoice = await db.invoices.findById(req.params.id);
res.json(invoice);
});
Any logged-in user can read any invoice. The fix is a lookup that includes the owner: findOne({ id, userId: req.user.id }).
Query in a loop
for order in orders:
customer = db.get_customer(order.customer_id) # one query per order
With 5,000 orders that is 5,000 queries. Fetch the customers in one query and look them up in a dictionary.
Key points
- Concurrency, authorisation, cryptography and hot paths fail in ways green tests and quick reads miss.
- Ask what happens when two requests run the same code at the same moment.
- Check that each route verifies access to the specific record, not just that the user is logged in.
- Use vetted libraries for crypto and never accept a model-invented scheme.
- Add attacking tests and tooling such as race detectors and profilers, then keep these diffs small.
Common mistakes
- Skimming auth and concurrency code because it reads cleanly.
- Letting a model choose a hashing or token scheme.
- Relying on single-user tests for code that runs in parallel.
- Tuning code before measuring where the time goes.
Check yourself · 2 of 3
A generated endpoint returns an invoice by id, and only checks that the caller is logged in. What is missing?
Check yourself · 3 of 3
Why can a passing unit test not rule out a race condition?
Practice items, not counted towards your mastery.
Start here
- Measuring the Impact of Early-2025 AI on Experienced Open-Source Developer Productivity (opens in a new tab)PaperFree
A randomised trial where experienced developers were slower with AI yet believed they were faster.
- How AI assistance impacts the formation of coding skills (Anthropic) (opens in a new tab)ArticleFree
A vendor's own study of how delegating code to AI affects learning a new library.
- Do Users Write More Insecure Code with AI Assistants? (opens in a new tab)PaperFree
A user study where AI-assisted participants wrote less secure code and felt more sure of it.
More for this topic
- Secure Code Review Cheat Sheet (OWASP) (opens in a new tab)ArticleFree
A checklist for the auth, input and crypto code that deserves line-by-line review.
- Data Race Detector (Go) (opens in a new tab)DocsFree
Shows how race conditions are found by tooling rather than by reading, with real examples.