Your AuraDB is probably bigger than it needs to be

How to reduce your database size and save money

A few weeks ago, I found myself at a conference in Gay Paree — that's Paris for those who don't speak el Français.

I had been delivering a talk on the joys of data modeling for Stranger Graphs — an AI GraphRAG, Stranger Things plot predictor with Graph Data Science enhancements.

Anyway, question-time arrived.

"Why's it so f***ing expensive?!" yelled Mr. Somebloke.

Silence gripped the scent of deep-dish perfume and high-end cerveza — that's "beer" in French.

You, dear reader, may be feeling the pinch, so I am here to show you the map to the Fountain of Compaction: you're about to learn how to compact your Neo4j Aura database and save $$$.

Why your AuraDB is massive

Deleting a node does not shrink your database. The store files stay the same size on disk.

Neo4j uses logical deletes. Giving that space back to the operating system would cost I/O operations and slow your transactions. Neo4j just marks the record as deleted — fast, painless, and the space stays allocated.

In other words, the space taken by every node you've ever deleted lives on in the database underworld. New writes can reuse it, but nothing gives that space back automagically.

Charon ferrying nodes across the Acheron
Charon ferries your nodes across the Acheron, where they live on 'twixt worlds. (Firefly)

Fast? Blinding.

Space-saving, no.

Compaction

Compaction is the process of removing that reserved space and getting your database down to an appropriate size. The difference between an uncompacted and compacted database can be astronomical.

The database

For reference, let's take a look at this Movies database — you know it. It contains 108K nodes, 399,999 relationships and millions of properties and indexes.

However, take a look at these Goliath figures:

A 64GB Aura instance with a 70% full disk
This is a 64GB database — 70% full.

This is a 64GB database - 70% full.

This is intentionally abnormal.

I had previously run a statement to generate 50 million Pirate Ipsum nodes:

cypher
UNWIND range(1, 50000000) AS i
CALL (i) {
  CREATE (:Filler {
    id: randomUUID(),
    text1: 'Prow scuttle parrel provost Sail ho shrouds spirits boom mizzenmast yardarm.',
    text2: 'Pinnace holystone mizzenmast quarter crow\'s nest nipperkin grog yardarm hempen halter furl.',
    text3: 'Swab barque interloper chantey doubloon starboard grog black jack gangway rutters.'
  })
} IN TRANSACTIONS OF 1000000 ROWS

And then another to delete them all:

cypher
MATCH (n:Filler)
CALL (n) {
  DELETE n
} IN TRANSACTIONS OF 100000 ROWS

Take a look at the store size against the node labels—you'll see the problem:

Store size against node labels
60GiB to store 108k nodes — something's amiss.

Even though we have deleted those 50 million nodes, they're still hanging around in the underworld.

Worry not. We can easily get this down with a little help from Neo4j Desktop.

Compacting walkthrough

Note

Check out this article for a longer, more in-depth walkthrough.

First, take a snapshot in AuraDB:

  • Go to the Instances tab
  • Click the three-dot menu
  • Click Backup & restore
  • Go to the Snapshots tab
  • Click Take snapshot
A snapshot in progress
A snapshot in process

Once your backup is ready, download it.

Backup size

After its jolly excursion, my database .backup ends up being 2.25GB.

A pirate staring at a 2.25GB .backup file
Shiver me timbers! That be a mighty large database yeh've got thar!

Yours may not be as massive, but these overwriting operations can stack up — you might be surprised at the drop in size. For instance, it's not uncommon to go from a 1GB .backup down to a 40MB .dump.

Aura In Production

Run Neo4j Aura in production. Monitor CPU, memory, and query performance, analyze logs, and manage backup and restore for your managed graph database.

Compact the database

To compact the database, you'll need to create a new instance in Neo4j Desktop and load the backup file into it:

The Create Instance dialog in Neo4j Desktop
Creating a new instance in Neo4j Desktop

Start and then stop the database without adding any data to it. In a moment, we'll overwrite it with the .backup data. This gets your neo4j and system databases ready.

Next, import your backup to the new database, and make sure to check Allow overwriting.

The Load database from file dialog with Allow overwriting checked
Loading the backup into the new instance, with Allow overwriting checked

