Take Neo4j for a test spin with a CRUD demo based on Granny's Addressbook
My fans know that my standard test for everything from PaaS (platform as a service) to alternative programming languages is something I call Grannyβs Addressbook, a simple CRUD application. As it turns out, Grannyβs Addressbook is also an excellent test for graph databases, the black sheep of the NoSQL quadrumvirate (see βWhich freaking database should I use?β). To add functionality to the Addressbook, weβve used the leading graph database: Neo4j. Thanks to Phil Rhodes for his contributions to this article and for writing my code for me so that I had more time to cozy up to a nice glass of Lagavulin, neat. β Andy Oliver
My kidsβ paternal grandmother β I call her βMomβ β has a problem that is not solved by her killer app, Grannyβs Addressbook. Frequently, she asks me what gift to get the kids for the all-too-frequent stream of birthdays, Hanukkah, Christmas, Kwanzaa, Arbor Day, and whatever holidays. Frequently, I inform her that the children are already spoiled and Iβve gotten them anything they need and if not, there is a good reason that I donβt want them to have it. Those reasons vary from βitβs flammableβ to βitβs a weapon if you hold it rightβ to βit makes annoying noises that I donβt want to hearβ to the more common βI donβt feel like figuring out how to assemble it and by the time it gets assembled the pieces will have been lost.β
In other words, Grandma has a data problem. If she were to map out what all of the kidsβ friends or other children close to their age got for their birthday, she could easily figure out what to buy them without asking me.
As you recall, Grannnyβs Addressbook is a sample app Iβve used to demonstrate various technologies. It is essentially a one-table CRUD app that stores addresses in a database. It has no search or security and it doesnβt really need to scale. The GUI looks like this:
Click to enlarge.
In this case, the gift-giving problem has landed Granny in a complex data dilemma. She needs to know the kidsβ ages and their relationships to other people. She also needs to know what each kid got for his or her last birthday. We will represent this with three rather naive fields: First is βFriends,β which is a multiselect list box. The second is βBirthday.β The third is βLast Present,β representing gifts from their most recent birthday. So long as those fields are populated, weβll populate another field called βSuggested Presents,β which is a list of gift ideas. The new GUI looks like this:
Click to enlarge.
We essentially want to recommend a gift for a person based on what each of friend and friends of friends got for their last birthday. We also want to order the gifts by the distance of the relationship.
For example: Tommy is friends with Billy and Bryan. Bryan is friends with Kelly and Jamie. Jamie is friends with Steven. Billy is friends with Keith. We can look at the βdistance of the relationshipsβ thusly:
Tommy = 0 (the origin)
Billy = 1
Bryan = 1
Kelly = 2
Jamie = 2
Keith = 2
Steven = 3
Doing this in a relational database structure is pretty painful. In the JPA/SQL version of granny, I need two tables and multiple trips to the database just to walk the graph of relationships. I also have to do pretty much all the ordering and logic myself.
In other words, this is a classic graphical database problem. Relationships matter as much as, if not more than, the data itself. Neo4j is the most popular graph database on the market these days. While graph databases are part of the NoSQL movement, they really solve different problems than, say, Couchbase or MongoDB. We arenβt necessarily concerned with handling massive scale or doing analytics across terabytes of big data a la Hadoopβs HBase. In fact, most graph databases are transactional, and the reason they are NoSQL is that SQL is simply inadequate to express the problems, as you can see in the amount of code it took in the findSuggestions method.
For the Granny4j version using Neo4j the main query comes down to this:
// select friends and friends of friends, order by depth of the relationship
String findFriendsQuery = "start n=node(*), person=node({userNode}) MATCH p = (person)-[:FRIEND*1..2]-(friend) return distinct p order by length(p)";
As you can see there is a lot less code β and it does the job. Itβs also more efficient. Check out all the code for Granny4j.
Why is this important? Theoretically, you can hire offshore developers for as little as $15 an hour who know SQL β meaning the technology and people who know it are commoditized. Neo4j presumably requires more expensive expertise that is in lower demand. Nonetheless, thereβs always a correlation between lines of code and the number of bugs. We can decrease downtime and errors by decreasing the number of bugs per line, but itβs an expensive process, and ultimately, itβs easier to decrease the number of lines of code.
Thereβs also a big efficiency issue. Even on my laptop, the unit test for GrannyJPA takes considerably longer than Granny4j. If you consider this at the kind of scale that a major retailer would require and take into account the law of diminishing returns, thereβs a real performance and scalability issue.
The biggest objections to introducing a new structured storage technology are usually related to the experience with the technology within the organization or βsingle source of record.β While the latter concern is indeed a problem when combining many types of NoSQL databases with an existing SQL database, it wouldnβt be a problem with Neo4j. Like most graph databases, Neo4j is transactional. As for the former consideration, that exists with any new or in this case different technology.
Personally, Iβd rather be moving forward at a deliberate pace and finding new efficiencies than standing in place because itβs what Iβve always done. Moreover, graph database technology isnβt that much younger than the RDBMS.
As Iβve mentioned in the past, it all comes down to data structures. By using the RDBMS for everything over the last few decades, the industry has done the equivalent of using a list for every data structure. You wouldnβt use only one data structure for every type of data in memory, why would you do that just because youβre storing the data?
Sadly, my mom refuses to use any of the fancy tools Iβve developed. Sheβs just stopped asking me what to get the kids and asks my wife instead.
This article, βWhat are graph databases good for? Hereβs a killer app,β was originally published at InfoWorld.com. Keep up on the latest developments in application development and NoSQL, and read more of Andrew Oliverβs Strategic Developer blog at InfoWorld.com. For the latest business technology news, follow InfoWorld.com on Twitter.


