Christopher Flynn

Data Scientist
Data Engineer
PhD Mathematician

Open Source



RedisGraph Elixir client

2019-09-30 Feed

Over the past few months, I’ve done research into a number of graph database products as database backend solutions for some core platform architecture. One of the products that I found was RedisGraph, a graph db module that runs inside of redis, an in-memory key-value database which is typically used for caching, pub/sub, and message queues.

RedisGraph implements openCypher’s Property Graph Model, which describes a directed, labeled multigraph in which both node and edge entities may have properties.

The query implementation is a subset of the Cypher query language, which is described as being expressive, efficient, and human-readable. Cypher was originally created at Neo4j.

Redis provides a number of client library implementations in a number of languages, including Python, Javascript, Go, Java, and Ruby.

I noticed also that there wasn’t an Elixir implementation, nor could I find one from a third party. Since I wanted to get better at the language, I figured I would write one myself.

I used the Redix library’s GenServer to communicate with redis. Most of the Elixir code I wrote is a port of the Python client implementation, with changes to suit the purely functional nature of Elixir.

To launch a redisgraph database instance locally, use

docker run -p 6379:6379 -it --rm redislabs/redisgraph

Here is a simple example on constructing a RedisGraph.Graph and fetching the results of a MATCH query:

alias RedisGraph.{Node, Edge, Graph, QueryResult}

# Create a connection using Redix
{:ok, conn} = Redix.start_link("redis://localhost:6379")

# Create a graph
graph ={
  name: "social"

# Create a node
john ={
  label: "person",
  properties: %{
    name: "John Doe",
    age: 33,
    gender: "male",
    status: "single"

# Add the node to the graph
# The graph and node are returned
# The node may be modified if no alias has been set
# For this reason, nodes should always be added to the graph
# before creating edges between them.
{graph, john} = Graph.add_node(graph, john)

# Create a second node
japan ={
  label: "country",
  properties: %{
    name: "Japan"

# Add the second node
{graph, japan} = Graph.add_node(graph, japan)

# Create an edge connecting the two nodes
edge ={
  src_node: john,
  dest_node: japan,
  relation: "visited"

# Add the edge to the graph
# If the nodes are not present, an {:error, error} is returned
{:ok, graph} = Graph.add_edge(graph, edge)

# Commit the graph to the database
{:ok, commit_result} = RedisGraph.commit(conn, graph)

# Print the transaction statistics

# Create a query to fetch some data
query = "MATCH (p:person)-[v:visited]->(c:country) RETURN, p.age, v.purpose,"

# Execute the query
{:ok, query_result} = RedisGraph.query(conn,, query)

# Pretty print the results using the Scribe lib

which gives the following results:

# Commit result statistics
  "Labels added" => nil,
  "Nodes created" => "2",
  "Nodes deleted" => nil,
  "Properties set" => "5",
  "Query internal execution time" => "0.228669",
  "Relationships created" => "1",
  "Relationships deleted" => nil

# Query result pretty-printed
| ""       | "p.age"     | "v.purpose"     | ""     |
| "John Doe"     | 33          | nil             | "Japan"      |

Further reading

RedisGraph Elixir




Back to the posts.