Prompt
How do I keep search results in sync with Postgres?
Latest observation
Keeping search results in sync with Postgres usually means treating Postgres as the source of truth and propagating changes to your search index reliably.
Common patterns
1. Dual-write in the app
When you write to Postgres, also write to the search index in the same request.
- Pros: simple to understand
- Cons: easy to get out of sync if one write succeeds and the other fails
This is usually the least reliable option unless you add retries and reconciliation.
2. Async indexing with a queue
Your app writes only to Postgres, then emits an event like “row changed” to a queue or stream. A worker updates the search index.
Typical flow:
- Transaction commits in Postgres
- App publishes an event
- Worker consumes event
- Worker updates search index
- Pros: decoupled, scalable
- Cons: eventual consistency, more moving parts
This is a very common and solid approach.
3. Transactional outbox pattern
This is a safer version of async indexing.
Inside the same Postgres transaction as your data change, you also write an “outbox” record. A separate process reads the outbox and updates search.
Why it helps:
- If the DB transaction rolls back, no outbox event is recorded
- If the app crashes after commit, the outbox is still there to be processed
Typical flow:
- Update business tables
- Insert outbox event in same transaction
- Commit
- Worker polls outbox or streams it
- Worker updates search index
- Mark outbox event as processed
This is often the best default architecture.
4. CDC (Change Data Capture) from Postgres
Use logical replication or a tool like Debezium to capture changes directly from Postgres and send them to your search system.
- Pros: no app changes needed for syncing logic
- Cons: more infrastructure, harder to operate
Good when you want strong consistency in change propagation without coupling indexing to app code.
Practical recommendations
If you want the easiest robust setup
Use:
- Postgres
- Transactional outbox
- Worker
- Search index like Elasticsearch, OpenSearch, Meilisearch, Typesense, or Algolia
Make indexing idempotent
Your indexing worker should be safe to retry:
- use document IDs based on Postgres primary keys
- upserts instead of inserts
- include a version or updated_at timestamp if needed
Handle deletes
Deletes should also generate index changes:
- soft delete: filter them out in search
- hard delete: remove from index
Add periodic reindexing
Even with a good sync pipeline, run a scheduled job to:
- compare Postgres vs index
- rebuild missing docs
- recover from bugs or outages
Track versioning
To avoid stale updates overwriting newer ones:
- store
updated_ator a monotonically increasingversion - only apply index updates if they’re newer than what’s already indexed
A simple recommended architecture
- Application writes to Postgres
- In same transaction, write an outbox event
- Background worker reads outbox
- Worker updates search index
- Failed events are retried
- Nightly reindex job repairs drift
Rule of thumb
- Need simplicity? Use async indexing with retries
- Need reliability? Use transactional outbox
- Need low-level capture at scale? Use CDC
If you want, I can sketch a concrete implementation in Postgres + Node.js + Elasticsearch/OpenSearch or Postgres + Python + Meilisearch.