C.W.K.
Stream
Lesson 09 of 10 · published

WAL 모드 — 성능의 #1 win

~14 min · wal, performance, concurrency

Level 0Scout
0 XP0/80 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

PRAGMA 한 줄, 두 자릿수 개선

SQLite 기본 journal 모드는 DELETE: writer 가 reader block 하는 exclusive 락 획득. WAL (Write-Ahead Logging) 모드가 이걸 뒤집어 — reader 와 writer 가 서로 block 안 함. Reader 는 일관된 snapshot 봄, writer 는 log 파일 (db-wal) 에 append, 주기적으로 main DB 로 checkpoint.

DB 마다 한 번 켜 (persistent):

PRAGMA journal_mode = WAL;

거의 항상 원하는 이유:

  • Reader 가 writer 한테 block 안 됨, 반대도 마찬가지.
  • Write 극적으로 빠름 — fsync 적음, sequential append.
  • 모든 동시 워크로드의 prerequisite (웹 서버, async 앱, 백그라운드 sync 있는 데스크탑 앱).
Warning: WAL 모드는 제대로 된 file locking 의 진짜 파일시스템 필요. NFS, SMB, FUSE 등 quirky locking 가진 데서는 안전 X. 로컬 디스크만.

WAL 와 합리적 busy_timeout (5–30 초) pair — checkpoint 동안 일시적 lock 에 SQLITE_BUSY 로 실패 안 하고 잠깐 wait.

Code

'production-ready opener'·python
import sqlite3

def open_db(path: str) -> sqlite3.Connection:
    conn = sqlite3.connect(path, timeout=30.0)
    conn.execute('PRAGMA journal_mode = WAL')
    conn.execute('PRAGMA synchronous = NORMAL')   # WAL 와 안전, 더 빠름
    conn.execute('PRAGMA foreign_keys = ON')
    conn.execute('PRAGMA busy_timeout = 5000')
    return conn

# WAL 활성 확인:
print(open_db('demo.db').execute('PRAGMA journal_mode').fetchone())
# ('wal',)
수동 checkpoint — 보통 불필요·sql
-- Checkpoint 강제 (실행 동안 writer 활동 일시 중단)
PRAGMA wal_checkpoint(TRUNCATE);

-- WAL 상태 확인
PRAGMA wal_checkpoint;
-- (busy, log_pages, checkpointed_pages)

External links

Exercise

본인 머신에서 WAL win 재현. 작은 벤치마크: writer thread 한 개가 row insert 루프, reader thread 한 개가 SELECT count(*) 루프. 기본 (DELETE) journal mode 와 WAL 로 한 번씩. 양 throughput 비교. 병목 어디로 옮겼는지 기록.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.