Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ditch ActiveRecord for ActiveGraph is your new girlfriend

Ditch ActiveRecord for ActiveGraph is your new girlfriend

Rails was written in 2004, a time when RDBMS ruled the world and Pierce Brosnan was still James Bond! Today there's a plethora of NoSQL solutions gaining traction all around the world. Neo4J is the world's leading graph database. Written in Java, it serves some of the world's top companies and startups being 1000x faster than MySQL when it comes to deep joins. A graph database allows you to model your domain knowledge naturally as nodes connected by relationships, both of which can have an arbitrary number of properties and without the headaches of normalisation and other RDBMS idiosyncrasies. ActiveGraph is a fast, minimal Ruby ORM for Neo4J written on top of Neon - slim ruby binding for Neo4J. ActiveGraph integrates with Rails bringing everything you know and love about ActiveRecord on board. I'll tell you how you can easily swap out ActiveRecord and slid in ActiveGraph without disrupting your applications and almost instantly gaining performance, productivity and scalability by over several times and I'll tell you why you should embrace and love graphs, maybe even marry them! We as a community need to build better tools so more and more people can embrace this new approach and technology for data modelling.

Ujjwal Thaakar

March 23, 2014
Tweet

More Decks by Ujjwal Thaakar

Other Decks in Programming

Transcript

  1. 21

  2. Agenda • Briefly go over Neo4J • The what, how

    and why of ORMs • Current gems for binding to Neo4J • Neon • ActiveGraph • Ask
  3. Property Graph User Weird User • id • since •

    strength FRIEND-OF • id • name • email • password • name • email • password • weirdness
  4. • 1000x faster than MySQL for deep joins • Can

    handle billions of nodes easily • Millions of relationship traversals per second! • And yeah the relationships are directed • Cypher language for pattern matched traversal
  5. Why • SQL databases only store scalar values • Objects

    aren't scalar • We need to convert objects to simpler scalar values and vice versa • Translate the object into a form storable on a database while preserving its properties and relationships for later retrieval
  6. OGM • Graphs store objects and their relationships directly •

    So we coin a new name - Object Graph Mapping • Why? • Because I’m trying to be different
  7. Common API • Same API - embedded or REST •

    You either connect to the db or create/open one • Return ruby objects matching the interface for REST stuff • Mixin the Java objects from the embedded API and return them instead - RAW SPEED!
  8. • CRUD for Nodes and Relationships • DB synchronized hash

    • Identity map • Adding/Removing labels • Transactions • Auto Transactions
  9. Transactions • Issue with Auto TX • Wrap REST operations

    inside a transaction for explicit transactions • Wrap embedded operations inside transactions for auto_tx enabled
  10. Powerful stuff • Pretty much full compliance for Neo4J API

    • This is like tinker pops for Neo4J • Embedded matters for performance • First time ever! • Minimal
  11. ActiveModel Compliant • Nodes and Relationships are first class objects

    • Fully ActiveModel compliant as we use active_attr behind the scenes • Empirically high performance - benchmarks coming soon
  12. ActiveRecord API • class names map to labels on nodes

    • SpecialUser < User < ActiveGraph::Node • “SpecialUser”, “User” • Girlfriend < Friend < ActiveGraph::Relationship • “Girlfriend” but all properties of a “Friend”
  13. Associations • has_one is an outgoing relationship • belongs_to is

    an incoming relationship • has_many is direction agnostic • has_one(:friend).from(“User#friends”) • has_n(:friend).to(“User”)
  14. Composition • Compositions work • We store compositions as nodes

    with special relationships • ArangoDB solves this! Hail Lucas and his team!
  15. Graph Expressiveness • Cypher is super cool • You give

    it a pattern and it traverses and matches subgraphs MATCH (user)-[:friend]->(follower)! WHERE user.name IN ['Joe', 'Maria', 'Steve'] AND follower.name =~ 'S.*'! RETURN user, follower.name
  16. START me=node(1) MATCH (me)-[friend_rel:`friends`]->(friends) WHERE (friend_rel.since = 1994) RETURN friends

    Neo4j.query do node(1).outgoing(rel(:friends).where{|r| r[:since] == 1994}) end me = User.find name: “Ujjwal” Neo4j.query do me.outgoing(rel(:friends).where{|r| r[:since] == 1994}) end
  17. • Wrote Neon - didn’t like it • Started ActiveGraph

    - couldn’t settle on it • Rewriting right now • I’ll never get there!
  18. German Work Culture • Germans are bitchy about their time

    • I like it! • Efficient - you do it once and you do it right • I’m gonna be a German in my work!
  19. Ask • I’m new to Ruby • Please come and

    talk to me about the architecture • Even better help me build it • We need to build better tools to support NoSQL in Ruby • ujjwalt/neon • ujjwalt/activegraph