knaxuslearn · build · ship

Projects / People you may know / Phase 4

Phase 4 of 4 · about 2.5 hours

Five thousand people

You end with a graph of 5,000 people, a suggestion query with its cost, and three counts that agree.

Step 4.1: Load 5,000 people (about 25 minutes)

range(1, 5000) produces the numbers, and each one becomes a person. The cities cycle through five names and the birth years through thirty. The data is generated by arithmetic, so every run builds the same graph:

UNWIND range(1, 5000) AS i
CREATE (:Person {id: 'g' + toString(i), name: 'User ' + toString(i), city: ['Pune','Mumbai','Delhi','Bengaluru','Chennai'][i % 5], born: 1980 + (i % 30)});
MATCH (p:Person) RETURN count(p) AS people;

Step 4.2: Give them friends (about 35 minutes)

Each person i is joined to three others, found by multiplying i by 1, 7 and 31 and wrapping around. MERGE means "create it if it does not already exist", so running a pair twice does not make two arrows. Each lookup by id uses the constraint index from Step 1.3, which is why this finishes in seconds and not minutes:

UNWIND range(1, 5000) AS i
UNWIND [1, 7, 31] AS step
WITH i, ((i * step) % 5000) + 1 AS j
WHERE i <> j
MATCH (a:Person {id: 'g' + toString(i)}), (b:Person {id: 'g' + toString(j)})
MERGE (a)-[:FRIENDS_WITH]-(b);
MATCH (:Person)-[r:FRIENDS_WITH]->(:Person) RETURN count(r) AS friendships;

The count includes the fourteen friendships from Phase 1. Because friendships are stored once and read without a direction, a pair found from both sides is still one arrow.

Step 4.3: A suggestion, and what it cost (about 35 minutes)

Run the friends-of-friends query for one person in the big graph, with PROFILE:

PROFILE
MATCH (me:Person {id: 'g1'})-[:FRIENDS_WITH]-(f:Person)-[:FRIENDS_WITH]-(fof:Person)
WHERE fof <> me AND NOT (me)-[:FRIENDS_WITH]-(fof)
RETURN fof.id AS suggestion, count(DISTINCT f) AS mutual_friends
ORDER BY mutual_friends DESC, suggestion
LIMIT 5;

Read the plan from the bottom up. It starts with one NodeUniqueIndexSeek for the person with id g1, then follows relationship after relationship. Compare the DB Hits with the 5,000 people in the graph. The query visited a few dozen of them, not all. That is the main idea: the cost of a graph query depends on how many connections it touches, not on how big the whole graph is.

Cost follows the connections the query touches, not the size of the graph.

Step 4.4: Who has the most friends (about 20 minutes)

The number of friends a person has is their degree. This groups people by degree:

MATCH (p:Person)-[:FRIENDS_WITH]-(:Person)
WITH p, count(*) AS degree
RETURN degree, count(p) AS people
ORDER BY degree;

Our generated graph is very even, since every person got the same recipe. A real social graph is not. A few people have thousands of friends, and a friends-of-friends query that starts at one of them touches far more of the graph. That is why real systems cap or sample such queries.

Step 4.5: Count the same thing three ways (about 25 minutes)

A number you computed once is a guess. Here are three different ways to count the people two hops away from g1, who are not already friends: a pattern, a collected list, and a path length. If all three agree, we can trust the query.

MATCH (me:Person {id: 'g1'})-[:FRIENDS_WITH]-(f:Person)-[:FRIENDS_WITH]-(fof:Person)
WHERE fof <> me AND NOT (me)-[:FRIENDS_WITH]-(fof)
RETURN count(DISTINCT fof) AS by_pattern;
MATCH (me:Person {id: 'g1'})-[:FRIENDS_WITH]-(f:Person)
WITH me, collect(f) AS friends
UNWIND friends AS f
MATCH (f)-[:FRIENDS_WITH]-(fof:Person)
WHERE fof <> me AND NOT fof IN friends
RETURN count(DISTINCT fof) AS by_unwind;
MATCH (me:Person {id: 'g1'})-[:FRIENDS_WITH*2]-(fof:Person)
WHERE fof <> me AND NOT (me)-[:FRIENDS_WITH]-(fof)
RETURN count(DISTINCT fof) AS by_path_length;

Step 4.6: Deleting a person (about 20 minutes)

What happens if we delete someone who has friends? First try the plain way:

MATCH (p:Person {name: 'Jay'}) DELETE p;

Neo4j refuses. A relationship cannot exist with a missing end, so a node that still has relationships cannot be deleted. DETACH DELETE removes the relationships first:

A relationship cannot exist without both ends.
MATCH (p:Person {name: 'Jay'}) DETACH DELETE p;
MATCH (p:Person {name: 'Kiran'})-[r:FRIENDS_WITH]-() RETURN count(r) AS kiran_friendships;
MATCH (p:Person) WHERE p.id STARTS WITH 'p' RETURN count(p) AS small_graph_people;

Kiran lost the friendship with Jay and the small graph has one person fewer. The refusal is a safety net: you must ask to remove the relationships on purpose.

Phase 4 done

We started with one question: who is a friend of my friends and not yet my friend? The graph answers it as a pattern, ranks it with two kinds of evidence, stays cheap on 5,000 people because it only follows the connections it needs, and agrees with itself three ways.