Scaling a CRM Database to 300K Leads
Lessons from scaling a multi-tenant CRM database past 300K leads on MongoDB: index design around real query shapes, pagination, safe bulk operations and access hardening.
A CRM that feels instant at 5,000 leads can feel sluggish at 300,000. Lists take seconds to load, filters time out, and the team starts blaming "the server". Scaling CRM database performance at this size is rarely about bigger machines. It is about indexes that match how people actually query, pagination that does not degrade, and bulk operations that cannot go wrong.
I am a core engineer on Antoc AI, a real-estate CRM used by 100+ firms in India, which grew past roughly 300K leads managed. This post shares what we changed in MongoDB as that happened, and the order I would tackle things in again.
TL;DR
Find the real query shapes your screens send, then build compound indexes that start with the tenant key, followed by equality filters and the sort field. Replace deep skip pagination with range-based paging. Wrap bulk updates in code that validates filters and caps counts. Lock down the database with authentication, network restrictions and least-privilege users.
key takeaways
- At a few hundred thousand records, slowness almost always comes from missing or misordered compound indexes, not from the database being too small.
- Every compound index in a multi-tenant CRM should start with the tenant key, followed by equality filters, then the sort field.
- Bulk updates and deletes need filter validation in code, because an empty or wrong filter can touch every document.
- Authentication, network restrictions and least-privilege database users are part of scaling, since more data means more at stake.
Start with query shapes, not guesses
Before adding any index, collect the queries your app actually sends. In a CRM, a handful of screens generate most of the load:
| Screen | Typical filter | Typical sort |
|---|---|---|
| Lead list | business, stage, assigned user, source, date range | created date, newest first |
| My leads (salesperson) | business, assigned user, stage | next follow-up date |
| Duplicate check on intake | business, normalized phone | none |
| Pipeline board | business, stage | updated date |
| Reports | business, date range, grouped by source or user | none |
Turn on the MongoDB profiler for slow operations, or check slow query logs, and run explain("executionStats") on the worst offenders. Two numbers matter: totalDocsExamined versus nReturned. If the database examines 200,000 documents to return 25, you need an index.
Index design for a multi-tenant CRM
Tenant key first, always
In a multi-tenant CRM every query is scoped to a business. So every compound index starts with businessId. An index on { stage: 1, createdAt: -1 } without the tenant key forces the database to sift through other businesses' leads.
Equality, sort, range
The ordering rule that fixes most slow lists: equality fields first, then the sort field, then range fields. The MongoDB ESR guideline explains it in detail. For the "my leads" screen, that gives:
leadSchema.index({ businessId: 1, assignedTo: 1, stage: 1, nextFollowUpAt: 1 });
For duplicate checks on intake, a tight index on { businessId: 1, phone: 1 } turns every new lead's lookup into a near-instant probe, which matters when ads and portals push leads in bursts.
Do not index everything
Each index costs write time and memory. A lead collection with twenty single-field indexes is slower to write and still slow to read, because single-field indexes rarely serve multi-filter queries well. Prefer a few compound indexes designed around your top screens, and drop indexes the $indexStats output shows are never used.
Pagination and heavy queries
Replace deep skip with range paging
skip(50000).limit(25) makes the database walk 50,000 entries before returning anything. Page 1 is fast, page 2,000 is not. For infinite scroll and "next page" lists, use range-based pagination: remember the last item's sort value and _id, and query for items after it. Response time stays flat however deep the user scrolls.
Count less
Exact counts across hundreds of thousands of filtered documents are expensive. Show counts where they matter (pipeline totals, report figures) and avoid recomputing them on every list request. Cache or compute them on a schedule if they appear on busy screens.
Project only what you render
Lead documents grow: notes, custom fields, activity references. A list view needs maybe ten fields. Use projections so the database and network do not ship whole documents for a table row.
Safe bulk operations
As the dataset grows, so does the blast radius of a single bad command. Reassigning a salesperson's leads, bulk-changing stages or cleaning imported data all use updateMany or bulkWrite. A filter that accidentally comes through as {} updates every lead in every business.
A safety wrapper prevents that:
- Reject empty filters outright.
- Require the tenant key (
businessId) in every filter. - Count matching documents first and refuse if the count exceeds an expected cap.
- Log who ran the operation, the filter and the affected count.
- For deletes, prefer soft deletes with a timestamp so mistakes are recoverable.
This sounds bureaucratic until the day it stops a script from rewriting the wrong data.
Hardening access as data grows
More data means more at stake. Alongside performance work, tighten access:
- Require authentication on the database, with separate users for the app, background workers and admin tasks.
- Give each user only the roles it needs. The app does not need to drop collections.
- Restrict network access so only your application servers can reach the database port.
- Keep connection strings in environment configuration, never in the repo.
How I built this in practice
The Antoc case study covers the product: a real-estate CRM with lead capture from Meta lead ads, Google Ads, property portals, Google Sheets, webhooks and public forms, plus WhatsApp automation, an AI voice agent and an MCP server. All of that writes into MongoDB through Mongoose, from Node/Express services on GCP VMs.
As leads grew past 300K, we scaled and hardened the database: indexes built around real query shapes, safe bulk operations with filter validation, and authentication plus network restrictions on the database itself. Duplicate detection and auto-assignment on intake depend directly on those indexes, because every incoming lead triggers lookups before it is saved.
Common mistakes
- Adding single-field indexes on every filterable field instead of a few compound indexes for real screens.
- Leaving the tenant key out of compound indexes in a multi-tenant collection.
- Using deep
skippagination on large lists. - Running exact counts on every list request.
- Allowing
updateManyordeleteManywith unvalidated filters from admin tools or scripts. - Treating database authentication and network restrictions as optional once traffic grows.
keep reading
FAQ
Is 300K leads a lot for MongoDB?+
No. MongoDB handles collections far larger than that comfortably. At this size, performance problems usually come from query patterns and indexes rather than raw volume.
How do I find slow queries in a CRM database?+
Enable the database profiler or slow query log, then run explain on the worst offenders. Look for collection scans and for queries that examine far more documents than they return.
Should I switch from MongoDB to PostgreSQL as my CRM grows?+
Usually not for performance alone. Both scale well for CRM workloads. Switch only if your data has become heavily relational or you need features the current database lacks, because migrations are expensive.
What is the safest way to run bulk updates on production data?+
Validate that the filter is non-empty and scoped to a tenant, count matching documents first, cap the expected count, and log the operation. Test the exact filter on a staging copy before running it in production.
Devang Gandhi
AI & full stack developer at Passdn, building CRMs, SaaS, AI agents and Flutter apps for clients in the US, UK, Australia, UAE and India. More about me