Transcript
Page 1: Polyglot Persistence - Two Great Tastes That Taste Great Together

Polyglot PersistenceTwo Great Tastes

That Taste Great Together!

John [email protected]

@johnpwood

Page 2: Polyglot Persistence - Two Great Tastes That Taste Great Together

About Me

● Software Developer at Interactive Mediums● Primarily work on a web application that allows

our customers to engage and interact with their customers

● Writing code for about 15 years● Tinkering with NoSQL for about 1.5 years● Have a NoSQL solution that has been running

in production for a year

Page 3: Polyglot Persistence - Two Great Tastes That Taste Great Together

You Now Have A Choice

Page 4: Polyglot Persistence - Two Great Tastes That Taste Great Together

You Now Have A Choice

Page 5: Polyglot Persistence - Two Great Tastes That Taste Great Together

You Now Have A Choice

Page 6: Polyglot Persistence - Two Great Tastes That Taste Great Together

You Now Have A Choice

Page 7: Polyglot Persistence - Two Great Tastes That Taste Great Together

You Now Have A Choice

Page 8: Polyglot Persistence - Two Great Tastes That Taste Great Together

You Now Have A Choice

Page 9: Polyglot Persistence - Two Great Tastes That Taste Great Together

You Now Have A Choice

Page 10: Polyglot Persistence - Two Great Tastes That Taste Great Together

You Now Have A Choice

Page 11: Polyglot Persistence - Two Great Tastes That Taste Great Together

You Now Have A Choice

Page 12: Polyglot Persistence - Two Great Tastes That Taste Great Together

You Now Have A Choice

Page 13: Polyglot Persistence - Two Great Tastes That Taste Great Together

The RDBMS Is No Longer The Default Choice

Page 14: Polyglot Persistence - Two Great Tastes That Taste Great Together

The RDBMS Is No Longer The Default Choice

● Can be very difficult to scale horizontally● Schemas can be difficult to maintain and

migrate● For some applications, the data integrity

features of the RDBMS are an unnecessary overhead

● Data constraints and JOINs can be expensive at runtime

Page 15: Polyglot Persistence - Two Great Tastes That Taste Great Together

NoSQL Databases Have Stepped Up To Address These Issues

Page 16: Polyglot Persistence - Two Great Tastes That Taste Great Together

NoSQL Databases Have Stepped Up To Address These Issues

● Schema-less● Little to no data integrity enforcement● Self-contained data● Eventually consistent● Easy to scale horizontally to add processing

power and storage

Page 17: Polyglot Persistence - Two Great Tastes That Taste Great Together

But The RDBMS Is Far From Dead

Page 18: Polyglot Persistence - Two Great Tastes That Taste Great Together

But The RDBMS Is Far From Dead

● Incredibly mature, and battle tested● Immediate and constant consistency● Integrity of data is enforced● Efficient use of storage space if data

normalized properly● Supported by everyone and everything (tools,

frameworks, libraries, etc)● Incredibly flexible and powerful query language● Help is plentiful and easy to find

Page 19: Polyglot Persistence - Two Great Tastes That Taste Great Together

Choice is good...right?

Page 20: Polyglot Persistence - Two Great Tastes That Taste Great Together

Decisions, Decisions...

Page 21: Polyglot Persistence - Two Great Tastes That Taste Great Together

You Don't Have to Choose

Page 22: Polyglot Persistence - Two Great Tastes That Taste Great Together

“You've got your chocolate in my peanut butter!”

Page 23: Polyglot Persistence - Two Great Tastes That Taste Great Together

Polyglot Persistence

Page 24: Polyglot Persistence - Two Great Tastes That Taste Great Together

pol●y●glot - AdjectiveKnowing or using several languages

Page 25: Polyglot Persistence - Two Great Tastes That Taste Great Together

pol●y●glot - AdjectiveKnowing or using several languages

per●sist●ence - NounThe continued or prolonged existence of

something

Page 26: Polyglot Persistence - Two Great Tastes That Taste Great Together

Polyglot PersistenceThe continued or prolonged existence of

something using several languages

Page 27: Polyglot Persistence - Two Great Tastes That Taste Great Together

Polyglot PersistenceThe continued or prolonged existence of

something using several languagesdatabases

Page 28: Polyglot Persistence - Two Great Tastes That Taste Great Together

“Polyglot Persistence, like polyglot programming, is all

about choosing the right persistence option for the task at

hand.” - Scott Leberknight, October, 2008

http://www.nearinfinity.com/blogs/scott_leberknight/polyglot_persistence.html

Page 29: Polyglot Persistence - Two Great Tastes That Taste Great Together

Why On Earth Would You Want To Do This?

Page 30: Polyglot Persistence - Two Great Tastes That Taste Great Together

CAP Theorem

http://en.wikipedia.org/wiki/CAP_theorem

Page 31: Polyglot Persistence - Two Great Tastes That Taste Great Together

http://blog.nahurst.com/visual-guide-to-nosql-systems

Page 32: Polyglot Persistence - Two Great Tastes That Taste Great Together

Compromise

Page 33: Polyglot Persistence - Two Great Tastes That Taste Great Together

Consistency and Data Integrity

+Scalability and

Flexibility

Page 34: Polyglot Persistence - Two Great Tastes That Taste Great Together

Support A Wide Range of Storage

Requirements

Page 35: Polyglot Persistence - Two Great Tastes That Taste Great Together

Get The Job Done Faster, With Better

Quality

Page 36: Polyglot Persistence - Two Great Tastes That Taste Great Together

DB Doesn't Just Stand For Database

Page 37: Polyglot Persistence - Two Great Tastes That Taste Great Together

Don't Swim Upstream

Page 38: Polyglot Persistence - Two Great Tastes That Taste Great Together

