8 Solid Tips for Succeeding With Neo4j
Check out Chris Leishman's presentation on how to achieve optimal performance with Neo4j and be sure to follow these 8 tips for success.
Join the DZone community and get the full member experience.Join For Free
editor’s note: last october at graphconnect san francisco , chris leishman–executive manager at neo technology–delivered this presentation on how to achieve optimal performance with neo4j.
for more videos from graphconnect sf and to register for graphconnect europe, check out graphconnect.com .
my name is chris leishman, and i’ve been with neo technology for about four years. throughout this period, i’ve come to understand the successes and challenges customers have faced in their journeys to graph databases.
[note: this post is a great complement to the blog post by stefan ambruster , which goes over neo4j worst practices.]
the origins of neo4j
so, why do customers choose neo4j? the first question you should really ask is: why did we create neo4j in the first place?
because, we fundamentally believe that there is a better way to work with data. the best way to do this is by focusing on the relationships that exist in that data because it’s not enough to just have data; we want insight into our data.
with relational databases we have divided datasets stored in tabular structures. consider the following example with flights, airports, and cities:
to find out how the data is connected, we have to rely on a variety of foreign keys:
below is exactly the same data drawn in a graph structure, which shows us much more quickly how the different data points are related:
before graphs, we were almost entirely focused on how we store and retrieve data. below is a model developed by one of my colleagues, alistair , called the relational crossroads:
on the denormalise side, we have nosql, relational databases, and databases such as mongo and react that try to simplify data storage. by dividing and spreading out the data, we perform more rapid retrievals and updates to make our database very mapreduce -friendly.
on the other side, you have richer model, which includes a more connected understanding about structured data, expressive power, and fast traversals. so, while the nosql community is exploring this denormalization approach for storage simplification, neo4j is going down the richer model path to simplify the understanding instead.
when emil first started neo4j, he wanted to find a better way to build software than the hierarchical model that he had at his disposal. and once he had found that experience, we wanted to enable others to do the same. and, that’s ultimately how neo4j got to be here.
now that we understand the history of neo4j and what we’re trying to achieve, let’s dive into the eight best practices for using neo4j.
number 8: use a rich data model
the first place to start in data modeling is to identify your domains, i.e. the data points in your database. are they people, seeds, products or relationships? a good book for software engineers to reference is called domain-driven design by eric evans. interestingly, a lot of the concepts in the book about software design apply to graph databases as well. this means that the models designers use in software can also be used in databases, which is very powerful because simplifies the code and the understanding.
the best way to start data modeling is to draw out your domain on a whiteboard. you can give anyone a whiteboard and a pen, talk about the problem they are trying to solve, and end up with a drawn-out graph.
for example, if i want to talk to you about the cancellation of my flight from new york to san francisco, i’ll draw a graph:
we found that when given a whiteboard and pen, person after person will draw exactly the same thing. and, with a graph database you can translate this drawing into a few expressions, add some conditions and constraints, and you’ve already started modeling a database:
the best way to get really good at modeling is to study. the following resources are a great place to start:
tip #7: use cypher — carefully
the purpose behind building neo4j is to give you a better way to work with data. and, how do you do this? with cypher , a declarative, pattern-matching language for connected data.
declarative means it’s a language in which you describe what you want. rather than telling the database to do something specific, you describe what you want the database to give you. pattern matching means we describe patterns, ask the database to match those patterns and return them.
if we want to know all the possible reasons our flight the sfo to jfk was cancelled, written in cypher it would look like the following:
it’s amazing how easy it is to get this kind of information out of your data once you have a language like cypher at your disposal.
below are some helpful tools for transitioning from sql to cypher:
- even though cypher was inspired by sql, it’s helpful to learn the major differences between the languages. this guide will help you transition from sql to cypher.
- the cypher developer guide provides examples of all the different ways you can use cypher.
- this guide will help you understand how cypher queries are evaluated.
- you can also learn more in the cypher query language documentation .
- and finally, there are details in the actual neo4j product that show you how to go from sql to cypher. the application includes the northwind dataset—a sample database that is well-known in the relational database community—and allows you to work with it in neo4j as an introduction.
avoid cartesian products
while there are huge advantages to using cypher, it does have its own quirks. one thing you will need to learn how to avoid is a cartesian product .
in the following example, we are looking for two people in our database so we write the following query:
this could work, but only if you have two people in your database. even then there are two different results that will be returned:
there are even more possibilities if you have three people in your database:
as you can see, you could very quickly end up with billions of results from what looks like a very simple query. be sure to use the available query planners to help you avoid these types of queries.
avoid large result sets that build up memory
large result sets are the types of results returned from a cartesian product, but can result from other types of queries as well. large results will quickly fill up your memory storage, but there are tools available for streaming apis that allow you to stream data rather than building up large result datasets.
check your indexes and use labels
make sure to use labels on your queries and nodes. when you’re writing multiple queries in cypher, you can describe the corresponding labels for that query. labels are a way of designating a node with a particular type, whether that be a person or a product (this will be specific to your own domain).
the only time you use an index is to find a specific node in a graph. labeling your nodes prevents the index from searching through the entire database to find that one point you are looking for.
let’s go back to my previous example with flights. i often want to look up airports by code or flights by name, so i add indexes for those in order to start my query at those types of nodes:
when the database performs the query, it will see that i’ve got an index on flight names, so it will start looking up data at the nodes labeled with
. however, because we also created an airport code, it could start with a node labeled
. the database will make a choice between the two indexes based on statistics along with a number of other factors that we won’t get into here.
something to keep in mind with indexes is that each one needs to be maintained and stored on a disk. query planning is a must when determining how to use indexes, because it will help you determine what types of questions you will ask, and therefore the types of indexes you’ll need.
to understand which index your database chose and whether or not it was the right one, you can use cypher profiling. it’s a great tool to understand what your queries are doing and where the costs are:
based on this information, you can work with your team to figure out how to improve your queries.
use separate queries
cypher is a very powerful and expressive language that allows us to do much more than we could do in sql. but just as in sql, there are huge mistakes you can make in cypher.
don’t write extremely long, blocked queries. while cypher can take many separate queries and chain them together in a dataflow using the
clause, you don’t have to do everything in one shot. you should never write code that looks like the below:
tip #6: don’t use cypher — carefully
we think cypher is fantastic, but we know it’s not for everything. fortunately, we built neo4j for developers, and it can be combined with a lot of other tools to be effective.
neo4j was originally written as a driver, like an embedded database you could include into java code for a developer to then build on top of:
cypher is the very top surface of neo4j. we have a rest api, and you can do a rest call to get back an individual node or an individual relationship.
don’t fear server extensions!
java server extensions exist to solve a specific problem, and in some cases do so very well.
we’ve worked with many customers and taken queries that perform okay within cypher, but their particular needs go above what cypher can provide. the java extensions can provide a vast increase in performance and provide very fine-grained control over how a query is run and allow you to optimize it far beyond anything a declarative language could do.
my colleague max de marzi is our guru of writing java server extensions. he works with our customers and writes a lot of server extensions to solve of their problems. max also runs a very prolific blog that includes posts on how to write server extensions and how to get the best possible performance out of neo4j.
below is a classic server extension written by max, which is his warm-up routine:
with this extension you hit the
endpoint and it iterates through every node in the graph, loads it into memory and lets it go again. all it’s doing is bringing all those pages off disk into the page caches and warming everything up. at that point the system is warmed up and ready to go, giving the best performance possible.
tip #5: do performance testing
this is the number one thing people often miss. they’re excited to go into production and want to launch the product but end up having to stop everything at the last minute to try and figure out why something isn’t working properly. max has written another great blog post on using gatling with neo4j for performance testing.
tip #4: tune the server configuration
every server needs to be tuned for your environment. this will include configuring the page cache size which will allow you to use as much memory from the page as possible, preferably larger than the store file, while also allowing room for growth. and before you do anything else, it’s important to turn on the gc logging, which will track your memory use. keep in mind that it logs a lot of information so you do have to watch the size of the logs.
monitoring gc pauses, which indicates pauses in the running of your application, is also important. in a production environment, this type of issue will cause you to run into trouble. so set your performance tests, run the tests, adjust your configuration, monitor and repeat either before every new release or after a significant growth in the size of your data.
tip #3: get a cluster running
a lot of people leave this until too late in their process of going live with neo4j. before deploying a cluster, they try and build everything on a single instance for as long as they can.
this can cause a lot of problems because the semantics are likely to change. distributed systems are hard, but the best thing to do is embrace that pain as early as possible by using a cluster in your development/staging environment. there’s a lot of great information available on our website about how distributed databases work.
what you’re trying to learn with clustering is how data will move through your cluster. you write to a slave, it gets propagated to the master and the master propagates those out to all the slaves:
another important tip: as early as possible in the application design process, figure out how to separate reads and writes. this will enable optimizations in terms of how you talk to neo4j, as certain servers in the cluster will be better for accepting writes, while others are better for reads.
tip #2: pick an awesome driver
tip #1: have a relationship with the neo4j team
neo4j’s mission is to give people a better way to work with data. we want you to succeed, and having a relationship with us will make it much easier for you to achieve success. we’re very easy to get in touch with!
inspired by chris’ talk? register for graphconnect europe on 26 april 2016 for more presentations, workshops and lightning talks on the evolving world of graph database technology.
Published at DZone with permission of Chris Leishman, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.