Database Reference
In-Depth Information
now see how both of these mechanisms work in turn. You should in the process begin
to understand and predict replica set behavior, particularly in failure scenarios.
A
LL
ABOUT
THE
OPLOG
At the heart of MongoDB's replication stands the oplog. The oplog is a capped collec-
tion that lives in a database called
local
on every replicating node and records all
changes to the data. Every time a client writes to the primary, an entry with enough
information to reproduce the write is automatically added to the primary's oplog.
Once the write is replicated to a given secondary, that secondary's oplog also stores a
record of the write. Each oplog entry is identified with a
BSON
timestamp, and all sec-
ondaries use the timestamp to keep track of the latest entry they've applied.
4
To better see how this works, let's look more closely at a real oplog and at the oper-
ations recorded therein. First connect with the shell to the primary node started in
the previous section, and switch to the
local
database:
> use local
switched to db local
The
local
database stores all the replica set metadata and the oplog. Naturally, this
database isn't replicated itself. Thus it lives up to its name; data in the
local
database
is supposed to be unique to the local node and therefore shouldn't be replicated.
If you examine the
local
database, you'll see a collection called
oplog.rs
, which is
where every replica set stores its oplog. You'll also see a few system collections. Here's
the complete output:
> show collections
me
oplog.rs
replset.minvalid
slaves
system.indexes
system.replset
replset.minvalid
contains information for the initial sync of a given replica set
member, and
system.replset
stores the replica set config document.
me
and
slaves
are used to implement write concern, described at the end of this chapter, and
system
.indexes
is the standard index spec container.
First we'll focus on the oplog. Let's query for the oplog entry corresponding to the
topic document you added in the previous section. To do so, enter the following
query. The resulting document will have four fields, and we'll discuss each in turn:
> db.oplog.rs.findOne({op: "i"})
{ "ts" : { "t" : 1296864947000, "i" : 1 }, "op" : "i", "ns" :
"bookstores.books", "o" : { "_id" : ObjectId("4d4c96b1ec5855af3675d7a1"),
"title" : "Oliver Twist" }
}
4
The BSON timestamp is a unique identifier comprising the number of seconds since the epoch and an incre-
menting counter.