9148c358d0
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를 Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류. - 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로 자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거, 동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거. - 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming, Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business, Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로, 나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는 title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백). 원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지. - 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서. - 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는 지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지. - Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경. - 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
6.0 KiB
6.0 KiB
id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, created_at, updated_at, last_reinforced, review_reason, merge_history, tags, raw_sources, tech_stack, applied_in
| id | title | category | status | canonical_id | aliases | duplicate_of | source_trust_level | confidence_score | verification_status | created_at | updated_at | last_reinforced | review_reason | merge_history | tags | raw_sources | tech_stack | applied_in | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| optimistic-concurrency-control | 낙관적 동시성 제어 (Optimistic Concurrency Control) | Coding | draft | optimistic-concurrency-control |
|
null | B | 0.9 | conceptual | 2026-05-09 | 2026-05-09 | 2026-05-09 |
|
|
|
낙관적 동시성 제어
충돌은 드물다고 가정하고 락 없이 진행. 충돌이 발생하면 버전 비교로 감지하고 재시도하거나 사용자에게 알린다. 비관적 락보다 throughput 높지만 충돌이 잦으면 retry 폭주.
📖 핵심 개념
두 사용자가 같은 레코드를 동시에 수정할 때:
- 비관적(pessimistic) 락: 한 명이 잠금 → 다른 명은 대기. throughput 낮음, deadlock 위험.
- 낙관적(optimistic): 둘 다 진행, 마지막 commit 시점에 "내가 읽은 버전이 아직도 유효한가?" 확인. 깨졌으면 reject.
낙관적 락은 다음 둘 중 하나로 구현:
- Version 컬럼 (정수 또는 timestamp): UPDATE 시
WHERE version = ?조건. 변경 시 version+1. - HTTP ETag / If-Match: 서버가 ETag 발급 → 클라이언트가 PUT 시
If-Match헤더로 보냄.
💻 코드 패턴
1. Version 컬럼 (SQL)
ALTER TABLE orders ADD COLUMN version INT NOT NULL DEFAULT 0;
async function updateShipping(orderId: string, addr: Address, expectedVersion: number) {
const result = await db.query(
`UPDATE orders
SET shipping = $1, version = version + 1
WHERE id = $2 AND version = $3`,
[addr, orderId, expectedVersion]
);
if (result.rowCount === 0) {
// 누군가 이미 수정했거나 주문이 없음
throw new ConflictError('Concurrent update detected — re-read and try again');
}
}
2. HTTP ETag
// GET — ETag 발급
app.get('/api/orders/:id', async (req, res) => {
const order = await db.findOrder(req.params.id);
res.setHeader('ETag', `"${order.version}"`);
res.json(order);
});
// PUT — If-Match 검증
app.put('/api/orders/:id', async (req, res) => {
const ifMatch = req.header('If-Match');
if (!ifMatch) return res.status(428).json({ error: 'If-Match required' });
const expected = parseInt(ifMatch.replace(/"/g, ''), 10);
try {
await updateShipping(req.params.id, req.body.shipping, expected);
res.status(204).end();
} catch (e) {
if (e instanceof ConflictError) return res.status(412).json({ error: 'Version mismatch' });
throw e;
}
});
3. 자동 재시도 wrapper
async function retryOnConflict<T>(
op: () => Promise<T>,
{ maxAttempts = 3, backoffMs = 50 }: { maxAttempts?: number; backoffMs?: number } = {}
): Promise<T> {
let lastErr: unknown;
for (let i = 0; i < maxAttempts; i++) {
try {
return await op();
} catch (e) {
if (!(e instanceof ConflictError)) throw e;
lastErr = e;
await new Promise(r => setTimeout(r, backoffMs * Math.pow(2, i)));
}
}
throw lastErr;
}
op() 안에서 항상 다시 read → 변경 → write 흐름이어야 한다. 같은 expectedVersion 으로 재시도하면 영원히 실패.
4. Document DB (MongoDB) — findOneAndUpdate with filter
const result = await orders.findOneAndUpdate(
{ _id: orderId, version: expectedVersion },
{ $set: { shipping: addr }, $inc: { version: 1 } },
{ returnDocument: 'after' }
);
if (!result.value) throw new ConflictError('version mismatch');
🤔 의사결정 기준
| 상황 | OCC 적합 | 비관적 락 적합 |
|---|---|---|
| 충돌 빈도 < 5% | ✅ | ❌ |
| 충돌 빈도 > 30% | ❌ (retry 폭주) | ✅ |
| 트랜잭션이 짧음 | ✅ | — |
| 트랜잭션이 길고 사용자 대기 중 | ✅ (사용자에게 충돌 알림) | ❌ (deadlock) |
| 분산 환경 (DB 여러 대) | ✅ | 어려움 |
| 잔액 / 재고 같은 strict invariant | OCC 가능하지만 SELECT FOR UPDATE 또는 UPDATE ... WHERE balance >= ? 사용 |
✅ |
❌ 안티패턴
- 버전 비교 없이
UPDATE SET ... WHERE id = ?: lost update. 마지막 쓰기가 이전 쓰기를 조용히 덮어씀. - 클라이언트가 보낸 version 을 그대로 신뢰 후 검증 안 함: 클라이언트가 의도적으로 옛 version 보내면 항상 통과.
- 재시도 시 같은 expectedVersion 사용: 영원히 실패. 재시도 안에서 다시 read 해야 함.
- 충돌 시 묵묵히 무시: 사용자는 자기 변경이 적용되었다고 믿음. 실제로 사라짐. 명시적 4xx 응답 + UI 알림.
- Version 을 timestamp 로: 같은 ms 에 두 update 가 들어오면 둘 다 통과. 정수 시퀀스가 안전.
- 모든 곳에 자동 retry: 사용자가 form 에 입력한 값을 자동 재시도 = 사용자가 본 데이터와 다를 수 있음. UI 가 "다른 사람이 수정했어요. 다시 보시겠어요?" 묻기.
- OCC 와 application-level 캐시 결합: 캐시가 stale version 을 들고 있으면 영원히 충돌. 캐시 invalidation 정책 필수.
🤖 LLM 활용 힌트
- LLM에게 SQL UPDATE 작성: "WHERE 조건에 version = expected 추가, rowCount 0 이면 ConflictError throw" 명시.
- REST API: "GET 응답에 ETag, PUT 요청에 If-Match 강제 (없으면 428)" 패턴 요청.
- retry wrapper: "read → modify → write 흐름이어야 retry 의미 있음. 같은 expected 로 재시도 금지" 강조.
🧪 검증 상태
- verification_status:
conceptual - JPA
@Version, Hibernate optimistic locking, REST API ETag 표준 (RFC 7232) 의 핵심 패턴. - 적용 사례 발견 시
applied_in추가.