Graph databases are capable of representing networks of entities that are interconnected based on various relationships. They use nodes, edges, and properties to store data that relational databases are not equipped with. Furthermore, nodes hold data entities, and edges store relationships among entities.
Quick Introduction to RedisGraph
Several graph databases to choose from nowadays, and this guide focuses on RedisGraph, which is a relatively new player in this area. The RedisGraph is built as a module for Redis that is easy to use and extremely fast. In addition, to interact with the RedisGraph databases, the Cypher query language is used, which is also used in some other graph databases as well.
Behind the Scenes – RedisGraph Internal Technology
RedisGraph uses sparse matrices to internally represent the graph data. It also leverages the highly-optimized GraphBLAS library that gives a combination of high performance and very compact storage. To highlight, Redis is single-threaded, and that is how incoming requests are handled even for RedisGraph; once a RedisGraph query reaches the RedisGraph database, it is passed on to a thread pool. Because Redis modules can manage multiple threads. In contrast to other graph databases, when serving queries, RedisGraph focuses more on high throughput scenarios. So, one graph query at any given time will typically run on only one thread. It might sound counterintuitive, but the benchmarks prove that this scales perfectly well.
Setting Up RedisGraph
Several approaches to setting up and interacting with the RedisGraph database as listed in the following.
- Using the official RedisGraph docker image
- Build the source code and load it as a module
- Redis cloud instance with RedisGraph module enabled
Using the RedisGraph Docker Image
The fastest way to up and run a RedisGraph database is to download and run a RedisGraph instance within a docker container. The following docker command is used to launch a Redis instance with RedisGraph enabled.
Once the docker container is up, you can talk to the RedisGraph database using Redis-CLI.
Building the Source
The source code for the RedisGraph can be found in the following repository.
It can be cloned easily with the following git command.
In Ubuntu or OS X, you need to install “cmake” using the following commands respectively.
Ubuntu:
OS X:
Since the “make” is available in the system, you can just execute the “make” command from the root of the project directory. Upon building the RedisGraph source code, the redisgraph.so module(compile binary) can be found in the src directory.
It is possible to load the RedisGraph module using the MODULE LOAD command at runtime. Furthermore, the loadmodule directive can be set to the path of the compiled binary in the redis.conf file as follows.
Redis Cloud With RedisGraph Module Enabled
One of the easiest ways to play with a RedisGraph is to create a Redis cloud subscription and use a Redis cloud instance with the RedisGraph module enabled. So, you can connect to the cloud instance with redis-cli without any further burden of building the source manually in the local machine and loading it explicitly.
Consuming RedisGraph Database – RedisGraph Commands
Once you are done with setting up the RedisGraph database, it is time to consume the database. Several commands are available for Redis clients to talk to the RedisGraph database. Most importantly, these basic commands accept openCypher queries. Some of the frequently used fundamental RedisGraph commands are listed in the following.
GRAPH.QUERY – Runs an openCypher query against a given graph which enhances its functionality with additional functions and clauses.
GRAPH.CONFIG GET | SET – Retrieves or Updates the value of a RedisGraph configuration parameter.
GRAPH.DELETE – Removes the graph and associated entities.
GRAPH.LIST – All the graph keys will list down.
GRAPH.EXPLAIN – Only shows the query execution plan and doesn’t execute the openCypher query.
More details on the above commands can be found here.
Use Case – Social Network With RedisGraph Database
In this example, we will be using a Redis cloud instance with the RedisGraph module enabled. The redis-cli is used to connect to the RedisGraph database, and you can verify that the RedisGraph module is enabled by executing the following command.
As expected, the graph module is available to use.
Creating a Graph
Let’s first create a graph that consists of some entities and relationships in the context of social networks. The following format needs to be followed to create a graph using openCypher query language.
Assume that John follows Mary, Mary follows Jack, and Jack follows Nisha in a social network. This can be represented using a graph database like RedisGraph easily. So, let’s store these entities and relationships associated with them as follows using openCypher query language.
Each new entry is separated by a comma.
Upon executing the GRAPH.QUERY command returns some useful information related to the created graph, such as the number of nodes, relationships, and properties created and set.
Adding New Nodes
It is possible to add new nodes to the previously created graph as well. Let’s add a new person called “Ricky Martin” to the existing graph as follows.
Querying the Graph
We can also retrieve the graph data using GRAPH.QUERY command as follows.
As expected, the second relationship details should be returned according to the above query. Because the query asks for the “follows” relationship, but specifically, the follower should be “Jack”.
In these examples, we used the redis-cli client to interact with RedisGraph. But you can use several other client libraries developed for different languages like python to query the RedisGraph databases.
Conclusion
To summarize, the RedisGraph is a graph database that is available as a Redis module with super-fast querying capabilities. As discussed, there are several ways that you can up and run a RedisGraph database, such as using a docker image, manually building the source code, or even with a Redis cloud subscription with the RedisGraph module enabled.
The RedisGraph commands and API can be used to talk to the database through redis-cli or any other Redis clients. As a final note, it is recommended to get familiar with the openCypher query language, which is used by RedisGraph to create, update, or delete Graphs, Nodes, and Relationships.