跪拜 Guibai
← All articles
Frontend · Backend · AI Programming

From Frontend to Full-Stack: The 12 Backend Thinking Patterns AI Won't Tell You

By 天天鸭 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

AI-generated backend code looks correct on the surface but regularly misses the hard parts—concurrency, idempotency, security, scaling—that cause real production incidents. This guide gives frontend developers a concrete framework to review and harden AI-written code before it goes live.

Summary

The article starts from the premise that AI has dramatically lowered the barrier to writing backend code, so frontend developers inevitably become full-stack. But understanding syntax isn't enough—what separates experienced backends is the ability to see what can go wrong in real environments. Using a simple "create order" API generated by AI (v0), the author walks through 12 thinking patterns organized into three parts: data correctness (modeling, security, idempotency, concurrency, transactions), traffic capacity (indexes, caching, async, rate limiting), and resilience (fault tolerance, observability, release compatibility).

Each pattern demystifies a backend concept with frontend analogies—like comparing state refactoring to table structure design, button-disable to idempotency, search box races to concurrency, Promise.all to transactions, Map to indexes, SWR to cache, Web Workers to async, debounce to rate limiting, Error Boundary to fault tolerance, console/Sentry to observability, and npm versioning to API compatibility. The article also provides concrete verification methods, SQL examples, and a final v3 code snippet that incorporates all 12 patterns.

A key message: AI writes v0 in seconds, and v3's code is also mostly AI-written—the difference is the developer's ability to review and decide. The article closes with a review checklist and a learning path, distinguishing what you must understand from what you can look up later.

Takeaways
— AI writes the simplest possible code based on the prompt; it will not proactively add idempotency, concurrency control, auth checks, indexes, or timeouts unless you specify them.
— Use unique indexes in the database as the ultimate safety net for idempotency: a unique index on the request ID is the only thing that catches simultaneous duplicate requests.
— Deduct stock with a single atomic SQL statement `UPDATE product SET stock = stock - ? WHERE id = ? AND stock >= ?` instead of read-check-write.
— `@Transactional` only works when called through the Spring proxy; internal self-calls, private methods, swallowed exceptions, and checked exceptions (without rollbackFor) silently disable it.
— Always set both connect and read timeouts on external calls—a slow dependency can exhaust the thread pool and bring down the entire service.
— For cache updates, update the database first, then delete the cache; this minimizes the window of inconsistency, and cache should always have a TTL.
— Store order details as a snapshot (product name, price at purchase time) so historical orders don't change when prices change later.
— Rate limiting is not a luxury; without it, a single script can exhaust database connections and block all users.
— Never trust frontend-supplied user IDs or prices; user ID must come from the token, price from the database.
— A production-ready order API requires unique indexes on request ID, order number, payment transaction ID, and points transaction type, plus an audit log for price changes.
Conclusions

The core difference between AI-generated and production-grade code isn't the language or framework—it's the developer's ability to anticipate failure scenarios that the AI was never told about.

Many backend concepts are just the same problems frontend developers already solve, but at a different scale and with different consequences: one user vs. all users, page state vs. years of data, console vs. logs.

The author's v3 code shows that after applying the 12 patterns, the code isn't drastically more complex—it just adds a few conditions, unique indexes, and a transactional boundary, which makes all the difference.

The article's emphasis on "verification methods" (e.g., sending concurrent requests, stopping dependencies, checking EXPLAIN) is a practical way to force developers to internalize each pattern rather than just read about it.

The advice to "let AI write tests but define test cases yourself" reflects a broader truth: AI is good at generating code that matches your intent, but you need the intent—the expected behavior—in the first place.

Concepts & terms
Idempotency
The property that calling the same operation multiple times produces the same result as calling it once, with no additional side effects. E.g., an order API should return the same order number for duplicate submissions instead of creating a second order.
Race condition
A situation where the outcome depends on the order of events that happen at the same time. In backend, two requests reading a stock value, both checking "enough," and both deducting before the other writes back, leads to overselling.
Check-then-modify gap
The window between a read (check) and a write (modify) during which another operation can interleave, causing lost updates or overselling. Solutions include atomic SQL updates, optimistic/pessimistic locks, or unique indexes.
Atomic update
A single database operation that combines a condition and an action in one step, e.g., `UPDATE product SET stock = stock - ? WHERE id = ? AND stock >= ?`. The database locks the row during the update, preventing interleaving.
Unique index
A database index that ensures all values in a column (or combination of columns) are distinct. Used as a hard guarantee for idempotency: attempting to insert a second row with the same request ID fails at the database level.
Database transaction
A sequence of database operations that either all succeed or all fail (rollback), maintaining atomicity. In Spring, `@Transactional` manages this via a proxy; operations that can't be rolled back (like sending an SMS) should be outside the transaction.
Local message table
A pattern for reliable async messaging: within the same database transaction that creates an order, also insert a row into a "pending messages" table. A scheduled task then sends those messages to the MQ and marks them as sent, so the message is never lost.
Keyset pagination
Pagination that uses the last seen ID as a cursor (`WHERE id < ? ORDER BY id DESC LIMIT 20`) instead of `LIMIT offset, rows`, which must scan and discard all preceding rows. Scales to deep pages but doesn't support jumping to arbitrary pages.
Cache Aside
A caching strategy: on read, serve from cache, miss → load from DB and populate cache; on write, update DB then delete the cache (or invalidate it). Deleting instead of updating prevents stale values from concurrent writes.
Circuit breaker
A fault tolerance pattern that detects a failing dependency (e.g., failure rate above a threshold) and temporarily stops calling it, failing fast instead of wasting threads. After a cooldown, it allows trial requests to test recovery.
Trace ID (traceId)
A unique identifier generated at the start of a request and propagated through all logs and downstream service calls via headers. Search by traceId to see the entire path of a single request, even across multiple services.
Graceful shutdown
A deployment technique that stops a server from accepting new requests but allows in-flight requests to complete before the process exits, avoiding breaking user operations during a rolling update.
From the discussion
Featured comments
神小渣

Full-stack is fine, but I don't want to write Java (crying). Which of Node, Go, Rust isn't better than Java?

天天鸭

So what you learn is the thinking and considerations, which are useful no matter what backend you write.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