Connecting the Dots: Graph Databases and Laravel
Keith Damiani

Slides: https://keithdamiani.com/laracon/

Today, talking about Graph databases! NYC is the perfect place to talk about graph databases. First of all, big thank-you to Taylor Otwell!

TEDTalk joke, lol

Currently in NYC, live in Charlottesville, born in Southern California, want to visit place in Europe.

How many people use graph databases (fair share of hands go up)

Content warning: Math

Going to talk about what a graph database is, how it differs from a relational database, use cases, writing code, how it relates to Laravel/Laracon

What exactly is a graph database? What exactly is a graph?

Graphs are all about understanding and working with connections between data. In New York City, easy to see the connections. By the way, shoutout to Jack McDade

Example of a subway. Subway stations and trains. Maps help make decisions on how to get to a location, including what stations have multiple trains serving it.

Lines and circles easily describe it. Like a family tree! Simple, hierarchical structure with vertices and edges (or people and relationships)

WAIT A MINUTE

A database that's really good at storing relations...like...a relational database?

Well, relational databases don't actually store any relationships; it makes them up!

In a graph database, relationships are explicitly stored. Relational databases? They're implicitly declared through foreign keys, etc.

Cue a New Kids On The Block reference ;)

Back in 1700s, problem of Konniksburg. Seven bridges over a river, connecting all the land masses. Is it possible to find a path that goes over all bridges and only once? Along comes Euler in 1735, going and inventing graph theory.

When working with highly-connected data, can get information out of the database. Beats Products/orders/order_details/suppliers/suppliers_products and all the joins. Graph databases are really good at doing what they do, but they aren't always the best tool

Laravel makes using multiple database connections easy

Well, what are they good for?

Depends on nature of data (highly-connected data is good for graph)

When there's a lot of value in the connections. Talking about neo4j, 4 billion connections per second. Cost of finding a related node is negligible

If hierarchical data, good idea

Friends-of-a-friend query. Performance query times scale poorly for relational databases, scale just fine for graph databases

Many-to-many and schemaless!

Common use cases

Adding a graph database can add buzzwords and allow you to charge your clients more! /s

Bad fits

Disconnected data (e.g. transactional, key-value)

Bulk data scans (though you can specify a filter

Aggregating across multiple notes (e.g. average age of users)

Writing to the database (essentially, joins are "created" as you write)

Large amount of text/blobs to store (some graph database engines can do this, but not neo4j)

Show me the code!

Most common platform is neo4j with free, paid plans, an O'Reilley book, good documentation

Step 1: Download. Step 2: Run (it's that easy)

neo4j uses Cypher as a query language. match(me: User, {name})

MATCH (me:User {name: 'Keith Damiani'})-[]-(u:User) RETURN me, u, r

// Find my friends
MATCH (me:User {name: 'Keith Damiani'})-[r:IS_FRIENDS_WITH]->(u:User) RETURN me, u, r

// Friends that are friends with me
MATCH (me:User {name: 'Keith Damiani'})<-[r:IS_FRIENDS_WITH]->(u:User) RETURN me, u, r

// Returns a relationship
CREATE (u1)-[r:IS_FRIENDS_WITH]->(u2) RETURN r, u1, u2

Also a way to specify a "hop" limit

Also a way to find six random users who are not directly connected to me

MATCH ... WHERE NOT (u.name = 'Keith Damiani' OR (u)-[](:...)

Three ways to integrate into Eloquent:

I'm writing my own called Laravel Vapor...wait, that might already be taken. Neo4Laravel

Available RIGHT NOW at larapals.com

Also check on your Wathanscore (degrees of separation from Adam Wathan)