40
Got Documents? AN EXPLORATION OF DOCUMENT DATABASES IN SOFTWARE ARCHITECTURE

Got documents Code Mash Revision

Embed Size (px)

Citation preview

Page 1: Got documents Code Mash Revision

Got Documents?AN EXPLORATION OF DOCUMENT DATABASES IN SOFTWARE ARCHITECTURE

Page 2: Got documents Code Mash Revision

About [email protected]@maggiepinthttps://www.tempworks.com

Page 3: Got documents Code Mash Revision
Page 4: Got documents Code Mash Revision

FlavorsMONGODB, COUCHDB, RAVENDB, AND MORE

Page 5: Got documents Code Mash Revision

MongoDB•Dominant player in document databases

•Runs on nearly all platforms

•Strongly Consistent in default configuration

•Indexes are similar to traditional SQL indexes in nature

•Stores data in customized Binary JSON (BSON) format that allows typing

•Limit support for cross-collection querying in latest release

•Client API’s available in tons of languages

•Must use a third party provider like SOLR for advanced search capabilities

Page 6: Got documents Code Mash Revision

CouchDB•Stores documents in plain JSON format

•Eventually consistent

•Indexes are map-reduce and defined in Javascript

•Clients in many languages

•Runs on Linux, OSX and Windows

•CouchDB-Lucene provides a Lucene integration for search

Page 7: Got documents Code Mash Revision

RavenDB•Stores documents in plain JSON format

•Eventually consistent

•Indexes are built on Lucene. Lucene search is native to RavenDB.

•Server only runs on Windows

•.NET, Java, and HTTP Clients

•Limited support for cross-collection querying

Page 8: Got documents Code Mash Revision

Other Players•Azure DocumentDB• Very new product from Microsoft

•ReactDB• Open source project that integrates push notifications into the database

•Cloudant• IBM proprietary implementation of CouchDB

•DynamoDB• Mixed model key value and document database

Page 9: Got documents Code Mash Revision

Architectural Considerations

Page 10: Got documents Code Mash Revision

How do document databases work?•Stores related data in a single document

•Usually uses JSON format for documents

•Enables the storage of complex object graphs together, instead of normalizing data out into tables

•Stores documents in collections of the same type

•Allows querying within collections

•Does not typically allow querying across collections

•Offers high availability at the cost of consistency

Page 11: Got documents Code Mash Revision

Consideration: Schema FreePROS

Easy to add properties

Simple migrations

Tolerant of differing data

CONS

Have to account for properties being missing

Page 12: Got documents Code Mash Revision

ACID Atomicity

◦ Each transaction is all or nothing

Consistency◦ Any transaction brings the database from one valid state to another

Isolation◦ System ensures that transactions operated concurrently bring the database to the same state as if they

had been operated serially

Durability◦ Once a transaction is committed, it remains so even in the event of power loss, etc

Page 13: Got documents Code Mash Revision

ACID in Document Databases•Traditional transaction support is not available in any document database

•Document databases do support something like transactions within the scope of a document

•This makes document databases generally inappropriate for a wide variety of applications

Page 14: Got documents Code Mash Revision

Consideration: Non-AcidPROS

Performance Gain

CONS

No way to guarantee that operations across succeed or fail together

No isolation when sharded

Various implementation specific issues

Page 15: Got documents Code Mash Revision

Case Study: Survey System

Page 16: Got documents Code Mash Revision

Requirements•An administration area is used to define ‘Surveys’.• Surveys have Questions• Questions have answers

•Surveys can be administrated in sets called workflows

•When a survey changes, this change can only apply to surveys moving forward• Because of this, each user must receive a survey ‘instance’ to track the version of the survey he/she got

Page 17: Got documents Code Mash Revision

A Traditional SQL Schema•With various other requirements not described here, this schema came out to 83 tables

•For one of our heaviest usage clients, the average user would have 119 answers in the ‘Saved Answer’ table

•With over 200,000 users after two years of use, the ‘Saved Answer’ table had 24,014,330 rows

•This table was both read and write heavy, so it was extremely difficult to define effective SQL indexes

•The hardware cost for these SQL servers was astronomical

•This sucked

Page 18: Got documents Code Mash Revision

Designing Documents•An aggregate is a collection of objects that can be treated as one

•An aggregate root is the object that contains all other objects inside of it

•When designing document schema, find your aggregates and create documents around them

•If you have an entity, it should be persisted as it’s own document because you will likely have to store references to it

Page 19: Got documents Code Mash Revision

Survey System Design•A combination SQL and Document DB design was used

•Survey Templates (one type of entity) were put into the SQL Database

•When a survey was assigned to a user as part of a workflow (another entity, and also an aggregate), it’s data at that time was put into the document database

