Appearance
Neo4j Production Setup
Production Checklist
neo4j.conf (production):
conf
# Memory: lấy số từ `neo4j-admin server memory-recommendation --memory=<RAM>`
# Đo trên 5.26 cho máy 64 GB: heap 24g, page cache 28g, phần còn lại cho OS, Lucene và Netty
server.memory.heap.initial_size=24g
server.memory.heap.max_size=24g
server.memory.pagecache.size=28g
# Transaction log
db.tx_log.rotation.retention_policy=7 days
# Bolt connector
server.bolt.enabled=true
server.bolt.listen_address=:7687
server.bolt.thread_pool_max_size=400
# HTTP connector (for monitoring)
server.http.enabled=true
server.http.listen_address=:7474
# Security
dbms.security.auth_enabled=true
# Query logging: giá trị là OFF, INFO hoặc VERBOSE (true bị từ chối, đo)
db.logs.query.enabled=INFO
# Log queries >1s
db.logs.query.threshold=1s
# Ghi cả tham số truy vấn: có thể chứa dữ liệu nhạy cảm
db.logs.query.parameter_logging_enabled=true
# Chỉ Enterprise: Community không nhận ra các khoá dưới và từ chối khởi động (đo, strict validation)
# server.backup.enabled=true
# Mặc định server.backup.listen_address=127.0.0.1:6362 (chỉ máy cục bộ): backup từ máy khác (--from=10.0.1.5:6362 ở mục Backup) phải đặt server.backup.listen_address=10.0.1.5:6362
# server.backup.listen_address=10.0.1.5:6362
# dbms.cluster.minimum_initial_system_primaries_count=3
# Khoá discovery của cluster đổi tên giữa 5.x và 2025.x, xem Operations Manual đúng bảnConnection settings:
javascript
// Node.js (neo4j-driver); minh hoạ: host, user và mật khẩu là ví dụ
const neo4j = require('neo4j-driver')
const driver = neo4j.driver(
'neo4j://core1:7687', // một URI; muốn nhiều địa chỉ khởi động dùng `resolver` (doc: chỉ dùng cho kết nối ban đầu)
neo4j.auth.basic('neo4j', 'password'),
{
maxConnectionPoolSize: 50,
connectionAcquisitionTimeout: 60000, // 60s
// Routing của cluster (neo4j://)
connectionTimeout: 30000,
maxTransactionRetryTime: 30000
}
)
// Write to leader:
const writeSession = driver.session({
defaultAccessMode: neo4j.session.WRITE,
database: 'neo4j'
})
// Read from replica (load balanced):
const readSession = driver.session({
defaultAccessMode: neo4j.session.READ,
database: 'neo4j'
})Monitoring Queries
1. Check query performance:
cypher
// Bật query log (neo4j.conf): db.logs.query.enabled=INFO
// db.logs.query.threshold=1s (khớp neo4j.conf ở mục Production Checklist)
// View active queries (đo trên 5.26: allocatedBytes không phải cột, dùng currentQueryAllocatedBytes):
SHOW TRANSACTIONS YIELD transactionId, currentQuery, elapsedTime, currentQueryAllocatedBytes
WHERE elapsedTime > duration({seconds: 1})
RETURN transactionId, currentQuery, elapsedTime, currentQueryAllocatedBytes
ORDER BY elapsedTime DESC
// Kill slow query (Neo4j 5): SHOW TRANSACTIONS / TERMINATE TRANSACTIONS 'neo4j-transaction-123'2. Check memory usage:
cypher
// Neo4j 5: dbms.listPools() đã bỏ; dbms.queryJmx('org.neo4j:instance=kernel#0,name=Page cache') trả 0 dòng (đo trên 5.26).
// Heap và page cache lấy từ metrics (Enterprise: server.metrics.*, xuất ra Prometheus/CSV/JMX), tên metric trong Metrics reference.
// Alert if:
// - Heap usage >85%
// - Page cache hit ratio <90%3. Check open transactions:
cypher
SHOW TRANSACTIONS YIELD transactionId
RETURN count(*) AS openTx
// Alert if openTx >1000 (transaction leak)4. Check cluster health (Enterprise):
cypher
// Cluster overview (Enterprise; Community báo "Unsupported administration command: SHOW SERVERS", đo):
SHOW SERVERS YIELD name, address, state, health;
SHOW DATABASES YIELD name, address, role, currentStatus;
// Routing table (đo: ttl 300):
CALL dbms.routing.getRoutingTable({}, "neo4j")
YIELD ttl, servers
RETURN ttl, servers5. Check store sizes:
cypher
// Database sizes (cần plugin APOC Core; đo trên neo4j:5 với NEO4J_PLUGINS=["apoc"]):
CALL apoc.meta.stats() YIELD labels, relTypes, nodeCount, relCount
RETURN nodeCount, relCount
// Store file sizes:
// Check filesystem:
// du -sh /data/neo4j/databases/neo4j/*Backup Strategies
1. Online backup (Enterprise):
bash
# Full backup:
neo4j-admin database backup --from=10.0.1.5:6362 \
--to-path=/backup \
neo4j
# Incremental backup:
neo4j-admin database backup --from=10.0.1.5:6362 \
--to-path=/backup \
neo4j \
--fallback-to-full=false
# Dung lượng backup: doc không nêu tỉ lệ so với store, đo trên dữ liệu thật2. Offline backup (Community):
bash
# Stop Neo4j:
systemctl stop neo4j
# Dump thành MỘT file archive (database phải offline; đo trên 5.26 Community: neo4j.dump)
neo4j-admin database dump neo4j --to-path=/backup/
# Start Neo4j:
systemctl start neo4j3. Restore procedure:
bash
# Stop Neo4j:
systemctl stop neo4j
# Community: nạp lại từ file dump (đo: sau load, đếm đúng số node)
# RISK: --overwrite-destination=true ghi đè database neo4j hiện có (đo: dữ liệu cũ mất)
neo4j-admin database load neo4j --from-path=/backup --overwrite-destination=true
# Enterprise (minh hoạ, thay <timestamp>): khôi phục từ artefact backup; Community không có lệnh `restore` (đo)
neo4j-admin database restore --from-path=/backup/neo4j-<timestamp>.backup \
--overwrite-destination=true neo4j
# Start Neo4j:
systemctl start neo4jSecurity Hardening
1. Enable authentication:
cypher
// Change default password:
ALTER CURRENT USER SET PASSWORD FROM 'neo4j' TO 'strong-password';
// Create users (Community có CREATE USER, đo):
CREATE USER app_user SET PASSWORD 'password' CHANGE NOT REQUIRED;
// Role và quyền chỉ có ở Enterprise (Community: "Unsupported administration command: GRANT ROLE ...", đo):
GRANT ROLE reader TO app_user;
// Quyền tuỳ chỉnh cấp cho ROLE, không cấp trực tiếp cho user:
CREATE ROLE app_role;
GRANT MATCH {*} ON GRAPH neo4j TO app_role;
GRANT WRITE ON GRAPH neo4j TO app_role;
GRANT ROLE app_role TO app_user2. Enable TLS/SSL:
conf
# neo4j.conf:
server.bolt.tls_level=REQUIRED
dbms.ssl.policy.bolt.enabled=true
dbms.ssl.policy.bolt.base_directory=/etc/neo4j/certificates/bolt
dbms.ssl.policy.bolt.private_key=private.key
dbms.ssl.policy.bolt.public_certificate=public.crt3. Network security:
conf
# neo4j.conf:
# Chỉ lắng nghe IP riêng
server.bolt.listen_address=10.0.1.5:7687Firewall (cần root, chưa chạy):
bash
iptables -A INPUT -p tcp --dport 7687 -s 10.0.2.0/24 -j ACCEPT
iptables -A INPUT -p tcp --dport 7687 -j DROPPhải nhớ (Neo4j Production Setup):
- Community không có backup online, restore, cluster hay role: dùng
neo4j-admin database dump/loadkhi database offline (đo trên 5.26). - neo4j.conf không chấp nhận chú thích cùng dòng; khoá 5.x là
server.*, vàneo4j-admin server validate-configbắt khoá sai. - Cỡ heap/page cache lấy từ
neo4j-admin server memory-recommendation, không dùng tỉ lệ cố định.