A Graph Database is a type of NoSQL database that represents and stores data in the form of nodes, relationships (edges), and properties.
It is designed to efficiently store and query complex, highly connected data such as social networks, recommendation systems, fraud detection, and network topologies.
Core Concepts of Graph Databases
Element | Description | Example |
---|---|---|
Node | Represents an entity | Person, City, Product |
Edge | Represents a relationship between nodes | FRIEND_OF, PURCHASED, LOCATED_IN |
Property | Key-value pairs that store data about nodes or edges | Name: “Alice”, Age: 30 |
Label | A tag that defines the type of a node | Person, Movie, User |
What is Neo4j?
Neo4j is a leading open-source graph database that uses the property graph model. It is designed for fast traversal of relationships and supports a powerful graph query language called Cypher.
Features of Neo4j
- ACID-compliant (ensures transaction safety)
- Supports index-free adjacency (direct relationships between nodes)
- Highly optimized for graph traversal queries
- Scales horizontally and vertically
- Used in real-time recommendation, fraud detection, social networks
Cypher Query Language Basics
Cypher uses ASCII-art-like syntax to describe nodes and relationships.
1. Creating Nodes:
CREATE (p:Person {name: "Alice", age: 25});
2. Creating Relationships:
MATCH (a:Person {name: "Alice"}), (b:Person {name: "Bob"}) CREATE (a)-[:FRIEND_OF]->(b);
3. Querying the Graph:
Find all friends of Alice:
MATCH (a:Person {name: "Alice"})-[:FRIEND_OF]->(friend) RETURN friend.name;
Example Use Case: Social Network
Nodes:
(:Person {name: "John"})
(:Person {name: "Sara"})
Relationship:
(John)-[:FRIEND_OF]->(Sara)
This allows fast queries like:
- Who are John’s friends?
- Find mutual friends between John and Sara
- Suggest friends of friends
Advantages of Neo4j and Graph DBs
- Natural modeling of real-world relationships
- Efficient handling of deeply connected data
- Schema flexibility
- Fast path-based querying
Use Cases
- Social networks
- Fraud detection
- Recommendation engines
- Knowledge graphs
- Network and IT operations