•The user’s responses were saved as part of the workflow document

•Reading a user’s application data became as simple as making one request for her workflow document

Page 20: Got documents Code Mash Revision

Consideration: Models Aggregates WellPROS

Improves performance by reducing lookups

Allows for easy persistence of object oriented designs

CONS

none

Page 21: Got documents Code Mash Revision

Sharding•Sharding is the practice of distributing data across multiple servers

•All major document database providers support sharding natively

•Document Databases are ideal for sharding because document data is self contained (less need to worry about a query having to run on two servers)

•Sharding is usually accomplished by selecting a shard key for a collection, and allowing the collection to be distributed to different nodes based on that key

•Tenant Id and geographic regions are typical choices for shard keys

Page 22: Got documents Code Mash Revision

Replication•All major document database providers support replication

•In most replication setups, a primary node takes all write operations, and a secondary node asynchronously replicates these write operations

•In the event of a failure of the primary, the secondary begins to take write operations

•MongoDB can be configured to allow reads from secondaries as a performance optimization, resulting in eventual instead of strong consistency

Page 23: Got documents Code Mash Revision

Consideration: Scaling OutPROS

Allows hardware to be scaled horizontally

Ensures very high availability

CONS

Consistency is sacrificed

Page 24: Got documents Code Mash Revision

Survey System: End Result•Each user is associated with about 20 documents

•Documents are distributed across multiple databases using sharding

•Master/Master replication is used to ensure extremely high availability

•There have been no database performance issues in the year and a half the app has been in production

•Because there is no schema migration concern, deploying updates has been drastically simplified

•Hardware cost is reasonable (but not cheap)

Page 25: Got documents Code Mash Revision
Page 26: Got documents Code Mash Revision

Indexes•All document databases support some form of indexing to improve query performance

•Some document databases do not allow querying without an index

•In general, you shouldn’t query without an index anyways

Page 27: Got documents Code Mash Revision

Consideration: IndexesPROS

Improve performance of queries

CONS

Queries cannot reasonably be issued without an index so indexes must frequently be defined and deployed

Page 28: Got documents Code Mash Revision
Page 29: Got documents Code Mash Revision

Consideration: Eventual ConsistencyPROS

Optimizes performance by allowing data transfer to be a background process

CONS

Requires entire team to be aware of eventual consistency implications

Page 30: Got documents Code Mash Revision

Case Study 2: CRM

Page 31: Got documents Code Mash Revision

CRM Requirements•Track customers and basic information about them

•Track contacts and basic information about them

•Track sales deals and where they are in the pipeline

•Track orders generated from sales deals

•Track user tasks

Page 32: Got documents Code Mash Revision

Customers and Their Deals•Customers and Deals are both entities, which is to say that they have distinct identity

•For this reason, Deals and Customer should be two separate collections

•There is no native support for cross-collection querying in most Document Databases• The cross-collection querying support in RavenDB doesn’t perform well

Page 33: Got documents Code Mash Revision

Consideration: One document per interactionPROS

Improves performance

Encourages modeling aggregates well

CONS

Not actually achievable in most cases

Page 34: Got documents Code Mash Revision

Searching Deals by Customer Name•The deal document must contain a denormalized customer object with the customer’s ID and name

•We have a choice to make with this denormalization• Allow the denormalization to just be wrong in the event the customer name is changed• Maintain the denormalization when the customer name is changed

Page 35: Got documents Code Mash Revision

Denormalization Considerations•Is stale data acceptable? This is the best option in all cases where it is possible.

•If stale data is unacceptable, how many documents are likely to need update when a change is made? How often are changes going to be made?

•Using an event bus to move denormalization updates to a background process can be very beneficial if failure of an update isn’t critical for the user to know

Page 36: Got documents Code Mash Revision

Consideration: Models Relationships PoorlyPROS

None

CONS

Stale (out of date) data must be accepted in the system

Large amounts of boilerplate code must be written to maintain denormalizations

In certain circumstances a queuing/eventing system is unavoidable

Page 37: Got documents Code Mash Revision
Page 38: Got documents Code Mash Revision

Consideration: AdministrationPROS

Generally less involved than SQL

CONS

Server performance must be monitored

Hardware must be maintained

Index processes must be tuned

Settings must be tweaked

Page 39: Got documents Code Mash Revision

Consideration Recap•Schema Free

•Non-Acid

•Models Aggregates Well

•Scales out well

•All queries must be indexed

•Eventual Consistency

•One document per interaction

•Models relationships poorly

•Requires administration

Page 40: Got documents Code Mash Revision

…nerds like us are allowed to be unironically enthusiastic about stuff… Nerds are allowed to love stuff, like jump-up-and-down-in-the-chair-can’t-control-yourself love it.

-John Green