Table of contents
1.
Introduction
2.
Primary features of RedisGraph
3.
Installing RedisGraph
4.
RedisGraph in Action
4.1.
Graph construction
4.2.
Querying the graph 
5.
The Theory: Ideas behind RedisGraph 
5.1.
Representation 
5.2.
Traversal
5.3.
Algebraic expression 
6.
GraphBLAS 
7.
Frequently Asked Questions
7.1.
What is RedisGraph?
7.2.
Is RedisGraph open source?
7.3.
What is the difference between Redis replication and sharding?
8.
Conclusion
Last Updated: Mar 27, 2024

Design of Redis Graph

Author GAZAL ARORA
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Nowadays, graph-based data is everywhere. Facebook, Twitter, Google, and Pinterest are just a few companies that have realized the value of relationship data and are making the most of it. 

RedisGraph is an open-source graph database built on Redis that adds new graph features via a native C implementation with a focus on performance. 

Primary features of RedisGraph

  • Indexing and querying are simple and quick.
  • Memory-efficient custom data structures are used to store data in RAM.
  • On-disk persistence
  • OpenCypher, a popular graph query language, is used.
  • Sparse adjacency matrices are used to represent graphs.

Installing RedisGraph

RedisGraph is a Redis Stack component. For installation options, see the Redis Stack download page.

RedisGraph in Action

Let's look at some of the major RedisGraph ideas using the Redis-CLI tool:

Graph construction

Entities are commonly described as nodes in a graph. In this example, we'll make a tiny graph containing both actors and movies and an "act" relation that connects actors to the films in which they appeared. The graph will be used. To run a CREATE query, use the QUERY command. This will add new entities and relations to our graph.

graph.QUERY <graph_id>

'CREATE (:<label> {<attribute_name>:<attribute_value>,...})'

graph.QUERY <graph_id> 

'CREATE (<source_node_alias>)-[<relation> {<attribute_name>:<attribute_value>,...}]->(<dest_node_alias>)'

We'll make our graph with just one command:

graph.QUERY IMDB

 'CREATE (aldis:actor {name: "Aldis Hodge", birth_year: 1986}),
                         (oshea:actor {name: "OShea Jackson", birth_year: 1991}),
                         (corey:actor {name: "Corey Hawkins", birth_year: 1988}),
                         (neil:actor {name: "Neil Brown", birth_year: 1980}),
                         (compton:movie {title: "Straight Outta Compton", genre: "Biography", votes: 127258, rating: 7.9, year:                                   2015}),
                         (neveregoback:movie {title: "Never Go Back", genre: "Action", votes: 15821, rating: 6.4, year: 2016}),
                         (aldis)-[:act]->(neveregoback),
                         (aldis)-[:act]->(compton),
                         (oshea)-[:act]->(compton),
                         (corey)-[:act]->(compton),
                         (neil)-[:act]->(compton)'

Querying the graph 

A subset of the openCypher graph language is accessible via RedisGraph. While only a few languages are available, there is enough functionality to extract useful information from your graphs. We'll use the GRAPH.QUERY command to run a query:

GRAPH.QUERY <graph_id> <query>

Let's run a few queries against our movie database.

Find the total, maximum, minimum, and average age of the cast of "Straight Outta Compton":

GRAPH.QUERY IMDB 

'MATCH (a:actor)-[:act]->(m:movie {title:"Straight Outta Compton"})
RETURN m.title, SUM(2020-a.birth_year), MAX(2020-a.birth_year), MIN(2020-a.birth_year), AVG(2020-a.birth_year)'

RedisGraph will give the output:

1) 1) "m.title"
   2) "SUM(2020-a.birth_year)"
   3) "MAX(2020-a.birth_year)"
   4) "MIN(2020-a.birth_year)"
   5) "AVG(2020-a.birth_year)"
2) 1) 1) "Straight Outta Compton"
      2) "135"
      3) (integer) 40
      4) (integer) 29
      5) "33.75"
  1. Our result-set header is the first row, and it labels each column according to the return clause.
  2. Our query result is in the second row.