Once it's loaded, make sure you stop the database again — it will likely start automatically. Then:

  • Click the three dots menu
  • Go to Open
  • Open the Instance folder:
The Open menu with the Instance folder option
Opening the instance folder from the instance menu

This should open the correct folder.

The instance folder open in Finder
The instance folder

You'll need to open a terminal at this folder. On Mac, go back one level, right-click the folder and then open a terminal.

Opening a terminal at the instance folder from the Finder context menu
Opening a terminal at the instance folder

Once inside the folder, run the command below.

Warning

This copies the entire store. It needs free disk space for a second copy, and it can take a long time on a large database.

bash
bin/neo4j-admin database copy neo4j neo4jcopy --compact-node-store

You'll see output that looks like this:

Output from the neo4j-admin database copy command
Output from the copy command

At the very end of the output, you'll see a list of constraints and indexes that will need to be reapplied to the new copy.

The constraints and indexes listed at the end of the command output
The constraints and indexes to reapply to the new copy

Copy these to the clipboard.

Next, start your instance:

The Start instance button in Neo4j Desktop
Starting the instance

Inside the query window, run the following:

cypher
CREATE DATABASE neo4jcopy

This command registers the existing database, so you can access it. From here, you can switch to the new database and validate its integrity.

If you're compacting a local database, now would be the time to run those constraints copied from the terminal.

cypher
CREATE RANGE INDEX `user_name` FOR (n:`User`) ON (n.`name`) OPTIONS {};
CREATE RANGE INDEX `actor_name` FOR (n:`Actor`) ON (n.`name`) OPTIONS {};
CREATE RANGE INDEX `director_name` FOR (n:`Director`) ON (n.`name`) OPTIONS {};
CREATE RANGE INDEX `movie_title` FOR (n:`Movie`) ON (n.`title`) OPTIONS {};
CREATE CONSTRAINT `imdbId` FOR (n:`Movie`) REQUIRE (n.`imdbId`) IS UNIQUE OPTIONS {};
CREATE CONSTRAINT `actorId` FOR (n:`Actor`) REQUIRE (n.`imdbId`) IS UNIQUE OPTIONS {};
CREATE CONSTRAINT `userId` FOR (n:`User`) REQUIRE (n.`userId`) IS UNIQUE OPTIONS {};
CREATE CONSTRAINT `genre_name` FOR (n:`Genre`) REQUIRE (n.`genre`) IS UNIQUE OPTIONS {};
CREATE CONSTRAINT `directorId` FOR (n:`Director`) REQUIRE (n.`imdbId`) IS UNIQUE OPTIONS {};

If you're uploading to Aura, skip this step for now and stop the database.

The Stop instance button in Neo4j Desktop
Stopping the instance

Export neo4jcopy to .dump:

The export .dump dialog with neo4jcopy selected
Exporting neo4jcopy to a .dump file

Now, when we take a look at our database size, it has reduced from 2.25GB to 22.9MB.

A pirate admiring a 22.9MB .dump file
Yah'r, a fine database it be an' all. (Firefly)

Next, head back to Aura and overwrite your existing database with the new one.

Warning

Uploading the dump replaces everything in your Aura database, including anything written since you took the snapshot.

The Restore from backup file tab in Aura
Uploading the compacted dump to Aura
The Are you sure confirmation dialog in Aura
Confirming the overwrite in Aura

Now, if we check the database size, we have reduced the node count from 57,108,497 to the correct 108,497. Our total store size has reduced from 59.02GiB to 77MiB.

Store size reduced to 77MiB
Store size reduced to 77MiB

Conclusion

This has been a brief overview of how to reduce your database store size. However, the requirements and processes can differ depending on your Aura version.

To learn more about backups, restores and resource optimization in Aura, take these courses:

Aura In Production

Run Neo4j Aura in production. Monitor CPU, memory, and query performance, analyze logs, and manage backup and restore for your managed graph database.

AuraDB Fundamentals

Learn Neo4j AuraDB, the fully managed graph database service. Create a cloud instance, import data, query with Cypher, and manage backups in Aura.

Comments (0)

Loading comments...