78
Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala Alberto Perdomo @albertoperdomo FOSDEM 2013 Graph Processing DevRoom

Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Embed Size (px)

DESCRIPTION

Squire is an app for iPad that provides suggestions of movies and TV shows, based on the user’s preferences. There are also some social features, for instance a user can suggest a movie or TV show to one of his/her contacts. We have developed the backend and web services that support the Squire iPad app. The recommendations system was written in Scala and everything else was developed in Ruby. This architecture tries to combine the best features of the different languages. The Ruby API is easy to maintain, while the recommendations engine written in Scala and Neo4j is powerful, fast and able to handle heavy computations in a reasonable time.

Citation preview

Page 1: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala

Alberto Perdomo@albertoperdomo

FOSDEM 2013Graph Processing DevRoom

Page 2: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

About me

Page 3: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

About me

Co-Founder of Aentos

Page 4: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

About me

Co-Founder of Aentos

Living my dream* (CNY)

Page 5: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

About me

Co-Founder of Aentos

Living my dream* (CNY)

Web developer

Page 6: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013
Page 7: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

d

Page 8: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013
Page 9: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Features

Page 10: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Features

★ Extensive movies & TV-shows data

Page 11: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Features

★ Extensive movies & TV-shows data

★ Info & price on streaming & rent services:

iTunes, Amazon, Hulu, Netflix...

Page 12: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Features

★ Extensive movies & TV-shows data

★ Info & price on streaming & rent services:

iTunes, Amazon, Hulu, Netflix...

★ Social features (contacts, comments)

Page 13: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Features

★ Extensive movies & TV-shows data

★ Info & price on streaming & rent services:

iTunes, Amazon, Hulu, Netflix...

★ Social features (contacts, comments)

★ Ratings

Page 14: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Features

★ Extensive movies & TV-shows data

★ Info & price on streaming & rent services:

iTunes, Amazon, Hulu, Netflix...

★ Social features (contacts, comments)

★ Ratings

★ Suggestions by users

Page 15: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Features

★ Extensive movies & TV-shows data

★ Info & price on streaming & rent services:

iTunes, Amazon, Hulu, Netflix...

★ Social features (contacts, comments)

★ Ratings

★ Suggestions by users

★ System recommendations

Page 16: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

UnconventionalSystem

Architecture*

Page 17: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Ingredients

Page 18: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

✔ MongoDB

Ingredients

Page 19: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

✔ MongoDB

✔ Neo4j

Ingredients

Page 20: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

✔ MongoDB

✔ Neo4j

✔ Ruby

Ingredients

Page 21: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

✔ MongoDB

✔ Neo4j

✔ Ruby

✔ Scala

Ingredients

Page 22: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

⚛ System Overview ⚛iPad

Client

Main API(Ruby)

Recommend. Engine(Scala)

MongoDB Neo4jUsersRatingsMovies and TV shows

All data

Page 23: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

API: Ruby + MongoDB

Page 24: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

API: Ruby + MongoDB

⚛ REST+JSON

Page 25: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

API: Ruby + MongoDB

⚛ REST+JSON

⚛ Accounts & sessions

Page 26: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

API: Ruby + MongoDB

⚛ REST+JSON

⚛ Accounts & sessions

⚛ Movies & TV-Shows

Page 27: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

API: Ruby + MongoDB

⚛ REST+JSON

⚛ Accounts & sessions

⚛ Movies & TV-Shows

⚛ Contacts

Page 28: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

API: Ruby + MongoDB

⚛ REST+JSON

⚛ Accounts & sessions

⚛ Movies & TV-Shows

⚛ Contacts

⚛ Ratings

Page 29: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

API: Ruby + MongoDB

⚛ REST+JSON

⚛ Accounts & sessions

⚛ Movies & TV-Shows

⚛ Contacts

⚛ Ratings

⚛ User suggestions & recommendations

Page 30: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Recommendations: Scala + Neo4j

Page 31: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Recommendations: Scala + Neo4j

⚛ Movies & TV-shows (IDs & rel.)

Page 32: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Recommendations: Scala + Neo4j

⚛ Movies & TV-shows (IDs & rel.)

⚛ Users

Page 33: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Recommendations: Scala + Neo4j

⚛ Movies & TV-shows (IDs & rel.)

⚛ Users

⚛ Ratings

Page 34: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Communication (RabbitMQ)

iPad Client

Main API(Ruby)

Recommend. Engine(Scala)

MongoDB Neo4j

RabbitMQ + JSON

JSON array of IDs

GET /suggestions

Query params in JSON

UsersRatingsMovies and TV shows

JSON array of data

Retrieve datafor response Collect IDs of suggestions

All dataGraph reset*

Page 35: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Issues

Page 36: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Issues

☠ API server waiting for query in Neo4j

Page 37: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Issues

☠ API server waiting for query in Neo4j

