
Hai request cùng sửa một dữ liệuTwo requests updating the same data
Flash sale 0 giờThe midnight flash sale.0 giờ ngày 11/11, 8.000 người cùng bấm mua 500 tai nghe giá 99.000 đồng trên một sàn thương mại điện tử. Request rơi đều vào 12 instance API sau load balancer.At midnight on 11/11, 8,000 people tap buy on 500 headphones priced at 99,000 VND. Requests spread evenly across 12 API instances behind the load balancer.
Hai người dùng bấm cùng lúc: load balancer đưa họ tới hai instance API khác nhau, nhưng cả hai cùng ghi vào một dòng trong PostgreSQL. Khoá trong bộ nhớ của instance này không thấy instance kia, nên chỉ database mới phân xử được ai thắng. Chạy hai lần sửa hồ sơ để thấy version chặn ghi đè, rồi so “Read-modify-write mất lượt thích” với “Cộng dồn nguyên tử” để thấy cùng một tính năng mà kết quả khác hẳn.Two users click at the same moment: the load balancer sends them to two different API instances, yet both write the same PostgreSQL row. One instance's in-memory lock cannot see the other, so only the database can decide who wins. Play the two profile edits to see a version stop the overwrite, then compare “Read-modify-write loses a like” with “Atomic increment” to see the same feature end very differently.
1A và B cùng mở form sửa hồ sơ 42 từ hai thiết bị. Cả hai nhận version 7 và giữ nó trong form.A and B both open the edit form for profile 42. Both receive version 7 and keep it in the form.
Đọc giá trị, tính trong code, rồi ghi lại là ba bước tách rời. Hai request xen kẽ nhau sẽ ghi cùng kết quả và một thay đổi biến mất. Với bộ đếm, chuyển phép tính vào SQL: SET like_count = like_count + 1.Reading a value, computing in code and writing it back are three separate steps. Two interleaved requests write the same result and one change disappears. For counters, move the arithmetic into SQL: SET like_count = like_count + 1.
“Kiểm tra email chưa tồn tại rồi INSERT” hay “kiểm tra còn hàng rồi trừ kho” đều có khe hở giữa hai bước. Gộp điều kiện vào câu ghi (UPDATE stock SET qty = qty - 1 WHERE id = ? AND qty > 0, rồi kiểm tra số dòng) hoặc để unique constraint quyết định.“Check the email is free, then INSERT” or “check stock, then decrement” both leave a gap between the steps. Fold the condition into the write (UPDATE stock SET qty = qty - 1 WHERE id = ? AND qty > 0, then check the row count) or let a unique constraint decide.
Người dùng mở form vài phút rồi mới lưu, nên không thể giữ khoá DB suốt thời gian đó. Thêm cột version: client gửi lại version đã đọc, UPDATE … WHERE id = ? AND version = ?; 0 dòng nghĩa là có người sửa trước, trả 409. JPA làm việc này với @Version.A user keeps a form open for minutes before saving, so you cannot hold a DB lock that long. Add a version column: the client sends back the version it read, UPDATE … WHERE id = ? AND version = ?; zero rows means someone saved first, so return 409. JPA does this with @Version.
Khi nhiều giao dịch ngắn cùng sửa một dòng và cần đọc trước khi quyết định (chuyển tiền giữa hai ví), SELECT … FOR UPDATE khoá dòng để các giao dịch xếp hàng. Giữ transaction ngắn, khoá theo thứ tự cố định (id nhỏ trước) để tránh deadlock, đặt lock_timeout.When many short transactions modify one row and must read before deciding (moving money between two wallets), SELECT … FOR UPDATE locks the row so they queue. Keep transactions short, lock in a fixed order (lower id first) to avoid deadlocks, and set lock_timeout.
PostgreSQL mặc định READ COMMITTED, không chặn lost update. REPEATABLE READ và SERIALIZABLE báo lỗi 40001 khi phát hiện xung đột, nên ứng dụng phải bắt lỗi và chạy lại cả transaction. MySQL InnoDB ở REPEATABLE READ không báo lỗi lost update theo cách này.PostgreSQL defaults to READ COMMITTED, which does not prevent lost updates. REPEATABLE READ and SERIALIZABLE raise error 40001 on a conflict, so the application must catch it and rerun the whole transaction. MySQL InnoDB at REPEATABLE READ does not report lost updates this way.
Cache hay bộ đếm dùng chung giữa các thread cũng bị race. Java: synchronized, AtomicLong, ConcurrentHashMap.merge. Go: sync.Mutex, sync/atomic, hoặc để một goroutine sở hữu dữ liệu và nhận lệnh qua channel. Chạy go test -race trong CI. Các khoá này chỉ có tác dụng trong một instance.Caches or counters shared between threads race too. Java: synchronized, AtomicLong, ConcurrentHashMap.merge. Go: sync.Mutex, sync/atomic, or one goroutine owning the data and taking commands over a channel. Run go test -race in CI. These locks only work within a single instance.
| BảngTable | Cột chínhKey columns | Ghi chúNotes |
|---|---|---|
user_profile | id, display_name, bio, avatar_url, version, updated_at | version tăng mỗi lần UPDATE; client gửi lại version đã đọcversion grows on every UPDATE; the client sends back the version it read |
post | id, author_id, body, like_count, created_at | like_count chỉ thay đổi bằng like_count + 1 hoặc - 1 trong SQLlike_count only changes through like_count + 1 or - 1 in SQL |
post_like | post_id, user_id, created_at | primary key (post_id, user_id): lớp chặn cuối cùng cho thích trùngprimary key (post_id, user_id): the last line of defence against double likes |
wallet | id, user_id, balance, version | check (balance >= 0); chuyển tiền dùng SELECT … FOR UPDATE theo thứ tự idcheck (balance >= 0); transfers use SELECT … FOR UPDATE in id order |
ProfileService, ConflictAdvice và PostRepository (Go: Repo) là code chạy trong cả hai hộp API instance 1 và API instance 2 ở sơ đồ: update dùng optimistic locking và ConflictAdvice trả 409 khi version lệch, like dùng unique constraint cộng với incrementLikes nguyên tử, để hộp PostgreSQL phân xử. ViewCounter của Go là trạng thái nằm trong bộ nhớ một instance, nên chỉ cần sync.Mutex.ProfileService, ConflictAdvice and PostRepository (Go: Repo) run in both the API instance 1 and API instance 2 boxes in the diagram: update uses optimistic locking and ConflictAdvice returns 409 on a version mismatch, like uses a unique constraint plus an atomic incrementLikes, so the PostgreSQL box arbitrates. Go's ViewCounter is state inside one instance's memory, so a sync.Mutex is enough.
package com.example.profile;
import lombok.RequiredArgsConstructor;
import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@RequiredArgsConstructor
public class ProfileService {
private final UserProfileRepository profiles;
private final PostRepository posts;
private final PostLikeRepository likes;
@Transactional
public UserProfile update(long id, ProfileEdit edit) {
UserProfile p = profiles.findById(id).orElseThrow();
if (p.getVersion() != edit.version()) {
throw new ObjectOptimisticLockingFailureException(UserProfile.class, id);
}
p.setDisplayName(edit.displayName());
p.setBio(edit.bio());
return p; // UPDATE … WHERE id = ? AND version = ? at commit
}
@Transactional
public void like(long postId, long userId) {
if (likes.insertIgnore(postId, userId) == 0) return; // already liked
posts.incrementLikes(postId); // no read-modify-write
}
}package main
import (
"context"
"errors"
"sync"
"github.com/jackc/pgx/v5"
)
var ErrConflict = errors.New("profile was modified by someone else")
func (r *Repo) UpdateProfile(ctx context.Context, e ProfileEdit) error {
tag, err := r.db.Exec(ctx, `
UPDATE user_profile
SET display_name = $1, bio = $2, version = version + 1
WHERE id = $3 AND version = $4`,
e.DisplayName, e.Bio, e.ID, e.Version)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return ErrConflict // handler maps it to 409
}
return nil
}
func (r *Repo) Like(ctx context.Context, postID, userID int64) error {
return pgx.BeginFunc(ctx, r.db, func(tx pgx.Tx) error {
tag, err := tx.Exec(ctx,
"INSERT INTO post_like (post_id, user_id) VALUES ($1, $2) ON CONFLICT DO NOTHING",
postID, userID)
if err != nil || tag.RowsAffected() == 0 {
return err // nil: already liked
}
_, err = tx.Exec(ctx,
"UPDATE post SET like_count = like_count + 1 WHERE id = $1", postID)
return err
})
}
// In-process shared state needs a lock too; run go test -race in CI.
type ViewCounter struct {
mu sync.Mutex
n map[int64]int
}
func (c *ViewCounter) Inc(postID int64) {
c.mu.Lock()
defer c.mu.Unlock()
c.n[postID]++
}| Optimistic (cột version)Optimistic (version column) | Pessimistic (SELECT … FOR UPDATE)Pessimistic (SELECT … FOR UPDATE) | |
|---|---|---|
| Hợp vớiGood for | Ít xung đột, người dùng sửa qua form trong thời gian dàiRare conflicts, users editing through a form over a long time | Dòng tranh chấp cao, transaction ngắn cần đọc rồi quyết địnhHot rows, short transactions that read before deciding |
| Bên thua nhận gìWhat the loser gets | 409 hoặc lỗi, phải tải lại hoặc thử lạiA 409 or an error; reload or retry | Chờ khoá rồi chạy tiếp với dữ liệu mớiWaits for the lock, then proceeds on fresh data |
| Chi phíCost | Không giữ khoá; nhiều xung đột thì tốn công thử lạiNo locks held; many conflicts mean wasted retries | Giữ khoá, giảm song song, có thể deadlock nếu khoá sai thứ tựHolds locks, reduces parallelism, can deadlock if locks are taken out of order |
| Ví dụExample | Sửa hồ sơ, sửa bài viết, cấu hìnhProfile edits, article edits, settings | Chuyển tiền giữa hai ví, giữ chỗ ghế cuối cùngMoving money between wallets, reserving the last seat |
synchronized trong Java hay sync.Mutex trong Go chỉ khoá trong một process; chạy 12 pod là có 12 khoá độc lập. Bài kiểm thử trên laptop một instance vẫn xanh nên lỗi chỉ lộ ra lúc cao điểm. Đưa điều phối xuống DB bằng UPDATE có điều kiện, version hoặc SELECT FOR UPDATE, và chạy test tải với ít nhất 2 instance.Java synchronized or Go sync.Mutex only locks inside one process; 12 pods means 12 independent locks. Tests on a single-instance laptop stay green, so the bug only shows at peak. Move coordination into the database with a guarded UPDATE, a version or SELECT FOR UPDATE, and load-test with at least 2 instances.
Bắt OptimisticLockException rồi gửi lại cùng payload với version mới chính là ghi đè thay đổi của người khác, chỉ chậm hơn một nhịp. Với form người dùng nhập, trả 409 để client tải bản mới và cho người dùng quyết định. Chỉ retry tự động khi thao tác tự tính lại được từ dữ liệu mới, và giới hạn 3 lần có jitter.Catching OptimisticLockException and resending the same payload with the new version just overwrites someone else's change a moment later. For user-entered forms, return 409 so the client reloads and the user decides. Retry automatically only when the operation can be recomputed from fresh data, capped at 3 attempts with jitter.
SELECT xem đã like chưa rồi mới INSERT vẫn để lọt hai request cùng thấy chưa có. Tạo unique index trên (post_id, user_id) và dùng INSERT ... ON CONFLICT DO NOTHING, chỉ tăng bộ đếm khi có dòng được chèn. Giữ INSERT và UPDATE bộ đếm trong cùng transaction để không lệch nhau.SELECTing whether a like exists before INSERTing still lets two concurrent requests both see nothing. Create a unique index on (post_id, user_id), use INSERT ... ON CONFLICT DO NOTHING, and bump the counter only when a row was inserted. Keep the INSERT and the counter UPDATE in one transaction so they never drift.
UPDATE nguyên tử đúng nhưng mọi request cùng xếp hàng chờ khoá một dòng; bài viết viral 5.000 like/giây làm p99 lên hàng giây và cạn connection pool. Theo dõi lock wait time trong pg_stat_activity, đặt lock_timeout khoảng 2 giây. Với bộ đếm cực nóng, chia thành 16 dòng con rồi cộng lại khi đọc, hoặc gom trong Redis INCR và flush định kỳ.An atomic UPDATE is correct, but every request queues for the lock on one row; a viral post at 5,000 likes a second pushes p99 into seconds and drains the connection pool. Watch lock wait time in pg_stat_activity and set lock_timeout around 2 seconds. For extremely hot counters, split into 16 sub-rows summed on read, or batch with Redis INCR and flush periodically.
Mỗi pattern mở bài học tương ứng trong menu Pattern.Each one opens its lesson in the Patterns menu.
synchronized chỉ khoá trong một JVM. Request đi tới instance khác không thấy khoá đó. Điều phối phải nằm ở nơi dùng chung: DB (cộng dồn nguyên tử, version, FOR UPDATE) hoặc khoá phân tán.synchronized only locks within one JVM; a request on another instance never sees it. Coordination must live in the shared place: the database (atomic updates, versions, FOR UPDATE) or a distributed lock.
Optimistic khi xung đột hiếm và thời gian giữa đọc và ghi dài (form). Pessimistic khi dòng bị tranh chấp nhiều và transaction ngắn, vì thử lại liên tục sẽ tốn hơn chờ khoá.Optimistic when conflicts are rare and the gap between read and write is long (forms). Pessimistic when a row is heavily contended and transactions are short, because constant retries cost more than waiting for a lock.
Nó ngăn bất thường trong DB, nhưng đổi lại là lỗi 40001 khi xung đột. Ứng dụng phải chạy lại cả transaction, và mọi side effect bên ngoài (gửi email, gọi API) phải idempotent hoặc làm sau commit. Thông lượng cũng giảm khi tranh chấp cao.It prevents anomalies inside the DB, at the price of 40001 errors on conflict. The application must rerun the whole transaction, and external side effects (emails, API calls) must be idempotent or happen after commit. Throughput also drops under contention.
Kiểm tra trong code là check-then-act, luôn có khe hở giữa hai request. Unique constraint là lớp chặn cuối cùng do DB đảm bảo; code chỉ cần bắt lỗi vi phạm hoặc dùng ON CONFLICT.A check in code is check-then-act and always leaves a gap between two requests. The unique constraint is the last line of defence enforced by the DB; the code just handles the violation or uses ON CONFLICT.
Phím 1–4 để chọn đáp án, Enter để kiểm tra.Press 1–4 to choose an answer, Enter to check.
Ôn tập ngắt quãng (spaced repetition): thẻ được lặp lại sau 1 → 3 → 7 → 14 → 30 ngày để củng cố trí nhớ dài hạn.Spaced repetition: cards are repeated after 1 → 3 → 7 → 14 → 30 days to build long-term memory.