MongoDB Applied Design Patterns
Rick Copeland
Beijing Cambridge Farnham Kln Sebastopol Tokyo
Download from Wow! eBook
Special Upgrade Offer
If you purchased this ebook directly from oreilly.com, you have the following benefits:
DRM-free ebooksuse your ebooks across devices without restrictions or limitations
Multiple formatsuse on your laptop, tablet, or phone
Lifetime access, with free updates
Dropbox syncingyour files, anywhere
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Please note that upgrade offers are not available from sample content.
Preface
Whether youre building the newest and hottest social media website ordeveloping an internal-use-only enterprise business intelligence application,scaling your data model has never been more important.Traditional relationaldatabases, while familiar, present significant challenges and complications whentrying to scale up to such big data needs.Into this world steps MongoDB, aleading NoSQL database, to address these scaling challenges while alsosimplifying the process of development.
However, in all the hype surrounding big data, many sites have launched theirbusiness on NoSQL databases without an understanding of the techniques necessaryto effectively use the features of their chosen database.This book provides the much-needed connection between the features of MongoDB andthe business problems that it is suited to solve.The books focus on thepractical aspects of the MongoDB implementation makes it an ideal purchase fordevelopers charged with bringing MongoDBs scalability to bear on the particularproblem youve been tasked to solve.
Audience
This book is intended for those who are interested in learning practical patternsfor solving problems and designing applications using MongoDB. Although most ofthe features of MongoDB highlighted in this book have a basic description here,this is not a beginning MongoDB book. For such an introduction, the reader wouldbe well-served to start with MongoDB: The Definitive Guide by Kristina Chodorow and Michael Dirolf (OReilly) or, for a Python-specific introduction, MongoDB and Python by Niall OHiggins (OReilly).
Assumptions This Book Makes
Most of the code examples used in this book are implemented using either the Pythonor JavaScript programming languages, so a basic familiarity with their syntax isessential to getting the most out of this book.Additionally, many of the examples and patterns are contrasted with approaches tosolving the same problems using relational databases, so basic familiarity withSQL and relational modeling is also helpful.
Contents of This Book
This book is divided into two parts, with Part I focusing on general MongoDBdesign patterns and Part II applying those patterns to particular problem domains.
Part I: Design Patterns
Part I introduces the reader to some generally applicable design patterns inMongoDB.These chapters include more introductory material than Part II, and tend to focusmore on MongoDB techniques and less on domain-specific problems.The techniques described here tend to make use of MongoDB distinctives, orgenerate a sense of hey, MongoDB cant do that as you learn that yes,indeed, it can.
: To Embed or Reference This chapter describes what kinds of documents can be stored in MongoDB, and illustrates the trade-offs between schemas that embed related documents within related documents and schemas where documents simply reference one another by ID. It will focus on the performance benefits of embedding, and when the complexity added by embedding outweighs the performance gains. : Polymorphic Schemas This chapter begins by illustrating that MongoDB collections are schemaless, with the schema actually being stored in individual documents. It then goes on to show how this feature, combined with document embedding, enables a flexible and efficient polymorphism in MongoDB. : Mimicking Transactional Behavior This chapter is a kind of apologia for MongoDBs lack of complex, multidocument transactions. It illustrates how MongoDBs modifiers, combined with document embedding, can often accomplish in a single atomic document update what SQL would require several distinct updates to achieve. It also explores a pattern for implementing an application-level, two-phase commit protocol to provide transactional guarantees in MongoDB when they are absolutely required.
Part II: Use Cases
In Part II, we turn to the applied part of Applied Design Patterns , showingseveral use cases and the application of MongoDB patterns to solvingdomain-specific problems.Each chapter here covers a particular problem domain and the techniques andpatterns used to address the problem.
: Operational Intelligence This chapter describes how MongoDB can be used for operational intelligence, or real-time analytics of business data. It describes a simple event logging system, extending that system through the use of periodic and incremental hierarchical aggregation. It then concludes with a description of a true real-time incremental aggregation system, the Mongo Monitoring Service (MMS), and the techniques and trade-offs made there to achieve high performance on huge amounts of data over hundreds of customers with a (relatively) small amount of hardware. : Ecommerce This chapter begins by describing how MongoDB can be used as a product catalog master, focusing on the polymorphic schema techniques and methods of storing hierarchy in MongoDB. It then describes an inventory management system that uses optimistic updating and compensation to achieve eventual consistency even without two-phase commit. : Content Management Systems This chapter describes how MongoDB can be used as a backend for a content management system. In particular, it focuses on the use of polymorphic schemas for storing content nodes, the use of GridFS and Binary fields to store binary assets, and various approaches to storing discussions. : Online Advertising Networks This chapter describes the design of an online advertising network. The focus here is on embedded documents and complex atomic updates, as well as making sure that the storage engine (MongoDB) never becomes the bottleneck in the ad-serving decision. It will cover techniques for frequency capping ad impressions, keyword targeting, and keyword bidding. : Social Networking This chapter describes how MongoDB can be used to store a relatively complex social graph, modeled after the Google+ product, with users in various circles, allowing fine-grained control over what is shared with whom. The focus here is on maintaining the graph, as well as categorizing content into various timelines and news feeds. : Online Gaming This chapter describes how MongoDB can be used to store data necessary for an online, multiplayer role-playing game. We show how character and world data can be stored in MongoDB, allowing for concurrent access to the same data structures from multiple players.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic Indicates new terms, URLs, email addresses, filenames, and file extensions. Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.
Next page