Skip to main content
Apache Cassandra is a highly scalable, distributed NoSQL database designed for handling large amounts of data across many commodity servers with no single point of failure. It supports vector storage for semantic search capabilities in AI applications and can scale to massive datasets with linear performance improvements.

Usage

import os
from mem0 import Memory

os.environ["OPENAI_API_KEY"] = "sk-xx"

config = {
    "vector_store": {
        "provider": "cassandra",
        "config": {
            "contact_points": ["127.0.0.1"],
            "port": 9042,
            "username": "cassandra",
            "password": "cassandra",
            "keyspace": "mem0",
            "collection_name": "memories",
        }
    }
}

m = Memory.from_config(config)
messages = [
    {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
    {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
    {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
    {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
m.add(messages, user_id="alice", metadata={"category": "movies"})

Using DataStax Astra DB

For managed Cassandra with DataStax Astra DB:
config = {
    "vector_store": {
        "provider": "cassandra",
        "config": {
            "contact_points": ["dummy"],  # Not used with secure connect bundle
            "username": "token",
            "password": "AstraCS:...",  # Your Astra DB application token
            "keyspace": "mem0",
            "collection_name": "memories",
            "secure_connect_bundle": "/path/to/secure-connect-bundle.zip"
        }
    }
}
When using DataStax Astra DB, provide the secure connect bundle path. The contact_points parameter is ignored when a secure connect bundle is provided.

Config

Here are the parameters available for configuring Apache Cassandra:
ParameterDescriptionDefault Value
contact_pointsList of contact point IP addressesRequired
portCassandra port9042
usernameDatabase usernameNone
passwordDatabase passwordNone
keyspaceKeyspace name"mem0"
collection_nameTable name for storing vectors"memories"
embedding_model_dimsDimensions of embedding vectors1536
secure_connect_bundlePath to Astra DB secure connect bundleNone
protocol_versionCQL protocol version4
load_balancing_policyCustom load balancing policyNone

Setup

Option 1: Local Cassandra Setup using Docker:

# Pull and run Cassandra container
docker run --name mem0-cassandra \
    -p 9042:9042 \
    -e CASSANDRA_CLUSTER_NAME="Mem0Cluster" \
    -d cassandra:latest

# Wait for Cassandra to start (may take 1-2 minutes)
docker exec -it mem0-cassandra cqlsh

# Create keyspace
CREATE KEYSPACE IF NOT EXISTS mem0
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};

Option 2: DataStax Astra DB (Managed Cloud):

  1. Sign up at DataStax Astra
  2. Create a new database
  3. Download the secure connect bundle
  4. Generate an application token
For production deployments, use DataStax Astra DB for fully managed Cassandra with automatic scaling, backups, and security.

Option 3: Install Cassandra Locally:

Ubuntu/Debian:
# Add Apache Cassandra repository
echo "deb https://downloads.apache.org/cassandra/debian 40x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
curl https://downloads.apache.org/cassandra/KEYS | sudo apt-key add -

# Install Cassandra
sudo apt-get update
sudo apt-get install cassandra

# Start Cassandra
sudo systemctl start cassandra

# Verify installation
nodetool status
macOS:
# Using Homebrew
brew install cassandra

# Start Cassandra
brew services start cassandra

# Connect to CQL shell
cqlsh

Python Client Installation

Install the required Python package:
pip install cassandra-driver

Performance Considerations

  • Replication Factor: For production, use replication factor of at least 3
  • Consistency Level: Balance between consistency and performance (QUORUM recommended)
  • Partitioning: Cassandra automatically distributes data across nodes
  • Scaling: Add nodes to linearly increase capacity and performance

Advanced Configuration

from cassandra.policies import DCAwareRoundRobinPolicy

config = {
    "vector_store": {
        "provider": "cassandra",
        "config": {
            "contact_points": ["node1.example.com", "node2.example.com", "node3.example.com"],
            "port": 9042,
            "username": "mem0_user",
            "password": "secure_password",
            "keyspace": "mem0_prod",
            "collection_name": "memories",
            "protocol_version": 4,
            "load_balancing_policy": DCAwareRoundRobinPolicy(local_dc='DC1')
        }
    }
}
For production use, configure appropriate replication strategies and consistency levels based on your availability and consistency requirements.
I