Prompt
Building search for a large dataset with frequent updates
Latest observation
When building search over a large dataset with frequent updates, the main challenge is balancing:
- fast reads / low-latency search
- freshness of updates
- indexing cost
- operational simplicity
A good design usually depends on the type of search you need (keyword, filters, full-text, faceting, vector, etc.), but the common patterns are:
1) Use a search engine built for indexing, not your primary database
For large-scale search, typical choices are:
- Elasticsearch / OpenSearch — very common for full-text, filtering, aggregations
- Solr — mature, strong search features
- Postgres full-text search — okay for smaller scale / simpler needs
- Meilisearch / Typesense — simpler, often good for smaller or medium workloads
- Vector DB / hybrid search systems if semantic search is needed
If the dataset is large and updates are frequent, a dedicated search index is usually the right choice.
2) Separate the source of truth from the search index
A common architecture:
- Primary database stores the canonical record
- Search index is a derived, read-optimized copy
- Updates flow from DB → index asynchronously
This avoids making search depend directly on the transactional store.
Typical flow
- Write/update data in primary DB
- Emit change event or capture CDC
- Update search index asynchronously
- Search queries go to the index
This gives scalability and avoids coupling writes to expensive indexing operations.
3) Use incremental updates, not full reindexing
For frequent updates, avoid rebuilding the whole index.
Better options:
- Partial document updates
- Upserts
- Delete + reinsert only changed records
- Batch indexing with small micro-batches
If the search engine supports it, update only the fields that changed.
4) Use an asynchronous pipeline
Frequent updates can overwhelm the index if every write is synchronous.
Common pattern:
- Application writes to DB
- Changes are published to a queue/stream
- Indexer consumes events and updates the search engine
Tools:
- Kafka
- RabbitMQ
- AWS SQS/SNS
- PostgreSQL logical replication / CDC
- Debezium for CDC pipelines
This improves resilience and lets you buffer bursts of changes.
5) Accept eventual consistency
With frequent updates, search results may lag behind writes by seconds.
That’s often acceptable and is the tradeoff for scale and performance.
If you need strong consistency between writes and search results, the system becomes much harder and slower. Usually:
- transactional DB = source of truth
- search index = eventually consistent projection
6) Design your documents for search, not normalization
Search indexes work best when data is denormalized.
Instead of joining many tables at query time:
- flatten common fields into one searchable document
- precompute searchable text
- include filter fields and facets directly in the document
This reduces query complexity and improves performance.
7) Be careful with update-heavy fields
Some fields cause more reindexing cost than others:
- frequently changing fields can create heavy index churn
- large text fields are expensive to reprocess
- nested documents and deep relationships can make updates costly
If some fields change constantly, consider:
- separating them into another index
- using a “hot” and “cold” data split
- only indexing fields actually used for search
8) Partition/shard carefully
For large datasets, sharding matters a lot.
You want:
- balanced shard sizes
- enough shards for parallelism
- not too many shards, which adds overhead
Good shard strategy depends on:
- total data size
- update rate
- query patterns
- tenant boundaries, if multi-tenant
If data is naturally grouped by tenant, time, or category, partitioning by that can help.
9) Use batching and backpressure
If updates are frequent, indexing one record at a time can be inefficient.
Use:
- batch writes to the index
- retry with exponential backoff
- dead-letter queue for failed events
- backpressure to avoid overwhelming the indexer
Batching improves throughput significantly.
10) Handle deletes and out-of-order events
With async pipelines, event ordering and deletions matter.
You need to handle:
- delete events
- update events arriving out of order
- duplicate events
- replay after failure
Common solution:
- include a version or timestamp in each event
- only apply the latest version
- make indexing idempotent
11) Plan for reindexing
At some point, you’ll need to:
- change mapping/schema
- fix bugs
- improve analyzers
- reprocess data
So design for:
- blue/green indexes
- alias switching
- ability to rebuild index from the source of truth
This lets you rebuild without downtime.
12) Tune based on workload
If updates are extremely frequent:
- favor simpler index structure
- minimize indexed fields
- batch aggressively
- consider separating hot fields
If searches must be very fresh:
- reduce pipeline lag
- keep indexing close to the DB
- perhaps use near-real-time refresh settings
If search is complex:
- use a proper search engine rather than ad hoc DB queries
A practical architecture
A common production setup looks like this:
- Primary DB: PostgreSQL/MySQL/etc.
- Event stream: Kafka/SQS/CDC
- Indexer service: consumes changes, transforms documents
- Search engine: OpenSearch/Elasticsearch
- Query API: serves user searches from the index
This is usually the best balance for large, frequently changing data.
Rule of thumb
- Small dataset, simple search → database search may be enough
- Large dataset, frequent updates → dedicated search engine + async indexing
- Need semantic search → hybrid search architecture
- Need strict freshness → consider cost/complexity of stronger consistency, but most systems use eventual consistency
If you want, I can also give you:
- a recommended architecture diagram,
- a technology comparison (Elasticsearch vs Postgres FTS vs Typesense), or
- a design for handling high update throughput.