What is a Graph Database?
GUN is a graph database, which means it stores data as interconnected nodes and edges rather than in rigid tables or document hierarchies. This fundamental design choice enables GUN to handle complex relationships, circular references, and flexible data structures that would be difficult or impossible in traditional SQL or NoSQL databases.The Graph Data Model
In GUN, everything is a graph:- Nodes: Objects that contain data (key-value pairs)
- Edges: References between nodes (relationships)
- Soul: A unique identifier for each node (like
#in GUN)
How GUN Stores Graph Data
Nodes and Keys
Every piece of data in GUN is stored as a node. You reference nodes using.get():
Edges and References
Nodes can reference other nodes, creating edges in the graph:Circular References
One of GUN’s most powerful features is native support for circular references - something that breaks traditional JSON and most databases:GUN automatically detects and handles circular references. You don’t need to worry about infinite loops or stack overflow errors.
Sets: Collections of Nodes
GUN provides sets to create unordered collections:Graph vs SQL vs NoSQL
SQL (Relational)
- Fixed schemas
- Tables with rows/columns
- JOINs for relationships
- Poor handling of many-to-many
NoSQL (Document)
- Flexible schemas
- Documents (JSON-like)
- Embedded or referenced docs
- Difficult circular refs
Graph (GUN)
- Schemaless
- Nodes + Edges
- Natural relationships
- Native circular refs
Comparison Example
Consider modeling a social network: SQL Approach:The Graph Structure in Memory
Internally, GUN represents graphs as nested objects. From the source code (src/get.js and src/put.js), each node has:
Working with Complex Graphs
Nested Data
Multiple Data Types
GUN can store different data structures simultaneously:Best Practices
1
Design your graph structure
Think about relationships first. How are your nodes connected? Draw it out.
2
Use meaningful soul identifiers
3
Avoid deep nesting
Too many
.get() chains hurt performance. Keep graphs shallow when possible.4
Leverage sets for collections
Use
.set() for lists of items, .map() to iterate over them.Performance Considerations
From the GUN benchmarks, the graph engine can handle:- 20M+ ops/sec for read operations
- Native handling of circular references with no performance penalty
- Efficient memory usage through reference sharing
GUN’s graph structure means nodes are stored once in memory, even if referenced from multiple places. This is more efficient than duplicating data in traditional databases.
Next Steps
Decentralization
Learn how GUN’s graph syncs across peers
Realtime Sync
Discover realtime updates with .on()