Database Reference
In-Depth Information
Now let's start making use of our new tags! We can set a read preference of the nearest member of our replica set
in site a .
$m->setReadPreference(MongoClient::RP_NEAREST, array( array('site' => 'a'),));
Now that we have tackled read preference, let's start on write concern. Write concern is slightly more complex,
as we first need to modify our replica set configuration to add the extra getLastErrorModes . In this case we want to
create a write concern stating that a given write must be committed to enough nodes to have been written to two
different sites. This means a write must at minimum be committed to site a and site b . To do this we need to set the
getLastErrorModes variable to be a document that contains the name of our new write concern and a rule that says
we want it written to two different “site” tags. This is done as follows:
conf = rs.conf()
conf.settings. getLastErrorModes = { bothSites : { "site": 2 } } }
rs.reconfig(conf)
Now we need to insert our document and specify our new write concern.
$col->insert($document, array("w" => "bothSites"));
It's as easy as that. Now we can guarantee that our writes are committed to both sites! Now, let's say we want to
make this the default write concern for any write made to our cluster.
conf = rs.conf()
conf.settings.getLastErrorDefaults = { bothSites : 1 } }
rs.reconfig(conf)
Now any writes we make will be made using the default write concern of bothSites . So if we just perform a
vanilla insert!
Summary
MongoDB provides a rich set of tools for implementing redundant and robust replication topologies. In this chapter,
you learned about many of these tools, including some of the reasons and motivations for using them. You also
learned how to set up many different replica set topologies. Additionally, you learned how to inspect the status of
replication systems using both the command-line tools and the built-in web interface. And finally, you learned how
to set up and configure read preferences and write concerns to ensure that you are reading from and writing to the
correct places.
Please take the time required to evaluate each of the options and functions described in this chapter to make sure
you build a replica set best suited to your particular needs before attempting to use one in a production environment.
It is incredibly easy to use MongoDB to create test beds on a single machine; just as we have done in this chapter.
Therefore you are strongly encouraged to experiment with each method to make sure that you fully understand the
benefits and limitations of each approach, including how it will perform with your particular data and application.
 
Search WWH ::




Custom Search