Conversation
SetMaxOpenConns(1) is only required for writing. In WAL mode readers block
neither each other nor the writer, but one shared connection puts ingestion and
UI queries in the same queue: a heavy list request stalls incoming events, and a
burst of events stalls the UI.
storage.OpenPooled(dsn, n) keeps Open(dsn) as a single-connection wrapper, so
nothing changes for existing callers, and the pool size comes from
database.max_open_conns / DATABASE_MAX_OPEN_CONNS (default 4).
Two DSN parameters are required for a pool to be correct, and are injected when
absent:
- busy_timeout — otherwise a competing write fails instead of waiting;
- _txlock=immediate — otherwise busy_timeout does not apply to writes at all,
because a deferred transaction starts as a reader and SQLite refuses the
lock upgrade immediately with SQLITE_BUSY. This was not theoretical: a
four-connection pool without it produced "upsert sentry_traces: database is
locked (5)" on a live stream within minutes.
Note that a pool multiplies cache_size: it is per connection, so on a
memory-constrained host the DSN value should be divided by the pool size.
Measured on our instance (≈40k events/day, 1 GB RAM): with the pool the UI stops
waiting behind ingestion and the service RSS stays flat.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SetMaxOpenConns(1)is commented "SQLite single-writer", which is true for writing only. In WAL mode readers block neither each other nor the writer, but one shared connection puts ingestion and UI queries in the same queue: a heavy list request stalls incoming events, and a burst of events stalls the UI.Changes
storage.OpenPooled(dsn, n);Open(dsn)stays as a single-connection wrapper, so existing callers are unaffected.database.max_open_conns/DATABASE_MAX_OPEN_CONNS, default 4.busy_timeout— otherwise a competing write fails instead of waiting;_txlock=immediate— otherwisebusy_timeoutdoes not apply to writes at all, because a deferred transaction starts as a reader and SQLite refuses the lock upgrade immediately withSQLITE_BUSY(waiting would deadlock two readers that both want to write).That second one was not theoretical: a four-connection pool without it produced
upsert sentry_traces: database is locked (5) (SQLITE_BUSY)on our live stream within minutes. Every transaction in this code base is a writing one, so taking the write lock upfront costs nothing.One caveat worth documenting:
cache_sizeis per connection, so on a memory-constrained host the DSN value should be divided by the pool size. On our 1 GB host we went fromcache_size(-65536)with one connection tocache_size(-16384)with four, and RSS dropped from 197 MB to 90 MB.Testing
go vet ./...,go test ./...andgo buildpass. AddedTestOpenPooled_ConcurrentWritesAndReads(4 writers × 25 writes with interleaved reads through the same pool — this is the test that fails withSQLITE_BUSYwithout_txlock=immediate) andTestOpenPooled_SingleConnection. Running in production on our instance withmax_open_conns: 4and zero busy errors since.