yadavsourabhgh / mongo-keepalive
Keep MongoDB Atlas free-tier clusters alive by sending periodic ping commands
Package info
github.com/YadavSourabhGH/mongo-keepalive
Language:HTML
pkg:composer/yadavsourabhgh/mongo-keepalive
Requires
- php: >=8.1
- mongodb/mongodb: ^1.19
README
Prevent MongoDB Atlas free-tier clusters from becoming inactive by periodically sending a ping command.
MongoDB Atlas pauses free-tier (M0) clusters after 60 days of inactivity. This library keeps your cluster alive by running db.adminCommand({ ping: 1 }) on a configurable interval (default: every 12 hours).
Supported Languages
| Language | Package | Status | Directory |
|---|---|---|---|
| Node.js | mongo-keepalive |
✅ Published | node/ |
| Python | mongo-keepalive |
✅ Published | python/ |
| Go | mongo-keepalive/go |
📦 Tag-based | go/ |
| Rust | mongo-keepalive |
✅ Published | rust/ |
| Java | mongo-keepalive |
🚧 Ready | java/ |
| PHP | mongo-keepalive |
🚧 Ready | php/ |
| Ruby | mongo-keepalive |
✅ Published | ruby/ |
| C# | MongoKeepAlive |
✅ Published | csharp/ |
| Dart | mongo_keepalive |
✅ Published | dart/ |
| Kotlin | mongo-keepalive |
🚧 Ready | kotlin/ |
Status Legend:
- ✅ Published: Available on package registry
- 📦 Tag-based: Install via GitHub tag/module path
- 🚧 Ready: Code complete, publishing in progress
Why Use This?
- 🎯 Simple: One function call to keep your cluster alive
- 🔄 Reliable: Built-in retry logic handles transient failures
- ⚡ Lightweight: Minimal memory footprint and dependencies
- 🌍 Multi-language: Use the same solution across your entire stack
- 🔧 Configurable: Adjust ping intervals to your needs
- 📝 Production-ready: Comprehensive logging and error handling
Installation
Node.js
npm install mongo-keepalive
Python
pip install mongo-keepalive
Go
go get github.com/YadavSourabhGH/mongo-keepalive/go
Rust
[dependencies] mongo-keepalive = "1.0.0"
Java / Kotlin (Maven)
<dependency> <groupId>com.mongokeepalive</groupId> <artifactId>mongo-keepalive</artifactId> <version>1.0.0</version> </dependency>
PHP
composer require YadavSourabhGH/mongo-keepalive
Ruby
gem install mongo-keepalive
C#
dotnet add package MongoKeepAlive
Dart
dart pub add mongo_keepalive
Usage
Every implementation exposes a similar API:
startKeepAlive({
uri: "mongodb+srv://user:pass@cluster.mongodb.net/db",
interval: "12h"
})
Node.js
const { startKeepAlive } = require("mongo-keepalive"); startKeepAlive({ uri: "mongodb+srv://user:pass@cluster.mongodb.net/db", interval: "12h", });
Python
from mongo_keepalive import start_keep_alive start_keep_alive( uri="mongodb+srv://user:pass@cluster.mongodb.net/db", interval="12h", )
Go
package main import keepalive "github.com/YadavSourabhGH/mongo-keepalive/go" func main() { keepalive.StartKeepAlive(keepalive.Options{ URI: "mongodb+srv://user:pass@cluster.mongodb.net/db", Interval: "12h", }) }
Rust
use mongo_keepalive::start_keep_alive; #[tokio::main] async fn main() { start_keep_alive( "mongodb+srv://user:pass@cluster.mongodb.net/db", "12h", ).await; }
Java
import com.mongokeepalive.KeepAlive; public class Main { public static void main(String[] args) { KeepAlive.startKeepAlive( "mongodb+srv://user:pass@cluster.mongodb.net/db", "12h" ); } }
PHP
use MongoKeepAlive\KeepAlive; KeepAlive::startKeepAlive( 'mongodb+srv://user:pass@cluster.mongodb.net/db', '12h' );
Ruby
require "mongo_keepalive" MongoKeepAlive.start_keep_alive( uri: "mongodb+srv://user:pass@cluster.mongodb.net/db", interval: "12h" )
C#
using MongoKeepAlive; KeepAlive.StartKeepAlive( "mongodb+srv://user:pass@cluster.mongodb.net/db", "12h" );
Dart
import 'package:mongo_keepalive/keepalive.dart'; void main() { startKeepAlive( uri: 'mongodb+srv://user:pass@cluster.mongodb.net/db', interval: '12h', ); }
Kotlin
import com.mongokeepalive.KeepAlive fun main() { KeepAlive.startKeepAlive( uri = "mongodb+srv://user:pass@cluster.mongodb.net/db", interval = "12h" ) }
How It Works
- Connects to your MongoDB cluster using the official driver for each language.
- Pings the database with
db.adminCommand({ ping: 1 })at the configured interval. - Retries automatically on transient failures (3 attempts with 5-second delays).
- Logs every ping attempt and result with timestamps.
- Runs efficiently — uses a single lightweight timer/scheduler with minimal memory usage.
The ping command is a lightweight operation that doesn't impact your cluster's performance but successfully registers as "activity" to prevent automatic pausing.
Configuration
| Option | Type | Default | Description |
|---|---|---|---|
uri |
string | — | MongoDB connection string (required) |
interval |
string | "12h" |
Ping interval (e.g. "6h", "30m") |
Interval Format
"30m"→ 30 minutes"6h"→ 6 hours"12h"→ 12 hours (default)
Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a feature branch:
git checkout -b feat/my-feature. - Commit your changes:
git commit -m "feat: add my feature". - Push to the branch:
git push origin feat/my-feature. - Open a Pull Request.
Please ensure your code:
- Follows the idiomatic conventions of the target language.
- Includes logging and retry logic.
- Does not introduce unnecessary dependencies.
Features
✨ Cross-platform compatibility: Works across all major programming languages and ecosystems
🔐 Secure: Uses official MongoDB drivers with standard connection strings
⏱️ Flexible intervals: Configure from minutes to hours based on your needs
🛡️ Error resilient: Automatic retry logic with exponential backoff
📊 Observable: Built-in logging for monitoring and debugging
🚀 Zero-config defaults: Works out of the box with sensible 12-hour interval
♻️ Graceful shutdown: Clean stop mechanisms in all implementations
🎨 Idiomatic code: Each language implementation follows best practices and conventions
FAQ
Q: Will this increase my MongoDB Atlas costs?
A: No. The ping command is extremely lightweight and doesn't count against your storage or compute quotas. Free-tier clusters remain free.
Q: How often should I ping?
A: The default 12-hour interval is recommended. This is frequent enough to prevent inactivity while minimizing unnecessary operations.
Q: Can I use this in production?
A: Yes! All implementations include proper error handling, logging, and retry logic suitable for production use.
Q: Does this work with paid MongoDB Atlas tiers?
A: Yes, but it's primarily designed for free-tier (M0) clusters which auto-pause after 60 days of inactivity.
Q: What happens if my internet connection drops?
A: The library will retry failed pings automatically and continue when connectivity is restored.
Links
License
This project is licensed under the MIT License.