Skip to main content

How to query Graph data in Redis using Python

End-of-Life Notice

Redis is phasing out RedisGraph. This blog post explains the motivation behind this decision and the implications for existing Redis customers and community members.

End of support is scheduled for January 31, 2025.

Beginning with Redis Stack 7.2.x-y, Redis Stack will no longer include graph capabilities (RedisGraph).

Profile picture for Ajeet Raina
Ajeet Raina, Former Developer Growth Manager at Redis

RedisGraph is the fastest graph database that processes complex graph operations in real time, 10x – 600x faster than any other graph database. Show how your data is connected through multiple visualization integrations including RedisInsight, Linkurious, and Graphileon. Query graphs using the industry-standard Cypher query language and easily use graph capabilities from application code.

My Image

RedisGraph Python Client

The 'redisgraph-py' is a package that allows querying Graph data in a Redis database that is extended with the RedisGraph module. The package extends redisgraph-py's interface with RedisGraph's API

Follow the steps below to get started with RedisGraph with Python:

Step 1. Run Redis Stack Docker container

 docker run -p 6379:6379 --name redis/redis-stack

Step 2. Verify if RedisGraph module is loaded

 info modules
# Modules

Step 3. Loading the Python Module

 pip install redisgraph

Step 4. Write a python code

 import redis
from redisgraph import Node, Edge, Graph, Path

r = redis.Redis(host='localhost', port=6379)

redis_graph = Graph('social', r)

john = Node(label='person', properties={'name': 'John Doe', 'age': 33, 'gender': 'male', 'status': 'single'})

japan = Node(label='country', properties={'name': 'Japan'})

edge = Edge(john, 'visited', japan, properties={'purpose': 'pleasure'})


query = """MATCH (p:person)-[v:visited {purpose:"pleasure"}]->(c:country)
RETURN, p.age, v.purpose,"""

result = redis_graph.query(query)

# Print resultset

# Use parameters
params = {'purpose':"pleasure"}
query = """MATCH (p:person)-[v:visited {purpose:$purpose}]->(c:country)
RETURN, p.age, v.purpose,"""

result = redis_graph.query(query, params)

# Print resultset

# Use query timeout to raise an exception if the query takes over 10 milliseconds
result = redis_graph.query(query, params, timeout=10)

# Iterate through resultset
for record in result.result_set:
person_name = record[0]
person_age = record[1]
visit_purpose = record[2]
country_name = record[3]

query = """MATCH p = (:person)-[:visited {purpose:"pleasure"}]->(:country) RETURN p"""

result = redis_graph.query(query)

# Iterate through resultset
for record in result.result_set:
path = record[0]

# All done, remove graph.

Step 5. Execute the Python Script

| b'' | b'p.age' | b'v.purpose' | b'' |
| John Doe | 33 | pleasure | Japan |

Cached execution 0.0
internal execution time 3.3023
| b'' | b'p.age' | b'v.purpose' | b'' |
| John Doe | 33 | pleasure | Japan |

Cached execution 0.0
internal execution time 0.2475

Step 6. Monitor the Graph query> monitor
1632661901.024018 [0] "GRAPH.QUERY" "social" "CREATE (youkjweasb:person{age:33,gender:\"male\",name:\"John Doe\",status:\"single\"}),(jilbktlmgw:country{name:\"Japan\"}),(youkjweasb:person{age:33,gender:\"male\",name:\"John Doe\",status:\"single\"})-[:visited{purpose:\"pleasure\"}]->(jilbktlmgw:country{name:\"Japan\"})" "--compact"
1632661901.025810 [0] "GRAPH.QUERY" "social" "MATCH (p:person)-[v:visited {purpose:\"pleasure\"}]->(c:country)\n\t\t RETURN, p.age, v.purpose," "--compact"
1632661901.027485 [0] "GRAPH.QUERY" "social" "CYPHER purpose=\"pleasure\" MATCH (p:person)-[v:visited {purpose:$purpose}]->(c:country)\n\t\t RETURN, p.age, v.purpose," "--compact"
1632661901.029539 [0] "GRAPH.QUERY" "social" "CYPHER purpose=\"pleasure\" MATCH (p:person)-[v:visited {purpose:$purpose}]->(c:country)\n\t\t RETURN, p.age, v.purpose," "--compact" "timeout" "10"
1632661901.030965 [0] "GRAPH.QUERY" "social" "MATCH p = (:person)-[:visited {purpose:\"pleasure\"}]->(:country) RETURN p" "--compact"
1632661901.032250 [0] "GRAPH.RO_QUERY" "social" "CALL db.labels()" "--compact"
1632661901.033323 [0] "GRAPH.RO_QUERY" "social" "CALL db.propertyKeys()" "--compact"
1632661901.034589 [0] "GRAPH.RO_QUERY" "social" "CALL db.relationshipTypes()" "--compact"
1632661901.035625 [0] "GRAPH.DELETE" "social"

Let us comment out the last line. Try to query Graph data over over RedisInsight.

Step 7. Install RedisInsight

Run the RedisInsight container. The easiest way is to run the following command:

 docker run -d -v redisinsight:/db -p 8001:8001 redislabs/redisinsight:latest

Step 8. Accessing RedisInsight

Next, point your browser to http://localhost:8001.

Step 9. Run the Graph Query

You can use the limit clause to limit the number of records returned by a query:


My Image

Step 10. Run the Graph Query with no LIMIT


My Image


  • Learn more about RedisGraph in the Quickstart tutorial.

Redis Launchpad