How I Use MongoDB Atlas Metrics to Understand and Debug My Replica Set
Have you ever opened MongoDB Atlas, clicked on Metrics, and wondered what all those blue, spiky graphs actually mean?
I have.
When I first started exploring MongoDB Atlas metrics for my clinic application, I saw several graphs showing database activity across different cluster members. Instead of ignoring them, I decided to understand what they meant and how they could help me monitor my application.
In this post, I'll explain what I learned from examining my MongoDB Atlas cluster, focusing on three important areas:
Replica set members
Operation counters (Opcounters)
Connection rates and queued operations
I'll also show how these metrics can help identify potential problems in a Node.js application.
1. Understanding the Three Hosts in MongoDB Atlas
When I opened the Metrics page for my Cluster0 cluster in Project 0, I noticed three host entries at the top of the dashboard.
Their names looked similar to these:
ac-2auywr9-shard-00-00...:27017
ac-2auywr9-shard-00-01...:27017
ac-2auywr9-shard-00-02...:27017Each hostname represents a member of the cluster's replica set.
A replica set is a group of MongoDB servers that maintain copies of the same data to provide redundancy and support automatic failover.
In a typical three-member replica set, the members have the following roles:
Primary (P): Receives write operations and coordinates replication.
Secondary (S): Replicates data from the primary and can serve eligible read operations, depending on the application's read preference.
In my screenshot, the dashboard displays one primary and two secondary members.
Figure 1: MongoDB Atlas Metrics dashboard showing the replica set members and their Opcounters graphs.
Why does this matter?
Imagine that the primary member becomes unavailable.
In a properly configured replica set, the remaining eligible members can participate in an election to select a new primary.
This helps MongoDB maintain availability, although applications can still experience a brief interruption while the election and reconnection process takes place.
MongoDB Atlas manages much of this infrastructure for its managed clusters.
However, having a replica set does not mean you can ignore application errors, connection settings, or monitoring.
What I learned: Understanding which member is primary and which members are secondary helps me better understand how my database is operating.
One important clarification: the three hosts shown in my screenshot are replica set members. Their presence alone does not prove that the deployment is sharded. Replica sets and sharding are different MongoDB concepts.
2. What Are Opcounters in MongoDB Atlas?
The main graph in my screenshot is labelled Opcounters.
This metric shows the rate of different types of database operations being processed by a MongoDB server.
Depending on the operation type, the counters include:
Insert: Inserting documents into a collection.
Query: Performing query operations.
Update: Modifying existing documents.
Delete: Removing documents.
Getmore: Retrieving additional batches of results from a cursor.
Command: Executing database commands.
These metrics help me understand how the database is being used.
For example, if an application frequently retrieves patient records, query activity may increase. If it regularly updates records, update activity may increase.
How I read the graph
In my screenshot, the vertical axis shows operation rates ranging from approximately 0/s to 0.2/s.
The horizontal axis shows a period covering approximately 15:00 to 15:30.
The graph fluctuates rather than remaining completely flat.
That is not automatically a problem.
For a small application with relatively little traffic, database activity can be intermittent. A user may open a page, trigger a request to the backend, and cause several database operations. When there are no requests, activity may decrease.
However, the graph alone cannot tell me exactly how many users are visiting the application or whether the application is healthy.
I need to compare it with application logs, request activity, and other database metrics.
What different patterns might indicate
Here are some examples of how I would investigate different patterns.
When Opcounters remain at zero
This could mean the database is idle, no requests are reaching it, or the selected time window contains no recorded operations.
I would check application traffic and backend logs before concluding that the application is disconnected.
When Opcounters increase sharply
A sudden increase could be caused by increased user activity, a background task, repeated queries, or an application change.
I would investigate the timing and identify which operations increased before deciding whether there is a performance problem.
When operations increase but the application becomes slow
I would examine additional metrics, such as query execution time, CPU utilization, connection activity, and slow-query information.
A high operation rate does not automatically mean the database is unhealthy.
Similarly, a low operation rate does not necessarily mean something is wrong.
What this means for my clinic application
My clinic application is a MERN stack project hosted on AWS EC2 and connected to MongoDB Atlas.
Looking at Opcounters helps me understand when database activity is occurring.
My next step is to compare that activity with application requests and logs so I can identify which operations are being triggered and investigate unusual patterns.
That is a more useful approach than looking at a graph in isolation.
3. Connection Rate: Understanding Database Connections
Another useful metric available in MongoDB Atlas is Connection Rate.
This metric helps me understand how frequently connections to a MongoDB server are being established.
It is important to distinguish connection rate from the total number of active connections.
A high connection rate may indicate that an application is repeatedly opening new connections instead of reusing existing ones.
In a Node.js application, connection management is particularly important.
An example of a connection-management mistake
Consider this simplified example:
// Not recommended: creates a new client for every request
app.get("/patients", async (req, res) => {
const client = new MongoClient(process.env.MONGO_URI);
await client.connect();
// Perform database operations...
});This example creates a new MongoDB client whenever the route is called.
If the application does not manage and close those clients properly, it can waste resources and contribute to connection problems.
Even when connections are closed, repeatedly creating clients is generally unnecessary for a typical long-running Node.js application.
A better approach: reuse the MongoDB client
For many Node.js applications, a better approach is to create a MongoDB client once and reuse it throughout the application's lifetime.
For example:
import { MongoClient } from "mongodb";
const uri = process.env.MONGO_URI;
const client = new MongoClient(uri);
let db;
export async function connectDB() {
if (!db) {
await client.connect();
db = client.db("clinicDB");
}
return db;
}In this example, the database connection is initialized when connectDB() is first called, and subsequent calls reuse the database handle.
In a production application, I would also consider startup initialization, connection error handling, and graceful shutdown.
The lesson: Reusing a MongoDB client can help avoid unnecessary connection creation. However, a high connection rate alone does not prove that the application is exhausting its connection pool.
4. What Are Queued Operations?
Another metric worth understanding is queued operations.
Queuing can occur when operations must wait for resources or for processing to become available.
For example, an application may generate more concurrent work than the database can process immediately.
However, queued operations should be interpreted alongside other metrics rather than treated as a standalone diagnosis.
If I notice unusual queue activity, I would investigate:
Database CPU and resource utilization.
Query execution times.
Slow queries.
Connection usage.
Application request volume.
Whether the workload has recently changed.
I would also investigate whether frequently executed queries need better indexes.
Should I immediately upgrade my MongoDB cluster?
Not necessarily.
An increase in queued operations does not automatically mean the cluster needs a larger tier.
The underlying problem could be inefficient queries, excessive concurrency, connection-management issues, or limited resources.
Before upgrading, I would investigate the evidence and determine whether the workload has outgrown the current configuration.
For a small application, this is particularly important because unnecessary upgrades can increase cloud costs without addressing the real problem.
5. How These Metrics Help My AWS-Hosted MERN Application
My clinic application combines several technologies:
MongoDB Atlas: Managed database.
Node.js: Backend runtime.
Express.js: Backend framework.
React: Frontend library.
AWS EC2: Application hosting environment.
Each component has its own potential failure points.
For example, the website could become slow because of backend processing, inefficient database queries, network problems, or insufficient server resources.
Looking at MongoDB Atlas metrics gives me visibility into the database side of the application.
It does not tell me everything that is happening across the entire stack, but it gives me another source of information when troubleshooting.
This is why I want to develop the habit of examining database metrics rather than waiting until an application problem becomes obvious to users.
A few things I would check regularly
When investigating database performance, I would start with:
Opcounters: Is database activity changing unexpectedly?
Connection metrics: Is the application opening connections more frequently than expected?
Query diagnostics: Are there slow or inefficient queries?
Resource utilization: Is the database approaching its resource limits?
Application logs: Are requests failing or taking longer than usual?
The goal is not to stare at every graph every day.
The goal is to know which metrics can help answer a specific troubleshooting question.
6. A Note About MongoDB Write Concern and Failover
When connecting a Node.js application to MongoDB Atlas, you may encounter this option:
w=majorityThis specifies a write concern requiring acknowledgment from a majority of the replica set's voting members.
For example, a connection string may include:
retryWrites=true&w=majorityThese options serve different purposes:
retryWrites=trueenables retryable writes for supported operations.w=majorityrequests acknowledgment after the write has been applied to a majority of voting members.
They can help applications handle certain transient failures and improve write durability, but they do not guarantee that every operation will succeed during a failover without interruption.
Your application should still handle connection errors and retry operations appropriately when necessary.
7. What I Learned From Exploring MongoDB Atlas Metrics
Before exploring these metrics, it was easy to think of MongoDB Atlas simply as a place where my application stores data.
Now, I understand that the database provides visibility into much more than stored documents.
The Metrics dashboard can help me investigate database activity, connection behavior, and resource-related problems.
I also learned an important troubleshooting lesson:
A metric is a clue, not a complete diagnosis.
A graph showing zero operations does not automatically mean an application is disconnected. A high operation rate does not automatically mean queries are inefficient. And queued operations do not automatically mean it is time to upgrade the cluster.
Good troubleshooting means comparing several sources of evidence before deciding what to change.
As I continue developing my cloud and DevOps skills, understanding these metrics will help me make better decisions when investigating problems in my applications.
Final Thoughts
Monitoring is an important part of running an application, even when the application is small.
For my MERN stack clinic project, MongoDB Atlas Metrics gives me a useful starting point for understanding what is happening inside the database.
My next step is to explore Atlas alerts and determine which conditions are worth notifying me about, rather than relying entirely on manual checks.
Have you explored the Metrics section in MongoDB Atlas? Which metrics have helped you troubleshoot your application?
Read Also: My GitHub Actions Self-Hosted Runner Kept Exiting After One Job on AWS EC2 - How I Fixed It
https://blog.sirvickcloud9.online/2026/10/my-github-actions-self-hosted-runner.html

Comments
Post a Comment