search with a key-value store

29
Search with a Key-Value Store

Upload: dylan

Post on 12-Jan-2016

29 views

Category:

Documents


0 download

DESCRIPTION

Search with a Key-Value Store. Intro to NoSQL. Key-value store Schemaless Distributed Eventually Consistent. Key-Value. Single unique key for each value in the database Extremely fast look-up Easy distribution (no such thing as joins). Schemaless. Critical for extremely large data sets - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Search with a Key-Value Store

Search with a Key-Value Store

Page 2: Search with a Key-Value Store

Intro to NoSQL

•Key-value store

•Schemaless

•Distributed

•Eventually Consistent

Page 3: Search with a Key-Value Store

Key-Value

•Single unique key for each value in the database

•Extremely fast look-up

•Easy distribution (no such thing as joins)

Page 4: Search with a Key-Value Store

Schemaless

•Critical for extremely large data sets

•No alter table commands, each value has no pre-defined fields

Page 5: Search with a Key-Value Store

Distributed

•Data set is designed to be shared across multiple machines

•Typically makes use of commodity servers with enough RAM to keep the entire data set in memory

Page 6: Search with a Key-Value Store

Eventually Consistent

•Replica nodes are not notified of changes before a success response is returned to the client

•Makes NoSQL problematic for highly sensitive transactions (finance, etc)

Page 7: Search with a Key-Value Store

Database Design in NoSQL

•Denormalization is your friend

•Think of collections as views on a data set that

Page 8: Search with a Key-Value Store

A News Site Using SQL

Users

id

user_name

birthday

Stories

id

date

headline

content

Comment

id

story_id

user_id

content

Page 9: Search with a Key-Value Store

Loading a Story with SQL

SELECT * FROM stories

SELECT * FROM comments LEFT JOIN users ON users.id = comments.user_id

LEFT JOIN comments children ON children.parent_id = comments.id

WHERE story_id = x

Page 10: Search with a Key-Value Store

Redesigned in a NoSQL Data Store

Story #dgi3ck

dateheadlinecontent

commentsComment #la529

contentusername

user_image_urluser_idchildren

Comment #mn34icontent

usernameuser_image_url

user_idComment #5bg26content

usernameuser_image_url

user_idchildren

Page 11: Search with a Key-Value Store

Loading a Story with NoSQL

Stories::get(dgi3ck)

Page 12: Search with a Key-Value Store

Some Design Considerations

•What is the context in which we will access this data?

•What data do we need to access outside the of this context?

•How often does the data change?

Page 13: Search with a Key-Value Store

Embedded Data

•NoSQL can support foreign keys

•Some data is more appropriately stored “embedded” in a parent context

•E.g. Comments are rarely (if ever) accessed outside of their parent Story

Page 14: Search with a Key-Value Store

Cached Data

•Data from an object that needs to be accessed outside of the current context can be cached

•Keep in mind that it may need to be updated

•E.g. a user changes his username, Comments can be updated

Page 15: Search with a Key-Value Store

Several common NoSQL Stores

•Memcached

•BigTable

•SimpleDB

•MongoDB

Page 16: Search with a Key-Value Store

Why we chose MongoDB

•Auto-sharding and easy setup for distribution

•JavaScript API

•Powerful indexing capabilities

Page 17: Search with a Key-Value Store

MongoDB Libraries

•ORM: mongo_mapper

• https://github.com/jnunemaker/mongomapper

•Underlying Connection: mongo

• https://github.com/mongodb/mongo-ruby-driver

•BSON support: bson_ext

• http://rubygems.org/gems/bson_ext

Page 18: Search with a Key-Value Store

Lifebooker’s Availability Search

• Searches across Services

• Filters

• Time/Date

• Geographical Zone

• Service Category

• Practitioner Gender

• Concurrent Availability

• (and several more)

Page 19: Search with a Key-Value Store

Services, Discounts and Practitioners

•Services are offered by Providers

•Providers have Practitioners (Employees)

•Discounts are applied to Providers for a Service in a given time

Page 20: Search with a Key-Value Store

Modeling this Data in MongoDB

Page 21: Search with a Key-Value Store

Embedding with MongoMapper

Page 22: Search with a Key-Value Store

Indexing and Searching

•Mongo offers powerful indexing capabilities

•Arrays are “first-class citizens”

•Complex indices allow for great performance

Page 23: Search with a Key-Value Store

Creating Meta-Data

•With complex data structures, creating meta-data before_save will allow you to make that data easily searchable

•E.g. the maximum discount on a given day for a service

Page 24: Search with a Key-Value Store

Creating Indices

Page 25: Search with a Key-Value Store

Querying

•Uses DataMapper/Arel Syntax

•Chains conditions, ordering and offset

Page 26: Search with a Key-Value Store

Filtering Complex Data Structures

•MongoDB offers a JavaScript API for MapReduce

•Map - transform and filter data

•Reduce - combine multiple rows into a single record

Page 27: Search with a Key-Value Store

A Simple Use-Case

Page 28: Search with a Key-Value Store

Using MapReduce to Filter

Filter

Page 29: Search with a Key-Value Store

The Results•Scheduled to go live within 2 weeks

•With sharding/distribution, tests show almost no dip in response time with more than 10x the current data set

•20x faster than MySQL implementation

•100ms vs 2000ms (or more)