Use this file to discover all available pages before exploring further.
CockroachDB provides a distributed, PostgreSQL-compatible database that automatically scales and handles multi-region deployments for resilient AI agents.
from praisonaiagents import Agentagent = Agent( name="Distributed Agent", instructions="You are a globally distributed AI assistant.", db={"database_url": "postgresql://user:pass@xxx.cockroachlabs.cloud:26257/db?sslmode=verify-full"})# Data automatically distributed across regionsresult = agent.start("I need high availability and consistency")print(result)
3
Test Global Consistency
# Write from one locationagent.start("Remember: I'm testing global distribution")# Read from anywhere in the world - data is consistentresult = agent.start("What was I testing?")print(result) # "You were testing global distribution"# Same result worldwide due to strong consistency
from praisonai.db.adapter import PraisonAIDBfrom praisonaiagents import Agentdb = PraisonAIDB( database_url="postgresql://user:pass@cluster.cockroachlabs.cloud:26257/mydb?sslmode=verify-full", max_retries=5, # Extra retries for serialization conflicts retry_delay=1.0, # 1 second base delay pool_size=10 # Larger pool for distributed workload)agent = Agent(name="High-Availability Agent", db=db)
import osfrom praisonai import ManagedAgent, LocalManagedConfigfrom praisonai.db.adapter import CockroachDBfrom praisonaiagents import Agent# Create globally distributed agentdb = CockroachDB(database_url=os.environ["COCKROACHDB_URL"])managed = ManagedAgent( provider="local", db=db, config=LocalManagedConfig( model="gpt-4o-mini", name="Global Agent", system="You are a globally distributed AI assistant with strong consistency." ))agent = Agent(name="User", backend=managed)# Agent data automatically distributed across regionsresult1 = agent.run("Store this globally: I'm building a multi-region application")print(f"Agent: {result1}")result2 = agent.run("The app needs to handle users from different continents")print(f"Agent: {result2}")# Save session - data replicated globallysession_data = managed.save_ids()print(f"Global session: {session_data['session_id']}")# Resume from any region - same data everywheremanaged2 = ManagedAgent(provider="local", db=CockroachDB())managed2.resume_session(session_data["session_id"])agent2 = Agent(name="User", backend=managed2)result3 = agent2.run("What application am I building?")print(f"Resumed Agent: {result3}")# Works from any region with strong consistency
-- View data distribution (run in CockroachDB SQL interface)SHOW RANGES FROM TABLE praison_sessions;SHOW RANGES FROM TABLE praison_messages;-- See which nodes have your dataSELECT node_id, count(*) FROM [SHOW RANGES FROM TABLE praison_sessions] GROUP BY node_id;
# Connection string with follower reads enabledfollower_url = "postgresql://user:pass@cluster.cockroachlabs.cloud:26257/db?sslmode=verify-full&options=--cluster=my-cluster"# Some reads may be slightly stale but much fasteragent = Agent( name="Fast Read Agent", instructions="You prioritize read speed with slight staleness acceptable.", db={"database_url": follower_url})
-- Restore to point in time (via CockroachDB Console)RESTORE FROM LATEST IN 'gs://backup-bucket' AS OF SYSTEM TIME '2024-01-15 14:00:00';-- Create scheduled backupsCREATE SCHEDULE FOR BACKUP INTO 'gs://my-backup-bucket' RECURRING '@daily' WITH SCHEDULE OPTIONS first_run = 'now';
CockroachDB uses optimistic concurrency control. Design for retries:
from praisonai.db.adapter import CockroachDB# Tune retry settings for your workloaddb = CockroachDB( max_retries=10, # High-contention: more retries retry_delay=0.05 # Fast retry for short transactions)# Keep agent operations short and idempotentagent = Agent( name="Optimized Agent", instructions="Keep responses concise for fast database transactions.", db=db)
Optimize Connection Pooling
Distributed systems benefit from larger connection pools:
db = CockroachDB( database_url="postgresql://...", pool_size=20, # Larger pool for distributed load max_retries=5)# Multiple agents can share the same pool efficientlyagent1 = Agent(name="Agent 1", db=db)agent2 = Agent(name="Agent 2", db=db)
Monitor Performance
Track key CockroachDB metrics:
Serialization conflicts: High rate indicates need for retry tuning
Node latency: Shows geographic distribution performance
Storage usage: Plan for data growth
Use the CockroachDB Console for monitoring and alerts.
Plan for Multi-Region
Design agent interactions for global distribution:
# Store region information in session metadataagent = Agent( name="Regional Agent", instructions="You serve users globally with consistent data.", db=CockroachDB())# Session metadata can track user regionsession_metadata = {"user_region": "us-east", "timezone": "America/New_York"}
# Use connection string with local region preferenceregional_url = "postgresql://user:pass@us-east-1.cluster.cockroachlabs.cloud:26257/db?sslmode=verify-full"