Databases 6 min read

Getting Started with RedisGraph: Build and Query Graphs in Redis

This guide introduces RedisGraph, a Redis module that turns Redis into a high‑performance graph database, explains core graph concepts, shows how to create nodes and edges with Redis commands, and demonstrates queries using the Cypher‑like language.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Getting Started with RedisGraph: Build and Query Graphs in Redis

1. Introduction

Redis 4.0 introduced the Module system, allowing extensions such as RedisGraph, a high‑performance graph database built as a Redis module.

Graph databases consist of two core concepts: nodes (points) and edges (relationships). Nodes describe entities, edges describe relationships between entities.

Entity attributes are best stored in Redis Hash structures; a graph node corresponds to a Redis hash.

Example: two entities – a person “Obama” with attributes age 55 and occupation “former president”, stored as a hash with key obama; and a location “Hawaii” with attribute population, stored as a hash with key Hawaii. Adding a “born” edge between them creates a simple graph.

2. Usage Examples

(1) Create an edge

Syntax:

GRAPH.ADDEDGE graph_name, source, relationship, destination

Example:

GRAPH.ADDEDGE presidents "Barak Obama" born Hawaii

(2) Delete an edge

GRAPH.REMOVEEDGE presidents "Richard Nixon" born California

This removes the born relationship between the two nodes in the presidents graph.

(3) Query

GRAPH.QUERY presidents "MATCH (president)-[born]->(state:Hawaii) RETURN president.name, president.age"

The MATCH clause defines patterns (S)-[R]->(D), where S is the start node, R the relationship, and D the end node. RETURN specifies the fields to output.

3. Simple Comprehensive Example

We have two entity types – actors and movies – with the relationship acted_in .

(1) Create nodes

Actors (attributes: name, birth_year)

HMSET Aldis_Hodge name "Aldis Hodge" birth_year 1986
HMSET O'Shea_Jackson name "O'Shea Jackson" birth_year 1991
HMSET Corey_Hawkins name "Corey Hawkins" birth_year 1988
HMSET Neil_Brown name "Neil Brown" birthyear 1980

Movies (attributes: title, genre, votes, rating, year)

HMSET Straight_Outta_Compton title "Straight Outta Compton" genre Biography votes 127258 rating 7.9 year 2015
HMSET Never_Go_Back title "Never Go Back" genre Action votes 15821 rating 6.4 year 2016

(2) Create edges

GRAPH.ADDEDGE movies Aldis_Hodge act Straight_Outta_Compton
GRAPH.ADDEDGE movies O'Shea_Jackson act Straight_Outta_Compton
GRAPH.ADDEDGE movies Corey_Hawkins act Straight_Outta_Compton
GRAPH.ADDEDGE movies Neil_Brown act Straight_Outta_Compton
GRAPH.ADDEDGE movies Aldis_Hodge act Never_Go_Back

(3) Queries

Example 1: Compute total, max, min, and average age of actors in “Straight Outta Compton”.

GRAPH.QUERY movies "MATCH (actor)-[act]->(movie:\"Straight_Outta_Compton\") RETURN movie.title, SUM(actor.age), MAX(actor.age), MIN(actor.age), AVG(actor.age)"

Result:

1) "Straight Outta Compton,123.000000,37.000000,26.000000,30.750000"
2) "Query internal execution time: 0.108000 milliseconds"

Example 2: Count how many movies each actor has acted in.

GRAPH.QUERY movies "MATCH (actor)-[act]->(movie) RETURN actor.name, COUNT(movie.title) AS movies_count ORDER BY movies_count DESC"

Result:

1) "Aldis_Hodge,2.000000"
2) "O'Shea Jackson,1.000000"
3) "Corey Hawkins,1.000000"
4) "Neil Brown,1.000000"
5) "Query internal execution time: 0.071000 milliseconds"

4. Summary

RedisGraph implements core graph‑database operations and supports the mainstream Cypher query language. Although still young, as a Redis module it integrates tightly with Redis, allowing easy management via Redis clients; for modest requirements it is worth exploring.

Since Redis opened its module system, the ecosystem has begun to flourish, and we can expect increasingly powerful extensions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

graph databaseRedis ModulesGraph QueriesCypherRedisGraph
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.