DEV Community

Cover image for Getting started with Cypher and RedisGraph / Part I
Gianluca Fabrizi
Gianluca Fabrizi

Posted on

Getting started with Cypher and RedisGraph / Part I

I keep a personal to-do list where i annotate technologies and frameworks i'd like to learn. Aside from the perpetual "learn Rust", there's been a "learn graph db" for months now.
Now it's time to start exploring it.
What better way to understand how much I've learned than writing a serie of posts about it?

In this first post I try to explain what is a Graph DB, why I choose RedisGraph and how to use it locally. In the next post we will see how to query the db, how to edit and add relationships. In the third (and last) post we'll be looking at some examples.

Without further ado, let's briefly see what a Graph database is.

Graph DB

What is a Graph database?

In "classic" relational and NoSQL databases, data are structured in a tabular way (you may think of it as a spreadsheet); this makes querying from relations between data harder (usually you have to join two or more tables).
Graph databases are structured following a graph data model.
In "graph data model" relationships between nodes are the priority and the way you lay out your data is more expressive.
Usually Graph databases come with a visualization tool that lets you see how your data is connected, thus helping you getting useful insight from your data.
The rigid schema of a relational database make it hard seek for data relationships, whereas a graph db can make it easy.

Why use a Graph database?

So graph DBs allow you to better structure and visualize your data. But why you may need one?

Top use cases are: Real-time recommendation engines, Knowledge graphs and Fraud detection
But you can also think of a social media network where people are connected with different kind of relations.

Due to their flexibility, Graph DBs can easily adapt to your data model, even if it changes over time.
One of the approach that is become more and more relevant is to use Graph databases in addition to traditional data store.
For instance you can use a MySQL db to store your users, products and sales. Than you can have a Graph db to map your users habits and create your custom recommendation engine.
You always have to keep in mind that each db type has it's own preferred use: graph db wins easily when you have to traverse relationships between entities, while a SQL db is more efficient in aggregating and grouping results.

RedisGraph

Why RedisGraph?

Nowadays there are multiple Graph DBs: Neo4j is the first that comes in mind. Then there are AWS Neptune, ArangoDB, TypeDB, etc...

  • Neo4j has some tricky licensing and they seem to push hard on the "Enterprise" plan: with the community edition you can't have more than one database per installation and you can't create a cluster.
  • Amazon Neptune is a "pay as you go solution", not suitable for learning. It uses Apache TinkerPop Gremlin and W3C’s SPARQL as query language. It's way too easy to be locked-in.
  • ArangoDB has a unique multi-model approach (nodes are essentially documents stored in collections). It uses AQL, a proprietary query language.
  • TypeDB uses it's own query language (TypeQL); you can be locked-in pretty quick.
  • RedisGraph is a pluggable module for Redis, developed by RedisLabs. While being younger than it's direct competitor Neo4j (and still not as feature rich), it's simpler and faster. New features are being included in each version.

Graph Elements

A graph database consists of:
Nodes: nodes represents entities in your domain.
Edges: edges are the connections between nodes. An edge represents a relationship between a start node and an end node, thus giving the relationship a "direction".
Properties: properties are key-value pairs connected to nodes or edges. With properties you can store extra informations about an entity or a relation
Labels: labels help you categorize elements (nodes and edges)

An easy way to think about graphs is as analogous to the relationship between nouns and verbs. Nodes, or the nouns, are things such as people, places, and items. Relationships, or the verbs, are how they’re connected. People know each other and items are sent to places. The signal in those relationships is powerful. (source)

Running RedisGraph locally

To get started you can go on https://redis.com and sign up for a free Redis cloud tier. The free subscription includes 1 dedicated database, 30MB of RAM and RedisGraph module, so you can use it to start learning RedisGraph.

The most interesting way to start exploring RedisGraph though is by running a local docker container.
Here's a simple docker-compose.yml file:

version: "3"
services:
    redis-stack:
        image: redis/redis-stack:latest
        container_name: redis-stack
        volumes:
            - ./local-data/:/data
        expose:
            - 6379
            - 8001
        ports:
            - "6379:6379"
            - "8001:8001"
        environment:
            - PATH=./bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Enter fullscreen mode Exit fullscreen mode

You can launch it with docker compose up -d.
If you open your browser and point it to http://localhost:8001 you will see the main interface of RedisInsight.
RedisInsight is the web UI of Redis.
When you finished, you can stop the container with docker compose stop

In the next post

We've covered the basics of what a Graph database is and how it can be used.
In the next post we'll see how to create a database and use Cypher to query it.

Top comments (0)