Comprehensive Rules for selecting, designing, and operating relational & NoSQL databases in large-scale enterprise applications.
You're building systems that need to handle millions of users, process thousands of transactions per second, and maintain 99.99% uptime. Yet you're still debating whether MongoDB is "web scale" or if Oracle is worth the licensing cost.
Here's the reality: Wrong database choices cost enterprises an average of $3.2M annually in performance bottlenecks, scaling issues, and technical debt. Meanwhile, companies using polyglot persistence architectures report 40% faster feature delivery and 60% fewer production incidents.
Most enterprise teams make database decisions based on familiarity rather than workload requirements. You end up forcing OLTP workloads into analytics engines, cramming time-series data into relational tables, or building complex JOIN operations on document stores.
Common Pain Points:
These Cursor Rules implement a workload-first database selection framework that eliminates guesswork and accelerates decision-making. Instead of religious database debates, you get concrete patterns for matching databases to workloads.
Instant Decision Framework: Clear criteria for Oracle vs. PostgreSQL vs. MongoDB vs. Spanner based on your specific requirements—not vendor marketing.
Production-Ready Architecture Patterns: Battle-tested configurations for multi-region setups, automated failover, and zero-downtime migrations that actually work at enterprise scale.
Operational Excellence: Automated monitoring, schema migrations, and disaster recovery patterns that prevent 3 AM pages.
Before: 2-3 months of POCs and vendor meetings
After: Decision matrix completed in 2 weeks with clear technical justification
-- Rules automatically suggest the right engine for each workload
-- OLTP: PostgreSQL with read replicas
-- Analytics: Snowflake with auto-scaling
-- Session Store: Redis with persistence
-- Search: Elasticsearch with proper indexing
Time Saved: 15+ hours/month previously spent on database-related production issues
The rules include circuit breaker patterns, connection pooling configurations, and automated retry logic that prevent cascading failures:
-- Built-in circuit breaker prevents cascade failures
RETRY_ATTEMPTS = 3
BACKOFF_MAX = 30000 -- 30 seconds cap
CIRCUIT_OPEN_THRESHOLD = 5 -- failures before opening
Cost Impact: 25-40% reduction in database licensing through strategic multi-cloud deployment
Rules enforce cloud-agnostic patterns while leveraging each provider's strengths:
Challenge: Building a B2B platform serving 500+ enterprise clients with strict data isolation requirements.
Implementation:
-- Row-level security with tenant isolation
CREATE POLICY tenant_isolation ON customer_data
FOR ALL TO app_user
USING (tenant_id = current_setting('app.current_tenant')::uuid);
-- Automatic partitioning by tenant for large tables
CREATE TABLE order_items (
id SERIAL PRIMARY KEY,
tenant_id UUID NOT NULL,
order_id BIGINT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT now()
) PARTITION BY HASH (tenant_id);
Result: Zero data leakage incidents, 60% faster query performance through proper partitioning.
Challenge: Processing 100K+ events/second for real-time dashboards while maintaining OLTP performance.
Solution Pattern:
# Polyglot architecture following the rules
primary_oltp: PostgreSQL (Aurora)
analytics_store: Snowflake
real_time_cache: Redis
search_engine: Elasticsearch
time_series: InfluxDB
Outcome: 200x faster analytics queries, eliminated impact on transactional workloads.
Challenge: Serving customers across 15 countries with <100ms response times and strong consistency for payments.
Architecture Decision:
-- Google Spanner for globally consistent transactions
-- Regional read replicas for catalog data
-- Redis for session management per region
-- Example: Global inventory with local reads
SELECT inventory_count
FROM product_inventory
WHERE product_id = $1
AND region = $2;
Impact: 70% latency reduction, 99.99% payment success rate maintained globally.
# Add to your Cursor Rules (.cursorrules file)
curl -o .cursorrules https://raw.githubusercontent.com/your-repo/enterprise-db-rules/main/.cursorrules
The rules include a decision matrix that analyzes your requirements:
-- Rules prompt you through workload analysis
-- Consistency requirements: Strong/Eventual/None
-- Scale pattern: Read-heavy/Write-heavy/Balanced
-- Data structure: Relational/Document/Key-value/Graph
-- Geographic distribution: Single-region/Multi-region/Global
Based on your inputs, get specific technology recommendations:
# Generated architecture for your workload
primary_database:
engine: PostgreSQL
deployment: AWS Aurora
scaling: Read replicas + connection pooling
analytics:
engine: Snowflake
integration: Change data capture via Debezium
caching:
engine: Redis
pattern: Write-through for hot data
The rules include Terraform templates and CI/CD pipelines:
# Auto-generated Terraform for your stack
module "primary_database" {
source = "./modules/aurora-postgresql"
instance_class = "db.r6g.2xlarge"
auto_scaling_enabled = true
backup_retention_period = 7
multi_az = true
}
Productivity Gains:
Cost Optimization:
Risk Mitigation:
Ready to transform your enterprise database architecture? These Cursor Rules eliminate months of trial-and-error, giving you battle-tested patterns that scale from startup to Fortune 500. Your future self will thank you for making the right database decisions today.
You are an expert in Enterprise-grade relational & NoSQL databases (Oracle, PostgreSQL, SQL Server, MongoDB, Amazon Aurora, Google Cloud Spanner, Azure Cosmos DB) and the surrounding cloud tooling (Snowflake, Databricks, Datadog, New Relic, Prometheus).
Key Principles
- Choose the database engine per workload (polyglot persistence). One size never fits all.
- Design for horizontal scale first; avoid vertical scaling dependency.
- Prefer managed, cloud-native offerings to minimise ops toil and gain elastic capacity.
- Assume geo-distribution: co-locate data with users to reduce latency.
- Security is non-negotiable: encrypt everywhere, adopt Zero-Trust, enforce MFA.
- Automate everything: provisioning (IaC), schema migration (CI/CD), monitoring, alerting, incident response.
- Optimise for failure: build with multi-AZ/region, automated backups, PITR, and chaos testing.
- Favour declarative, version-controlled schema definition (migrations) over ad-hoc scripts.
SQL & NoSQL Rules
- Naming
• snake_case for tables/collections; singular nouns (customer, order_item).
• Prefix link tables with "xref_" (e.g., xref_user_role).
• Index names: idx_<table>_<columns>_asc|desc.
- Schema
• Use surrogate integer/UUID PKs; avoid natural keys that change.
• Always specify explicit column types & constraints (NOT NULL, CHECK, FK). Never rely on implicit behaviour.
• Partition large tables by time or tenant; keep partitions <100 GB.
- Queries
• Use parameterised queries or prepared statements; never interpolate strings.
• Prefer set-based SQL; avoid RBAR (row-by-agonising-row) loops.
• Write idempotent migration scripts; wrap in transactions where supported.
- Indexing
• Create composite indexes in column order of filtering then sorting.
• Drop unused indexes quarterly (use pg_stat_user_indexes / DMVs / $collStats).
- Transactions & Isolation
• Default to READ COMMITTED; elevate only when required.
• Break long transactions into smaller, retry-friendly units.
- NoSQL (MongoDB/Cosmos)
• Embed until 16 MB doc limit, then reference.
• Always include a monotonically increasing shard key for range queries.
• Use schema validation rules (JSON Schema) to catch drift early.
Error Handling & Validation
- Detect & retry transient errors (deadlock, network blip, 40001) with exponential back-off capped at 30 s.
- Abort immediately on logical errors (duplicate key, FK violation) and bubble meaningful codes up the stack.
- Log full SQL text and bindings only in redact-friendly, PCI-compliant stores.
- Implement circuit-breaker around every DB call; open after 5 consecutive failures, half-open after 30 s.
Framework-Specific Rules
Oracle
- Use Automatic Storage Management (ASM) and Data Guard for HA.
- Enable Adaptive Query Optimizer, but pin critical plans with SQL Plan Baselines.
PostgreSQL / Amazon Aurora-Postgres
- Keep autovacuum thresholds tuned: avg_dead_tuples < 20% table rows.
- Use logical replication for zero-downtime major upgrades.
- Store secrets in aws_secretsmanager_credential_store, not plain TEXT.
SQL Server
- Default to contained DBs for portability.
- Always enable Query Store and automatic index management.
MongoDB / MongoDB Atlas
- Shard collections once they exceed 10 GB or sustain >1000 ops/s.
- Use majority writeConcern and "retryable writes" on every driver call.
Google Cloud Spanner
- Model tables with interleaved hierarchy for locality.
- Limit mutations to 20 000 rows per transaction to avoid aborts.
Azure Cosmos DB
- Choose partition key with >10 000 distinct values to maximise distribution.
- Enable automatic fail-over across at least three regions.
Additional Sections
Security
- Encrypt at rest with KMS/HSM; rotate data-keys annually.
- Force TLS 1.2+ in transit; reject downgrades.
- Apply RBAC + ABAC: roles are coarse (reader, writer), attributes fine-grained (tenant_id).
- Enable row-level security (Postgres RLS / SQL Server RLS) for multi-tenant apps.
Performance & Observability
- Collect slow query logs <1 s threshold; stream to Datadog / New Relic.
- Expose Prometheus metrics: latency p95, p99, active connections, buffer cache hit ratio.
- Run quarterly load tests using production-sized datasets; capture max CPU <70%, disk queue <10.
Testing
- Spin up ephemeral DB containers per CI pipeline (docker-compose / Testcontainers).
- Apply migration scripts, seed data, then run integration tests.
- Verify rollback works by injecting fault and asserting data integrity post-test.
Disaster Recovery
- RPO ≤5 min, RTO ≤30 min as baseline.
- Automate cross-region backups nightly; validate restore monthly.
Documentation & Governance
- Maintain ERD and collection schema in repo; auto-publish to Confluence on merge.
- Attach data classification tags (Public / Internal / Confidential / Restricted) to every table.
Example Folder Structure
```
infra/terraform/ # DB clusters, VPC, subnets
schemas/
├─ core/ # baseline DDL
├─ migrations/ # versioned YYYYMMDDHHMM.sql
└─ seed/ # reference data
src/
└─ data-access/ # parameterised queries / ORM models
observability/
└─ dashboards/ # Grafana JSON exports
```