Production search systems must handle high query volumes while maintaining low latency. This guide covers scalable search architecture.
Architecture Patterns
Primary Database + Search Index
Keep primary database as source of truth, sync to search index:
typescript
// On document update
export async function updateWithSearch(document: any) {
// Update primary DB
await db.documents.update({ where: { id }, data: document });
// Sync to Elasticsearch
await elastic.index({ index: 'documents', id: document.id, body: document });
}Event-Driven Sync
Use message queue for async updates:
typescript
export async function queueDocumentUpdate(documentId: string) {
await queue.add('search-sync', { documentId });
}
// Worker processes updates
queue.process(async (job) => {
const doc = await db.documents.findUnique({ where: { id: job.data.documentId } });
await elastic.index({ index: 'documents', id: doc.id, body: doc });
});Performance Optimization
Index Sharding
typescript
await client.indices.create({
index: 'logs',
body: {
settings: {
number_of_shards: 5,
number_of_replicas: 1,
},
},
});Conclusion
Use async sync for better performance. Shard indices for horizontal scaling. Monitor query performance and optimize accordingly.