Database Reference
In-Depth Information
1.2.3
Secondary indexes
The best way to understand database indexes is by analogy: many topics have indexes
mapping keywords to page numbers. Suppose you have a cookbook and want to find
all recipes calling for pears (maybe you have a lot of pears and don't want them to go
bad). The time-consuming approach would be to page through every recipe, check-
ing each ingredient list for pears. Most people would prefer to check the topic's index
for the pears entry, which would give a list of all the recipes containing pears. Database
indexes are data structures that provide this same service.
Secondary indexes in MongoDB are implemented as B-trees . B-tree indexes, also
the default for most relational databases, are optimized for a variety of queries, includ-
ing range scans and queries with sort clauses. By permitting multiple secondary
indexes, MongoDB allows users to optimize for a wide variety of queries.
With MongoDB, you can create up to 64 indexes per collection. The kinds of
indexes supported include all the ones you'd find in an RDMBS ; ascending, descend-
ing, unique, compound-key, and even geospatial
indexes are supported. Because MongoDB and most
RDBMS s use the same data structure for their indexes,
advice for managing indexes in both of these systems is
compatible. We'll begin looking at indexes in the next
chapter, and because an understanding of indexing is
so crucial to efficiently operating a database, I devote all
of chapter 7 to the topic.
1. A working replica set
Secondary
Secondary
Primary
1.2.4
Replication
MongoDB provides database replication via a topology
known as a replica set . Replica sets distribute data across
machines for redundancy and automate failover in the
event of server and network outages. Additionally, repli-
cation is used to scale database reads. If you have a read-
intensive application, as is commonly the case on the
web, it's possible to spread database reads across
machines in the replica set cluster.
Replica sets consist of exactly one primary node and
one or more secondary nodes. Like the master-slave
replication that you may be familiar with from other
databases, a replica set's primary node can accept both
reads and writes, but the secondary nodes are read-only.
What makes replica sets unique is their support for
automated failover: if the primary node fails, the cluster
will pick a secondary node and automatically promote it
to primary. When the former primary comes back
online, it'll do so as a secondary. An illustration of this
process is provided in figure 1.3.
I discuss replication in chapter 8.
2. Original primary node fails and
a secondary is promoted to primary
Secondary
Primary
X
3. Original primary comes back online
as a secondary
Primary
Secondary
Secondary
Figure 1.3 Automated failover
with a replica set
Search WWH ::




Custom Search