DEV Community

Milinda Biswas
Milinda Biswas

Posted on

How We Built MongoDB to ElasticSearch Sync for $89/Month (And Saved Our Sanity)

MongoDB to ElasticSearch Sync on a Budget

How We Built MongoDB to ElasticSearch Sync for $89/Month (And Saved Our Sanity)

It was 2 AM on a Thursday when our product search died. Again.

I watched the server metrics climb as a single user typed "wireless headphones" into our search bar. 30 seconds later, they got results. Thirty. Seconds. They were long gone by then, and so was the sale.

At Avluz.com, we track prices across 10,000+ products from Amazon, eBay, and Walmart. Our MongoDB database held everything beautifully, but searching through it? That was killing us—literally costing us customers and revenue every day.

Every developer dealing with slow database queries

I knew we needed ElasticSearch. But when I looked at the quotes—$1,200/month for Algolia, $450/month for AWS OpenSearch—I felt that startup founder panic. We couldn't justify those costs, but we couldn't survive with 30-second searches either.

So we built our own solution for $89/month. Here's exactly how we did it.

The Problem: When Your Database Becomes Your Bottleneck

Let me show you what we w dealing with:

// Our old search approach (don't do this)
async function searchProducts(query) {
  const startTime = Date.now();

  const results = await db.collection('products').find({
    $or: [
      { title: { $regex: query, $options: 'i' } },
      { description: { $regex: query, $options: 'i' } },
      { category: { $regex: query, $options: 'i' } }
    ]
  }).limit(50).toArray();

  console.log(`Search took ${Date.now() - startTime}ms`);
  return results;
}
Enter fullscreen mode Exit fullscreen mode

This simple search was taking 15-30 seconds during peak traffic. Why? MongoDB isn't optimized for full-text search at scale. Every search scanned thousands of documents, and regex operations on unindexed fields are brutally slow.

Our metrics told a grim story:

  • Average search time: 23.7 seconds
  • Bounce rate on search: 78%
  • Server CPU usage: 85% during searches
  • Angry customer emails: Too many to count

We needed a dedicated search engine. The question was: could we afford one?

Top comments (0)