Graph database using Redis Stack


Profile picture for Ajeet Raina
Author:
Ajeet Raina, Developer Growth Manager at Redis

RedisGraph is a Redis module that enables enterprises to process any kind of connected data much faster than with traditional relational or existing graph databases. RedisGraph implements a unique data storage and processing solution (with sparse-adjacency matrices and GraphBLAS) to deliver the fastest and most efficient way to store, manage, and process connected data in graphs. With RedisGraph, you can process complex transactions 10 - 600 times faster than with traditional graph solutions while using 50 - 60% less memory resources than other graph databases!

Step 1. Create a free Cloud account#

Create your free Redis Enterprise Cloud account. Once you click on “Get Started”, you will receive an email with a link to activate your account and complete your signup process.

TIP

For a limited time, use TIGER200 to get $200 credits on Redis Enterprise Cloud and try all the advanced capabilities!

🎉 Click here to sign up

Step 2. Create Your database#

Choose your preferred cloud vendor. Select the region and then click "Let's start free" to create your free database automatically.

TIP

If you want to create a custom database with your preferred name and type of Redis, click "Create a custom database" option shown in the image.

create database

Step 3. Verify the database details#

You will be provided with Public endpoint URL and "Redis Stack" as the type of database with the list of modules that comes by default.

verify database

Step 4. Install RedisInsight#

RedisInsight is a visual tool that lets you do both GUI- and CLI-based interactions with your Redis database, and so much more when developing your Redis based application. It is a fully-featured pure Desktop GUI client that provides capabilities to design, develop and optimize your Redis application. It works with any cloud provider as long as you run it on a host with network access to your cloud-based Redis server. It makes it easy to discover cloud databases and configure connection details with a single click. It allows you to automatically add Redis Enterprise Software and Redis Enterprise Cloud databases.

You can install Redis Stack on your local system to get RedisInsight GUI tool up and running. Ensure that you have brew package installed in your Mac system.

brew tap redis-stack/redis-stack
brew install --cask redis-stack
==> Installing Cask redis-stack-redisinsight
==> Moving App 'RedisInsight-preview.app' to '/Applications/RedisInsight-preview.app'
🍺 redis-stack-redisinsight was successfully installed!
==> Installing Cask redis-stack
🍺 redis-stack was successfully installed!

Go to Applications and click "RedisInsight-v2" to bring up the Redis Desktop GUI tool.

Step 5. Add Redis database#

access redisinsight

Step 6. Enter Redis Enterprise Cloud details#

Add the Redis Enterprise cloud database endpoint, port and password.

access redisinsight

Step 7. Verify the database under RedisInsight dashboard#

database details

Step 8. Getting Started with RedisGraph#

In the following steps, we will use some basic RediGraph commands to insert data into a graph and then query the graph. You can run them from the Redis command-line interface (redis-cli) or use the CLI available in RedisInsight. (See part 2 of this tutorial to learn more about using the RedisInsight CLI.)

RedisGraph

Step 9: Insert data into a graph#

Insert actors#

To interact with RedisGraph you will typically use the GRAPH.QUERY command and execute Cypher queries. Let’s start to insert some actors into the graph:movies graph name, which is automatically created using this command:

>> GRAPH.QUERY graph:movies "CREATE (:Actor {name:'Mark Hamill', actor_id:1}), (:Actor {name:'Harrison Ford', actor_id:2}), (:Actor {name:'Carrie Fisher', actor_id:3})"
1) 1) "Labels added: 1"
2) "Nodes created: 3"
3) "Properties set: 6"
4) "Query internal execution time: 0.675400 milliseconds"

This single query creates three actors, along with their names and unique IDs.

Insert a movie#

> GRAPH.QUERY graph:movies "CREATE (:Movie {title:'Star Wars: Episode V - The Empire Strikes Back', release_year: 1980 , movie_id:1})"
1) 1) "Labels added: 1"
2) "Nodes created: 1"
3) "Properties set: 3"
4) "Query internal execution time: 0.392300 milliseconds"

This single query creates a movie with a title, the release year, and an ID.

Associate actors and movies#

The core of a graph is the relationships between the nodes, allowing the applications to navigate and query them. Let’s create a relationship between the actors and the movies:

