There are times when, for maintenance activity during business hours, we need to take a server out from the replica set, perform the maintenance, and put it back in the replica set. If the server to be worked upon is the primary, we somehow need to step down from the primary member position, conduct a re-election, and ensure that it doesn't get re-elected for a minimum given time frame. After the server becomes a secondary once the step down operation is successful, we can take it out of the replica set, perform the maintenance activity, and put it back in the replica set.
Refer to the Starting multiple instances as part of a replica set recipe in Chapter 1, Installing and Starting the MongoDB Server, for the prerequisites and to know about replica set basics. Set up a simple three-node replica set on your computer as mentioned in the recipe.
Assuming that at this point of time we have a replica set up and running, perform the following steps:
> rs.status()
> rs.stepDown()
> rs.status()
The steps we saw in the previous section are pretty simple, but there are a couple of interesting things that we will see.
The rs.stepDown()
method did not have any parameter. The function can in fact take a numeric value, the number of seconds for which the instance stepped down won't participate in the elections and won't become a primary; the default value for this is 60 seconds.
Another interesting thing to try out is, what if the instance that was asked to step down has a higher priority than other instances? Well, it turns out that the priority doesn't matter when you step down. The instance stepped down will not become primary, no matter what, for the provided number of seconds. However, if the priority is set for the instance stepped down, and it is higher than others, then after the time given to step down elapses, an election will happen, and the instance with the higher priority will become primary again.