Lessons › From one user to millions
People you may know
A social app with 5,000 req/s. 4% are "people you may know" lookups: 200 a second, each walking friends of friends. In SQL that's a join on a join, and each one takes over a second.
Play this levelFree with an account. It runs in your browser: build the design, press Run, and watch what breaks.
The lesson
SQL can answer this, but each hop is another join over a huge table, and each hop multiplies the rows. Two hops is slow. Three is a bad day.
A graph database such as Neo4j or Neptune stores each link next to the thing it links. A hop is a short step, so the cost depends on how much you walk, not on the size of the table.
It's a specialist tool. Use it when the links between things are the point, like friends or fraud rings, and keep plain lookups in your normal database.
What you have to achieve
5,000 requests a second at the busiest hour, 2% of them writes.
- No broken or illegal connections
- Use a Graph DB with at least 2 instances
- No component is overloaded
- No single point of failure
- Fewer than 0.1% of requests fail
- p99 response time under 200 ms
What you learn
The database calmed down the moment those lookups moved. Keeping the graph up to date is real work that isn't shown here: you'd feed it from a stream, just like a search index. Its speed here is a starting guess, because it depends on how far each query walks.
Check yourself
Why are friends of friends lookups slow in SQL?
- SQL can't store friendships
- Each hop is another join, and each multiplies the rows
- The table is encrypted
Show the answer
Each hop is another join, and each multiplies the rows. Two hops is a join on a join over a huge table. A graph database makes each hop a short step.
When is a graph database the right tool?
- For every app
- For simple lookups by ID
- When the links between things are the point
Show the answer
When the links between things are the point. Friends and fraud rings are about links. Keep plain lookups in your normal database.