Skip to content
David Butler edited this page Jul 10, 2013 · 26 revisions

Introduction

Neo4j.rb (any of the neo4j-core, neo4j-wrapper and neo4j gems) can be used in a HA Cluster. The Neo4j HA Cluster can also be useful as a development tool. By using HA cluster you can have write support for the rails console, or use the Neo4j Server Admin UI to monitoring and debugging the graph database.

Notice, there is no need to install the neo4j server in order to run a Neo4j(.rb) HA Cluster. For example, to run several Neo4j Rails instances you only need to start the rails server just like you do with using a local embedded neo4j database (zookeeper is not needed). The only thing needed is edit the neo4j.rb configuration and add a gem dependency to the neo4j-enterprise gem, see below.

More info: Neo4j HA documentation and HA Cluster for example of Neo4j::Config configuration. Also check the license for neo4j-enterprise (different to neo4j-community).

Make sure you are using neo4j.rb version >= 2.3.0

Rails Console with Write Support

The rails console will start up in read-only mode if you already have a running rails application since Neo4j does not allow write access to the database from several processes.

As a workaround you can use Neo4j HA which will replicate databases between processes. To enable write access from a rails console using Neo4j HA:

  • Add development dependencies to your Gemfile: neo4j-advanced and neo4j-enterprise, example:
gem 'neo4j-community', '1.9'
gem 'neo4j-advanced', '1.9'
gem 'neo4j-enterprise', '1.9'
  • in your config/application.rb, add
require 'neo4j/rails/ha_console/railtie' if Rails.env.development?

Note: Due to the way Rails loads Railties, it currently isn't possible to put this into development.rb.

  • Modify/Add line: config.neo4j.storage_path = ... unless Rails.env.development? This is needed since the ha_console/railties will set the location of the database (rails console and rails server will have different databases which will be synchronized).

This works in neo4j version 2.3.0

Using the Neo4j Server Admin

Follow the instruction above for using rails console with write support.

  • Download the neo4j-enterprise from here (version >= 1.9.M03)
  • Unpack the file.
  • Edit the conf/neo4j.properties file
ha.server_id=3
ha.initial_hosts=localhost:5001,localhost:5002,localhost:5003
ha.server=localhost:6003
ha.cluster_server=localhost:5003
ha.pull_interval=1
  • Edit the conf/neo4j-server.properties
# Uncomment this line:
org.neo4j.server.database.mode=HA
  • Start the server, bin/neo4j start

Notice its possible to use both the rails console using Neo4j HA and at the same time use the Neo4j Server UI using Neo4j HA.

A Complete Example

See screencast or follow the instructions below.

Generate a normal Rails Application

  • Generate a new normal Rails Application
rvm jruby # make sure you are using JRuby
gem install rails
rails new myapp -m http://andreasronge.github.com/neo4j/rails.rb -O
cd myapp

Update Gem Dependencies

  • Edit the Gemfile (add neo4j-community, neo4j-advanced and neo4j-enterprise) see above or Gemfile

  • Download and resolve gem dependencies

bundle update

Scaffold a Rails User

  • Generate model,view controller for User
rails generate scaffold User name:string email:string
  • You have just create a normal Rails application but using Neo4j as database,
rails s

And open a browser http://localhost:3000/users. If you open a rails console you will only have read only support, since only one process can access the database at the time. As a workaround you can use a HA Cluster instead.

Configure HA Cluster

  • Stop the rails server (Control C)

  • Edit two lines in the config/application.rb, see above or this

  • Start rails again, rails s

  • Start a Rails console in another terminal window rails console

  • Start neo4j, Neo4j.start see issue below.

  • Test it, for example: ** Create a user in the browser. ** Check that it replicates to the Rails console database, User.all.count ** Create a User in the Rails console, User.create(:name => 'kalle', :email=>'[email protected]')

Visualize DB with Neo4j Server

See above which lines in the neo4j server configuration you need to edit or here are the complete configuration files:

Issues

Neo4j 1.9.M03 was the last version that would let a HA cluster start without a majority of the nodes in the cluster. This means that to use neo4j 1.9 (neo4j.rb 2.3.0) you must start the server and load a page so that Neo4j starts. It will continue waiting until you also start the console and trigger Neo4j to start (e.g. Neo4j.start) see see https://github.com/andreasronge/neo4j/issues/258

Clone this wiki locally