Reader small image

You're reading from  MongoDB 4 Quick Start Guide

Product typeBook
Published inSep 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781789343533
Edition1st Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Doug Bierer
Doug Bierer
author image
Doug Bierer

Doug Bierer has been hooked on computers since his first program, written on a DEC PDP-8, in 1971. In his wide-ranging career, he has been a professional contract programmer since 1978, having written applications in BASIC, PL/I, assembler, FORTH, C, C++, dBase/FoxBase/Clipper, Pascal, Perl, Java, and PHP. He deployed his first website in 1993 while living in San Francisco. He speaks four languages, has traveled extensively, and now resides in Thailand. He also spent some years doing system administration and TCP/IP networking. Some of his technical works include PHP 7 Programming Cookbook and Learning MongoDB 4.x (Packt), as well as Learning PHP and MySQL, Learning PHP Security and Learning Doctrine (O'Reilly Media).
Read more about Doug Bierer

Right arrow

Maintaining MongoDB Performance

The features covered in this chapter will show you how to improve performance by creating and using indexes, and how to safeguard data using replication and backups. In addition, you will learn how to handle massive amounts of data with sharding.

The topics that are going to be covered in this chapter are as follows:

  • Indexes
  • Simple backup and restore
  • Replication
  • Sharding

Indexes

Command summary:

db.collection.createIndex( { <fieldname> : ( 1 | -1 ) } );

Creating indexes is an easy way to improve MongoDB performance at the collection level. Indexes can be created on a single field, multiple fields, or embedded fields within arrays or objects. When you issue a query which involves the indexed field, MongoDB is able to use information stored in the index rather than having to do a full scan of all database documents. In this sense, you can think of the index as a shortcut which saves time when producing query results.

There are three index types supported by MongoDB: single field (https://docs.mongodb.com/manual/core/index-single/#single-field-indexes), compound (https://docs.mongodb.com/manual/core/index-compound/#compound-indexes), and multi-key (https://docs.mongodb.com/manual/core/index-multikey/#multikey-indexes).

Each of these can be...

Simple backup and restore

Command summary (from the command line):

  • mongodump
  • mongorestore

Hopefully there is no need to stress how important it is to maintain a regular backup schedule. That being said, it is worth mentioning that doing a simple restore can actually cause problems in a properly constructed system of replicas or shards (covered later on in this chapter). In this section, we will address a simple backup of the MongoDB database that's residing on a single server.

mongodump

A backup can be as simple as issuing the command mongodump. This command will create a binary export of either a mongod or a mongos instance. Here is the output for the MongoDB instance, which is used to demonstrate concepts in this...

Replication

Command summary:

  • rs.initiate( { _id: "<replicaSetName>", members: [ { _id: N, host: "<host>" } ] )
  • rs.add( "<hostname>" | { host: "<hostname>" } )
  • rs.remove( "<hostname>")
  • rs.conf()
  • rs.status()

The purpose of replication is to provide immediate online redundancy so that there is no loss of service in case of failure. Before we get into the mechanics of how to create and use replicas, it's important to gain an understanding of what MongoDB replication is and how it operates.

Here are a couple of related topics which are beyond the scope of this book, but which might be of interest:
Change streams (https://docs.mongodb.com/manual/changeStreams/#change-streams) allows applications to subscribe to real-time changes in the data by using a Publish-Subscribe design.
Arbiters (https://docs...

Sharding

Command summary:

  • sh.addShard( "host:port" )
  • sh.enableSharding( "database" )
  • sh.shardCollection( "database.collection", { field: direction } )
  • sh.status()

Whereas replication is designed to provide redundancy of data, sharding is a feature which provides horizontal scaling. This means that instead of having a single extremely powerful server (vertical scaling), you spread the data across multiple servers operating in a cluster. This facility gives you the ability to handle massive amounts of data and lends itself to the cloud environment. Not only that, but because you have a subset of the entire database residing on multiple servers, each server is then able to handle its own database tasks, thus providing a means of load balancing.

Before we cover how to implement a sharded cluster, it's important to understand how sharding operates...

Summary

In this chapter, you learned about maintenance and performance. You first learned about simple backup and restore using mongodump and mongorestore. You then went on to learn about creating indexes, including single field, multiple field, and multi-key. After that, you learned about replica sets, that is, primary and secondaries, how to deploy them, and adding or removing members. Finally, you learned about sharding, config servers, shard servers, range and hashed sharding strategies, and how to choose a shard key.

In the next chapter, you will learn various security-related considerations, in addition to learning how to secure a MongoDB database.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
MongoDB 4 Quick Start Guide
Published in: Sep 2018Publisher: PacktISBN-13: 9781789343533
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Doug Bierer

Doug Bierer has been hooked on computers since his first program, written on a DEC PDP-8, in 1971. In his wide-ranging career, he has been a professional contract programmer since 1978, having written applications in BASIC, PL/I, assembler, FORTH, C, C++, dBase/FoxBase/Clipper, Pascal, Perl, Java, and PHP. He deployed his first website in 1993 while living in San Francisco. He speaks four languages, has traveled extensively, and now resides in Thailand. He also spent some years doing system administration and TCP/IP networking. Some of his technical works include PHP 7 Programming Cookbook and Learning MongoDB 4.x (Packt), as well as Learning PHP and MySQL, Learning PHP Security and Learning Doctrine (O'Reilly Media).
Read more about Doug Bierer