WAL Mode (Write-Ahead Logging) for Concurrency
By default, SQLite locks the entire database during writes. Enabling Write-Ahead Logging (`PRAGMA journal_mode=WAL`) allows readers to read concurrently while a writer is writing 🏎️.
import sqlite3
# Enable WAL journal mode:
conn = sqlite3.connect(":memory:")
result = conn.execute("PRAGMA journal_mode=WAL").fetchone()
print(f"Active journal mode: {result[0]} 🏎️")
conn.close()
# 22. WAL Mode (Write-Ahead Logging) for Concurrency By default, SQLite locks the entire database during writes. Enabling Write-Ahead Logging (`PRAGMA journal_mode=WAL`) allows readers to read concurrently while a writer is writing 🏎️. ```python import sqlite3 # Enable WAL journal mode: conn = sqlite3.connect(":memory:") result = conn.execute("PRAGMA journal_mode=WAL").fetchone() print(f"Active journal mode: {result[0]} 🏎️") conn.close() ``` If you deploy SQLite on production servers handling concurrent web requests, turning on WAL mode is the single most important configuration setting! 🏎️ --- ### Question What major concurrency benefit does WAL (Write-Ahead Logging) mode provide in SQLite? 🧠 - [ ] a. Readers do not block writers, and writers do not block readers 🏎️ - [ ] b. It compresses data by 90% - [ ] c. It converts SQLite into an in-memory Redis instance - [ ] d. It disables database commits --- ### Challenge Connect to SQLite. Execute `PRAGMA synchronous = NORMAL;`. Fetch the result with `.execute("PRAGMA synchronous").fetchone()[0]`. Print result. 🎯 <details> <summary>Solution</summary> ```python import sqlite3 conn = sqlite3.connect(":memory:") conn.execute("PRAGMA synchronous = NORMAL") res = conn.execute("PRAGMA synchronous").fetchone()[0] print(res) conn.close() ``` </details>