Gun is an open-source and realtime, decentralized, offline-first, graph database engine written in JavaScript.
It is designed to be small and simple for fast data synchronization which runs everywhere JavaScript is supported. Gun is a fully distributed database aiming at scaling web application and correctly synchronizing data among devices with offline-first feature.
Optimistic Concurrency Control (OCC)
Gun is a offline-first database engine and uses eventual consistency. It allows users to view and update data when it is offline, and data will be synchronized when it is online. Gun uses a self-designed conflict resolution algorithm, Hypothetical Amnesia Machine (HAM), which combines timestamps and vector clocks to resolve conflicts and order data.
Gun stores records in JSON files. Data and relations are represented as a graph. Records are connected by references, called “souls” internally in Gun. Some official and simple queries similar in SQL are implemented with paths traversing. Complex operations among relations can be easily extended on graph.
Gun supports both the disk-oriented and in-memory mode. It is offline-first database where browser asks peers for data to use in memory and browser local storage the most often as cache. By default, Gun has a built-in persistent storage layer called Radix and there are some other available third-party persistent service options suggested by Gun.
In Gun, every node utilizes its own memory space and shares the persistent storage with the other peers. Gun implements its own transport layer protocol for P2P networking, Daisy-chain Ad-hoc Mesh-network (DAM). The ad hoc mesh network is used to coordinate with peers by messaging without producing too many duplicate rebroadcasting messages.
Gun uses radix tree to store user data in memory, and the Radisk is what the project team have designed for it to persist storage in Radix Tree by default. Gun also supports self-implemented or third-party persistent storage like Amazon S3, so data compression can be done outside of Gun.
By default, Gun uses the Radisk Storage Engine as an in-memory and on-disk radix tree to store user data. Gun supports some existing persistent storage services like Amazon S3 to be plugged in, and also offers interfaces for developers to build persistent layer for it, so indexing can be flexible.