☠ Query MongoDB for add. data

Page 38: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Issues

☠ API server waiting for query in Neo4j

☠ Query MongoDB for add. data

☠ Transform data

Page 39: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Issues

☠ API server waiting for query in Neo4j

☠ Query MongoDB for add. data

☠ Transform data

☠ Ruby server is blocking

Page 40: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Communication (DB)

iPad Client

Main API(Ruby)

Recommend. Engine(Scala)

MongoDB Neo4j

GET /suggestions

UsersRatingsMovies and TV shows

Redirect to Recomm.Eng. w/ hashed params

Retrieve datafor query params Collect IDs of suggestions

All data

JSON array of suggested results

Retrieve data for response

Follow redirect link

Graph reset*

Page 41: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Improvements

Page 42: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Improvements

✌ No more long blocking requests in Ruby server

Page 43: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Improvements

✌ No more long blocking requests in Ruby server

✌ Move heavy lifting to Spray/Scala (async)

Page 44: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Improvements

✌ No more long blocking requests in Ruby server

✌ Move heavy lifting to Spray/Scala (async)

✌ Add cache layer

Page 45: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Improvements

✌ No more long blocking requests in Ruby server

✌ Move heavy lifting to Spray/Scala (async)

✌ Add cache layer

Page 46: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Additional Cache Layer

Page 47: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Additional Cache Layer

* Read-Through

Page 48: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Additional Cache Layer

* Read-Through

* Stores transformed data

Page 49: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Additional Cache Layer

* Read-Through

* Stores transformed data

* Implemented using spray-caching

Page 50: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Total Response Time

40s → <1s

Page 51: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Delta Updates

Page 52: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Delta Updates

* API: New ratings in queue collection (MongoDB)

Page 53: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Delta Updates

* API: New ratings in queue collection (MongoDB)

* Recommendations:Backend checks for updates in queue and applies them in graph

Page 54: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Delta Updates

iPad Client

Main API(Ruby)

Recommend. Engine(Scala)

MongoDB Neo4j

POST /rating

UsersRatingsMovies and TV shows

Store rating +Put rating in queue Collect IDs of suggestions

Check if updatesin queue

GET /suggestions

Apply updatesif pending

Retrieve data for response

Page 55: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

(Re)loading data in graph

Page 56: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

(Re)loading data in graph

1. Create new Neo4j instance

Page 57: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

(Re)loading data in graph

1. Create new Neo4j instance

2. Import data from MongoDB

Page 58: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

(Re)loading data in graph

1. Create new Neo4j instance

2. Import data from MongoDB

3. Hot swap

Page 59: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

(Re)loading data in graph

1. Create new Neo4j instance

2. Import data from MongoDB

3. Hot swap

★ Allows for massive updates in the primary

database

Page 60: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

(Re)loading data in graph

1. Create new Neo4j instance

2. Import data from MongoDB

3. Hot swap

★ Allows for massive updates in the primary

database

★ Used only for content-related data and user

Page 61: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013
Page 62: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Graph DB as an index for relations*

Page 63: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Graph DB as an index for relations*powerful queries on relations

Page 64: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Graph DB as an index for relations*powerful queries on relations

Page 65: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Graph DB as an index for relations*powerful queries on relations

⁂ like full text search for strings

Page 66: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Design decisions: Graph

Page 67: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Design decisions: Graph

✍ Copy subsets of data in Neo4j

Page 68: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Design decisions: Graph

✍ Copy subsets of data in Neo4j

✍ Rebuild graph with no downtime*

Page 69: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Design decisions: Graph

✍ Copy subsets of data in Neo4j

✍ Rebuild graph with no downtime*

✍ Massive updates blazingly fast

Page 70: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Design decisions: Graph

✍ Copy subsets of data in Neo4j

✍ Rebuild graph with no downtime*

✍ Massive updates blazingly fast

✍ Less critical pieces

Page 71: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

The right tools, for us! ✌

API in Ruby & MongoDB

Page 72: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

The right tools, for us! ✌

API in Ruby & MongoDBMaintanability, flexibility, libraries,

Page 73: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

The right tools, for us! ✌

API in Ruby & MongoDBMaintanability, flexibility, libraries,

schema-free!, development speed

Page 74: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

The right tools, for us! ✌

Recommendations

Page 75: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

The right tools, for us! ✌

RecommendationsScala: Performance + good interface for Neo4j

Page 76: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

The right tools, for us! ✌

RecommendationsScala: Performance + good interface for Neo4j

Neo4j: Graph problem

Page 77: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

Thanks! ☃

Alberto Perdomo@albertoperdomo

Page 78: Squire: A polyglot application combining Neo4j, MongoDB, Ruby and Scala @ FOSDEM GraphDevRoom 2013

❤ Credits ❤

Entering Hyperspace by Éole Wind