# Neo4j Graph Database

> Neo4j graph database with web browser interface.

- Category: Database Stacks
- Difficulty: beginner
- Components: neo4j
- Tags: neo4j, graph, database, cypher, nosql
- Source: https://docker.recipes/databases/neo4j-graph-database

## Overview

Neo4j is a native graph database that revolutionizes how applications handle connected data by storing information as nodes and relationships rather than traditional tables. Originally developed in 2007 by Neo Technology, Neo4j uses its proprietary Cypher query language to traverse complex relationship patterns with exceptional performance, making queries that would require expensive JOINs in relational databases execute in milliseconds. The database combines ACID compliance with graph-native storage, allowing applications to model real-world connections naturally while maintaining data integrity.

This Docker configuration deploys Neo4j with the APOC (Awesome Procedures on Cypher) plugin pre-installed, providing extended functionality for data integration, graph algorithms, and utility procedures. The setup exposes both the HTTP interface for browser-based interaction and the high-performance Bolt protocol for application connectivity. Memory allocation is configurable through environment variables to optimize performance based on dataset size and query complexity.

Developers building recommendation systems, fraud detection platforms, or knowledge management applications will find this stack particularly valuable. The inclusion of APOC procedures enables advanced data manipulation and analysis capabilities that extend Neo4j's core functionality, while the containerized deployment ensures consistent behavior across different environments. Organizations dealing with highly connected data - from social networks to supply chain management - can leverage Neo4j's relationship-first approach to uncover insights that traditional databases struggle to reveal.

## Features

- Native graph storage engine optimized for relationship traversal
- Cypher declarative query language for intuitive graph pattern matching
- APOC plugin providing 450+ procedures for data integration and analysis
- Neo4j Browser interface for visual query development and result exploration
- High-performance Bolt protocol for low-latency application connectivity
- ACID transaction support ensuring data consistency in concurrent operations
- Configurable JVM heap allocation for workload-specific performance tuning
- Built-in graph algorithms library for centrality, community detection, and pathfinding

## Use cases

- Social network platforms analyzing user connections and content recommendations
- Financial institutions detecting fraud through transaction relationship analysis
- E-commerce sites building real-time product recommendation engines
- Enterprise knowledge graphs connecting disparate data sources and systems
- Identity and access management systems modeling complex permission hierarchies
- Supply chain optimization tracking product flows and vendor relationships
- Bioinformatics research mapping protein interactions and genetic pathways

## Prerequisites

- Minimum 1GB RAM available for Neo4j container (4GB+ recommended for production)
- Ports 7474 (HTTP) and 7687 (Bolt) available on the host system
- Docker Engine 20.10+ and Docker Compose V2 for optimal compatibility
- Basic understanding of graph database concepts (nodes, relationships, properties)
- Familiarity with Cypher query language syntax for effective database interaction
- At least 10GB available disk space for database storage and transaction logs

## docker-compose.yml

```yaml
services:
  neo4j:
    image: neo4j:latest
    container_name: neo4j
    restart: unless-stopped
    ports:
      - "${HTTP_PORT:-7474}:7474"
      - "${BOLT_PORT:-7687}:7687"
    environment:
      - NEO4J_AUTH=${NEO4J_USER:-neo4j}/${NEO4J_PASSWORD:-password}
      - NEO4J_PLUGINS=["apoc"]
      - NEO4J_dbms_memory_heap_initial__size=${HEAP_INIT:-512m}
      - NEO4J_dbms_memory_heap_max__size=${HEAP_MAX:-1G}
    volumes:
      - neo4j_data:/data
      - neo4j_logs:/logs
      - neo4j_plugins:/plugins
    networks:
      - neo4j-network

volumes:
  neo4j_data:
  neo4j_logs:
  neo4j_plugins:

networks:
  neo4j-network:
    driver: bridge
```

## .env

```bash
# Neo4j
HTTP_PORT=7474
BOLT_PORT=7687
NEO4J_USER=neo4j
NEO4J_PASSWORD=password
HEAP_INIT=512m
HEAP_MAX=1G
```

## Usage notes

- Browser at http://localhost:7474
- Bolt protocol at localhost:7687
- Login with configured credentials
- APOC plugin included

## Troubleshooting

- Neo4j fails to start with 'Cannot allocate memory' error: Reduce NEO4J_dbms_memory_heap_max__size value or increase available system memory
- Browser shows 'ServiceUnavailable' when connecting: Check that Bolt port 7687 is accessible and Neo4j container has finished initialization
- APOC procedures not available in queries: Verify NEO4J_PLUGINS environment variable includes 'apoc' and container has restarted after configuration change
- Authentication fails with default credentials: Ensure NEO4J_AUTH format follows 'username/password' pattern and container has been recreated after password change
- High memory usage with OutOfMemoryError: Adjust heap size settings and consider adding swap space or upgrading system RAM
- Slow query performance on large datasets: Enable query logging with NEO4J_dbms_logs_query_enabled=true to identify inefficient Cypher patterns

