MongoDB Configuration
Setting up MongoDB for Linqra with high availability
Overview
MongoDB serves as the primary data store for Linqra, requiring a replica set configuration for high availability and data redundancy. The database is configured with a 3-node replica set architecture to ensure continuous operation and data consistency.
Data Structure
Core Data Collections
Collection | Purpose | Key Components |
---|---|---|
Users | User management and authentication | • Account information • Credentials • Sessions • Access tokens |
Teams | Organizational structure | • Team hierarchies • Department mappings • Member associations |
Roles | Access control and permissions | • Role definitions • Permission sets • Access policies |
API Management
Collection | Purpose | Key Components |
---|---|---|
Routes | API routing configuration | • Endpoint definitions • Route mappings • Load balancing rules |
Services | Service registry and management | • Service registry • Dependencies • Health status • Versions |
Documentation | API documentation | • API specifications • Version history • Endpoint docs |
System Collections
Collection | Purpose | Key Components |
---|---|---|
Metrics | Performance monitoring | • Response times • Request volumes • Error rates • Resource usage |
Audit | System auditing | • System events • User activities • Config changes |
Analytics | System analytics | • Usage patterns • Performance trends • Health metrics |
All collections are automatically replicated across the three nodes in the replica set, ensuring data redundancy and high availability.
Local Development Setup
Directory Preparation
Create the necessary data directories for the MongoDB replica set:
Docker Compose Configuration
The MongoDB replica set is configured using Docker Compose. Here’s the complete configuration for all three nodes:
Each node is configured with resource limits (1 CPU, 1GB memory) and uses the same keyfile for authentication. The only differences between nodes are their container names, ports, and data volume paths.
Host Configuration
Add the following entries to your hosts file (important for container communication):
Without proper host configuration, Docker containers may not be able to communicate with the MongoDB nodes.
Replica Set Initialization
- Connect to the primary node:
- Initialize the replica set:
- Verify the replica set status:
The replica set will automatically elect a primary node, with the remaining nodes becoming secondaries. Node roles may change over time as the cluster rebalances.
Connecting to the Replica Set
You can connect to the replica set using the following connection string:
This same connection string can be used in:
- Your application configuration
- MongoDB Compass
- Other MongoDB clients
Connection Configuration
MongoDB connection settings for your application:
For production environments, it’s recommended to use secrets management for storing credentials.
Replica Set Configuration
Linqra requires a 3-node MongoDB replica set configuration to ensure high availability and fault tolerance.
The replica set consists of:
- 1 Primary node (handles all write operations)
- 2 Secondary nodes (provide read scalability and automatic failover)
Benefits
- High Availability: Automatic failover if the primary node becomes unavailable
- Data Redundancy: Multiple copies of data across different nodes
- Read Scalability: Secondary nodes can handle read operations
- Disaster Recovery: Built-in backup and recovery capabilities
Dockerfile
Directory Structure
The data directories (data1, data2, data3) will be populated when the containers start running. The mongo-keyfile is generated during the setup process.