> GRAPH.QUERY graph:movies "MATCH (a:Actor),(m:Movie) WHERE a.actor_id = 1 AND m.movie_id = 1 CREATE (a)-[r:Acted_in {role:'Luke Skywalker'}]->(m) RETURN r"
1) 1) "r"
2) 1) 1) 1) 1) "id"
2) (integer) 1
2) 1) "type"
2) "Acted_in"
3) 1) "src_node"
2) (integer) 0
4) 1) "dest_node"
2) (integer) 3
5) 1) "properties"
2) 1) 1) "role"
2) "Luke Skywalker"
3) 1) "Properties set: 1"
2) "Relationships created: 1"
3) "Query internal execution time: 0.664800 milliseconds"

This command created a new relation indicating that the actor Mark Hamill acted in Star Wars: Episode V as Luke Skywalker.

Let’s repeat this process for the other actors:

> GRAPH.QUERY graph:movies "MATCH (a:Actor), (m:Movie) WHERE a.actor_id = 2 AND m.movie_id = 1 CREATE (a)-[r:Acted_in {role:'Han Solo'}]->(m) RETURN r"
> GRAPH.QUERY graph:movies "MATCH (a:Actor), (m:Movie) WHERE a.actor_id = 3 AND m.movie_id = 1 CREATE (a)-[r:Acted_in {role:'Princess Leila'}]->(m) RETURN r"

You can also do all of this in a single query, for example:

> GRAPH.QUERY graph:movies "CREATE (:Actor {name:'Marlo Brando', actor_id:4})-[:Acted_in {role:'Don Vito Corleone'}]->(:Movie {title:'The Godfather', release_year: 1972 , movie_id:2})"
1) 1) "Nodes created: 2"
2) "Properties set: 6"
3) "Relationships created: 1"
4) "Query internal execution time: 0.848500 milliseconds"

Querying the graph#

Now that you have data in your graph, you’re ready to ask some questions, such as:

“What are the titles of all the movies?”#

> GRAPH.QUERY graph:movies "MATCH (m:Movie) RETURN m.title"
1) 1) "m.title"
2) 1) 1) "Star Wars: Episode V - The Empire Strikes Back"
2) 1) "The Godfather"
3) 1) "Query internal execution time: 0.349400 milliseconds"

“What is the information for the movie with the ID of 1?”#

> GRAPH.QUERY graph:movies "MATCH (m:Movie) WHERE m.movie_id = 1 RETURN m"
1) 1) "m"
2) 1) 1) 1) 1) "id"
2) (integer) 3
2) 1) "labels"
2) 1) "Movie"
3) 1) "properties"
2) 1) 1) "title"
2) "Star Wars: Episode V - The Empire Strikes Back"
2) 1) "release_year"
2) (integer) 1980
3) 1) "movie_id"
2) (integer) 1
3) 1) "Query internal execution time: 0.365800 milliseconds"

“Who are the actors in the movie 'Star Wars: Episode V - The Empire Strikes Back' and what roles did they play?”#

> GRAPH.QUERY graph:movies "MATCH (a:Actor)-[r:Acted_in]-(m:Movie) WHERE m.movie_id = 1 RETURN a.name,m.title,r.role"
1) 1) "a.name"
2) "m.title"
3) "r.role"
2) 1) 1) "Mark Hamill"
2) "Star Wars: Episode V - The Empire Strikes Back"
3) "Luke Skywalker"
2) 1) "Harrison Ford"
2) "Star Wars: Episode V - The Empire Strikes Back"
3) "Han Solo"
3) 1) "Carrie Fisher"
2) "Star Wars: Episode V - The Empire Strikes Back"
3) "Princess Leila"
3) 1) "Query internal execution time: 0.641200 milliseconds"

Visualizing graph databases using RedisInsight#

If you are using RedisInsight, you can visualize and navigate into the nodes and relationships graphically. Click on the RedisGraph menu entry on the left and enter the query:

MATCH (m:Actor) return m

Click on the Execute button, and double click on the actors to follow the relationships You should see a graph like this one:

RedisGraph

Resources#

Next Step#

Last updated on