The Theory: Ideas behind RedisGraph 

Representation 

RedisGraph represents graphs with sparse adjacency matrices. Setting M's S, T entry to 1 (M[S, T]=1) records a directed relationship connecting source node S to destination node T in an adjacency matrix M. Source nodes are represented by matrix rows, whereas matrix columns represent destination nodes.

At least one matrix, named THE adjacency matrix, exists for every graph saved in RedisGraph (relation-type agnostic). Furthermore, each type-related relation has its specialized matrix. Consider the following graph with two sorts of relationships:

  1. visits
  2. friend

Three matrices make up the underlying graph data structure:

  1. THE adjacency matrix identifies every connection in the graph. THE visit matrix identifies visit connections.
  2. Marking friend relationships with a friend matrix
  3. E is a 'visit' connection linking node A to node B.

THE adjacency matrix at position [A, B] is set to 1 by a 'visit' connection E that connects node A to node B. V[A, B] is also set to 1 by the visit matrix V.

One additional matrix is assigned per label to accommodate typed nodes, and a label matrix is symmetric with ones along the main diagonal. If node N is identified as a Person, the Person matrix P will set position P[N, N] to 1.

This approach allows RedisGraph to quickly modify its graph, including:

  • Matrixes are simply extended by adding more rows and columns when new nodes are added.
  • Setting the required entries at the relevant matrices to add new relationships
  • Clearing relevant entries by removing relationships
  • Nodes are deleted by simply deleting the matrix row/column.

Graph traversal is one of the key reasons we chose to describe our graphs as sparse matrices.

Traversal

Matrix multiplication is used to traverse a graph. For example, if we wanted to locate friends of friends for every node in the graph, we could use the formula FOF = F^2. The outcome matrix FOF summarises the traversal, and F stands for the friendship relation matrix. The FOF's columns indicate buddies who are two hops distant, whereas the rows represent source nodes: j is a friend of a friend if FOF[i,j] = 1.

Algebraic expression 

We convert a search pattern like (N0)-[A]->(N1)-[B]->(N2)-[A]-(N3) into a series of matrix multiplications when we use it in a query. One alternative expression for the preceding example is A * B * Transpose (A).

Multiplication of matrices is an associative and distributive action. This offers us the option of deciding which terms to multiply first (preferring terms that will produce highly sparse intermediate matrices). It also allows for parallel computation when evaluating an expression, so we may compute AZ and BY simultaneously.

GraphBLAS 

RedisGraph employs GraphBLAS, a standard API similar to BLAS, to perform these operations for sparse matrices. The current implementation uses the CSC sparse matrix format (compressed sparse columns).

Frequently Asked Questions

What is RedisGraph?

RedisGraph is a graph database built entirely on top of Redis, with new commands and capabilities added using the new Redis Modules API. Its primary characteristics are: Indexing and querying are simple and quick. Memory-efficient custom data structures are used to store data in RAM.

Is RedisGraph open source?

While Redis is open source, higher-value modules are now covered by a commercial agreement that restricts the sale of Redis-based products. Redis Labs has moved its database modules away from open source and toward a new commercial licensing system that includes source code access.

What is the difference between Redis replication and sharding?

Sharding, also known as partitioning, is the process of partitioning data by key; Replication, also known as mirroring, is copying all data. Sharding improves performance by lowering a single resource's hit and memory load. Replication is helpful in increasing readability.

Conclusion

In this article, we learned about Redis Graph.

Although RedisGraphit is still a young project, RedisGraph can be a feasible alternative to other graph databases. You can use its subset of operations to analyze and examine graph data. As a Redis Module, this project can be accessed by any Redis client without any modifications. With the help of the open-source, the Redis community plan to continue enhancing and expanding RedisGraph.

Check out this problem - Matrix Median

Click here to learn about Redis-CLI.

You can use Coding Ninjas Studio to practice various DSA questions asked in the interviews. It will help you in mastering effective coding techniques, and you will also get interview experiences with people working in big companies.

Happy Learning!!

Live masterclass