SDSDS is a data storage system that automatically discovers peers in the same subnet (using UDP broadcast) and synchronizes (over TCP) a shared data object between them. Changes propagate to all nodes and each node acts as a redundant to the other nodes. This system requires no central point of failure (other than the network itself).
SDSDS only requires one other module, and does not require compilation. To get started with SDSDS should be as simple as: npm install sdsds
- Any data you store in the data store HAS to be an object and will have a 'timestamp' key added to it, this is to allow nodes to determine the last modified time.
- You should frequently checkpoint the stored data to a file (or database), and load this file when the library starts. SDSDS does not manage persistance of the data, only sharing the data. (an example of howto checkpoint is provided in the 'examples' directory)
var SDSDS = require('sdsds');
var sdsds = new SDSDS();
sdsds.on('listening', function() {
// from this point onwards, we can assume we have access to the data store.
// however, this does not ensure we have synchronized with any nodes,
// we may even be the first active node on the network.
// any data you store in the data store HAS to be an object and will have
// a 'timestamp' key added to it, this is to allow nodes to determine the
// last modified time.
sdsds.set("key", {"value":123});
sdsds.get("key"); // => {"value":123, "timestamp": xxxxxx}
sdsds.getAll(); // => {"key": {"value":123, "timestamp":xxxxxx}}
});
SDSDS is a combination of a TCP server, a TCP client, a UDP client and a UDP server.
Nodes are discovered using UDP broadcast packets to discover other nodes in the same subnet. This is achieved through the use of beacon packets that are sent every five (5) seconds. After a node receives the knowledge of a previously unknown (or a no longer connected) node, it attempts to make a connection and switches to the TCP protocol for handling data transfer.
Internally there are two methods of data transfer over TCP. We current support a 'full synchronization' and a 'write notify'.
The system will fully synchronize the whole shared data object whenever a new node connects to it. As a connection is made to the TCP server, the server sends the enter data object to the client. The client then decides which data is relevant compared to the data it initialized with. (Eventually the system may send periodic full synchronization packets to confirm the data store is still intact)
After a key has been set or modified in the data store, the node will transmit to all other connected nodes that this write operation has been applied. The receiving nodes will then decide if the data is relevant compared to it's current data.
A node will decide upon receiving data if this data is relevant compared to the current data it has. This is all based around a unix timestamp, if the incoming data is older than the data the node currently has, it will be rejected. This poses an issue if the node's clocks are out of sync, however this isn't really an issue if data isn't being rapidly fired.
host: The IP address/hostname of this machine (has to be accessible from the other nodes). If this is omitted the system will attempt to discover the IP address from your first network interface (this may be incorrect!)
port: The port that the TCP transfer runs upon.
data: An object containing the initial data for this node, if a node has crashed an is rebooting, this should contain the last data it knew about, if ommitted the data will remain empty until changed, or another node is connected.
debugLevel: This dictates the level of debugging output to console. It defaults to only errors ("ERR"), but the valid options are: "ERR", "WRN", "DBG" and "ALL".
Retrieve data from the data store. Returns the data object, or undefined if the key was not found.
key: the unique key that your data is attached to.
Set or modify data stored in the data store. Returns true if the data was saved successfully.
key: the unique key that your data is attached to.
value: an object that contains the data (a 'timestamp' key will be added to this object to track synchronization.
Retrieve all data stored in the data store. Returns the entire data store object.
Closes all open TCP and UDP connections. The instance of SDSDS is useless from this point and should be discarded.
Listens for an event and executes the callback upon firing.
eventName: the event name (see 'Events' for a list of events). callback: a function to call when the event fires.
Fires an event to all listeners. SDSDS does not internally listen for events, firing 'close' will NOT close SDSDS. This is mainly useful for triggering other code that is listening for SDSDS events.
eventName: the event name (see 'Events' for a list of events). arguments: any number of arguments can be passed to the event (it's recommended to match the arguments that are usually provided by the event).
Fired when both the TCP and UDP servers have started.
Fired when an internal error occurs (this should also pass through any TCP/UDP server/client errors)
Fired when both the TCP and UDP servers have been closed.
Fired when the current node disconnects from a remote node's TCP server.
nodeInstance: an object describing the node that the event applies to.
Fired when the current node connects (and the handshake completes) to a remote node's TCP server.
nodeInstance: an object describing the node that the event applies to.
Fired when a remote node connects to the current node's TCP server.
nodeInstance: an object describing the node that the event applies to.
Fired when a remote node connects to the current node's TCP server.
nodeInstance: an object describing the node that the event applies to.
Fired when the whole data object has been synchronized with a remote object.
nodeInstance: an object describing the node that the event applies to. data: the whole shared data object (after sync)
Fired when a data entry has been written to from a remote object.
nodeInstance: an object describing the node that the event applies to. key: the key that was written too. value: the value that was written.
SDSDS is currently a small project, maintained by a small team. We would welcome any contributions to the codebase.
- Peter Corcoran ([email protected])
SDSDS is an open source project licensed under the LGPLv3 licenses, you can find more details regarding this license in the 'LICENSE.md' file.