Possible Use Cases

Page 39: Polyglot Persistence - Two Great Tastes That Taste Great Together

Use A NoSQL Database For A Particular

Application Feature

Page 40: Polyglot Persistence - Two Great Tastes That Taste Great Together

Use A NoSQL Database For Speedy Batch

Processing

Page 41: Polyglot Persistence - Two Great Tastes That Taste Great Together

Use A NoSQL Database For Distributed Logging

Page 42: Polyglot Persistence - Two Great Tastes That Taste Great Together

Use A NoSQL Database For Large Tables

Page 43: Polyglot Persistence - Two Great Tastes That Taste Great Together

Use A RDBMS For Reporting

Page 44: Polyglot Persistence - Two Great Tastes That Taste Great Together

Sounds Great!What's The Catch?

Page 45: Polyglot Persistence - Two Great Tastes That Taste Great Together

Difficult For Data In Different Databases To

Interact

Page 46: Polyglot Persistence - Two Great Tastes That Taste Great Together

You Now Have To Decide Where To Store

Data

Page 47: Polyglot Persistence - Two Great Tastes That Taste Great Together

Increased Application And Deployment

Complexity

Page 48: Polyglot Persistence - Two Great Tastes That Taste Great Together

Additional Administrative

Responsibilities

Page 49: Polyglot Persistence - Two Great Tastes That Taste Great Together

Training

Page 50: Polyglot Persistence - Two Great Tastes That Taste Great Together
Page 51: Polyglot Persistence - Two Great Tastes That Taste Great Together

What Will This Do To My Beautiful Code?

Page 52: Polyglot Persistence - Two Great Tastes That Taste Great Together

It's All About The Layers

Page 53: Polyglot Persistence - Two Great Tastes That Taste Great Together

class User < ActiveRecord::Baseend

class ContestEntry < CouchRest::ExtendedDocument property :entry_numberend

Page 54: Polyglot Persistence - Two Great Tastes That Taste Great Together

class User < ActiveRecord::Base def contest_entries ContestEntry.entries_for_user(self.id) endend

class ContestEntry < CouchRest::ExtendedDocument property :entry_number property :user_id

def self.entries_for_user(user_id) # Execute your view to fetch the contest entries end

def user User.fi nd_by_id(user_id) endend

Page 55: Polyglot Persistence - Two Great Tastes That Taste Great Together

Additional Options Available

Page 56: Polyglot Persistence - Two Great Tastes That Taste Great Together

So, Who Is Actually Doing This?

Page 57: Polyglot Persistence - Two Great Tastes That Taste Great Together
Page 58: Polyglot Persistence - Two Great Tastes That Taste Great Together

● Primary MySQL database with a backup● A few very large tables, containing 5M – 30M

rows each, and growing quickly● Increasing query execution time● Some pages on the web app were timing out● Increasing database migration time● Rigid schema of the RDBMS was preventing

some planned features from moving forward

Page 59: Polyglot Persistence - Two Great Tastes That Taste Great Together

● Brought in a consultant to help us optimize our MySQL setup

● Optimized slow queries● Added some indexes● Offloaded some work to the backup database● Considered the use of summary tables for

statistics

Page 60: Polyglot Persistence - Two Great Tastes That Taste Great Together

+

Page 61: Polyglot Persistence - Two Great Tastes That Taste Great Together

● Migrated old data from large tables to CouchDB● Using CouchDB views to aggregate summary

data● Data is imported and views are updated nightly● Queries for statistics now very fast● Using Lucene (via couchdb-lucene) for full text

searching● Taking full advantage of CouchDBs schema-

less nature in several new application features

Page 62: Polyglot Persistence - Two Great Tastes That Taste Great Together

It's Not All Rainbows And Unicorns

Page 63: Polyglot Persistence - Two Great Tastes That Taste Great Together

● CouchDB databases and views can be very large on disk

● Some queries could not be substituted with CouchDB views

● Indexing tens of millions of documents for full text search with Lucene takes weeks

● Development takes longer, as the map/reduce model requires additional thought and planning

● Changing/Upgrading views in production not straightforward

http://www.couch.io/migrating-to-couchdb

Page 64: Polyglot Persistence - Two Great Tastes That Taste Great Together
Page 65: Polyglot Persistence - Two Great Tastes That Taste Great Together

http://twitter.com/about/opensource

Page 66: Polyglot Persistence - Two Great Tastes That Taste Great Together
Page 67: Polyglot Persistence - Two Great Tastes That Taste Great Together

● Vertically and horizontally partitioned MySQL● Several layers of aggressive caching, all

application managed● Schema changes impossible, resulting in the

use of bitfields and piggyback tables● Hardware intensive● Error prone● Hitting MySQL limits● Already eventually consistent

Page 68: Polyglot Persistence - Two Great Tastes That Taste Great Together

FlockDB

HBase

Page 69: Polyglot Persistence - Two Great Tastes That Taste Great Together

● Migrating from MySQL to Cassandra as their main online data store

● Hadoop/HBase used for people search feature● FlockDB used to manage the social graph● Hadoop for analytics● “As with all NoSQL systems, strengths in

different situations” - Kevin Weil, Analytics Lead, Twitter

http://www.slideshare.net/kevinweil/nosql-at-twitter-nosql-eu-2010

Page 70: Polyglot Persistence - Two Great Tastes That Taste Great Together

● Increased availability● The ability to support new features● The ability to analyze their massive amount of

data in a reasonable amount of time

http://www.slideshare.net/kevinweil/nosql-at-twitter-nosql-eu-2010

Page 71: Polyglot Persistence - Two Great Tastes That Taste Great Together

Right Tool For The Job

Page 72: Polyglot Persistence - Two Great Tastes That Taste Great Together

Thanks!

[email protected]@johnpwood