I have created this setup and tested using MongoDB Compass, I have connected from PC 2 with the connection string : “mongodb://2.2.2.2:27017/?replicaSet=ReplTest” (notice it is his own ip), this instance is a secondary and some how managed to write to the collection, why? (I thought you can only write using the primary).
If you have a better system design to suggest I would love to hear.
In a situation where PC 1 (the primary) falls and an election happens, is it possible to keep it read-only? (meaning inly PC 1 can write) .
Is it possible to know if a write op has replicated to other nodes? I saw there is a time stamp but an ID for this op would be better…
In case PC 1 has done a write op and PC 2 wasn’t connected.
This has happened to me, I believe it is something with Compass, because when you enter mongosh from within Compass, it directs you to the primary node. Confirm if this has happened to you.
In database environments, the ideal is to always have odd nodes, in this case you have 3 machines with 6 services and that is not a good thing. You can evaluate a PSS (primary-secondary-secondary) or PSA (primary-secondary-arbiter) architecture
You can keep the nodes as read, where only PC1 receives writing, but this means you cannot guarantee complete high availability. You can do this by removing votes from other nodes. (I do not recommend)
By default, all transactions in MongoDB have a majority write concern to ensure that the writing was done on more than one node. If any node goes down, when it returns to the cluster, it will receive the missing data for it, if you still have the oplogs. If not, you will need to do a full sync
If this is something for a productive environment, I recommend reading production notes
I will definitely test this without using compass hoping the writing is denied.
I do understand that there are best practice rules for typical database environments, but this is not the case, this database is for a closed network without many falls or traffic, and the idea is to keep it simple.
How do I keep the nodes as read-only and promise only PC1 receives writing? I know this is problematic for the usual cases but not here. also if PC1 falls and an election occurs can i promise that the writing wont happen? (as PC1 is down…)
Is there maybe an event to subscribe notifying when and which node has done writing?
When you connect with replicaSet=ReplTest you tell the driver that you want to connect to the replica set, not to the individual node. As such the first thing accomplished is to retrieve the replica set configuration and then connect to the primary. If you want to connect to an individual node you need to remove the replicaSet parameter from your connection string.
All nodes are read-only except the primary. With config you are may be able to prevent all nodes except PC1 to become primary.
I have removed, “mongodb://2.2.2.2:27017/” , yet the write operation still came thru… and I don’t understand why (I have connected with MongoDB Compass to a secondary without the replicaSet parameter and succeeded writing )
In theory, this should be enough. Doing it this way ensures that secondaries cannot become primaries. In your architecture, you don’t need the referee, you don’t need it.
It doesn’t have an event or subscription to know when and which node wrote. The primary node is the only one that can receive writes and it replicates to the secondary nodes. What you can do is read the Change Streams to know the data that is being written and do some logic with that or plug in a CDC.
1.1.1.1 has a priority of 10 and 2.2.2.2 has a priority of 1, therefore I don’t think it became a primary.
Both of them were up and before the writing I saw that 1.1.1.1 was the primary.
Priority 0 ensures that members with this level are unable to become primaries. In this case your cluster would be readonly during the PC1 crash and from what I understand that is what you want.