Couchbase

From G1Joshi/Agent-Skills · View interactive version

Couchbase is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Couchbase NoSQL assistance covering N1QL/SQL++ queries, memory-first caching, document scopes, and cross-datacenter replication (XDCR). Use when building ultra-low latency JSON document platforms.

How to use this skill

Skill file

Couchbase

Couchbase works as a Key-Value store (managed memory cache) + Document Database. It is famous for its "memory-first" architecture and N1QL (SQL for JSON).

When to Use

Quick Start

-- N1QL Query
SELECT u.name, ARRAY_AGG(o.item) as orders
FROM `travel-sample`.inventory.users u
JOIN `travel-sample`.inventory.orders o ON u.id = o.user_id
WHERE u.city = "Paris"
GROUP BY u.name;

Core Concepts

Memory-First VBucket Architecture

Documents are mapped across 1024 virtual buckets (vBuckets), cached directly in RAM before being asynchronously written to disk:

[ Application Client ] ──Sub-Millisecond Read/Write──→ [ Managed Memory Cache (RAM) ]
                                                                 │ (Async Flush)
                                                                 ▼
                                                        [ Append-Only Disk Storage ]

SQL++ (N1QL) Declarative JSON Queries

Executes full SQL expressions over schemaless JSON structures:

-- Query nested JSON orders in Couchbase SQL++
SELECT
  meta().id AS orderId,
  customer.email,
  ARRAY_SUM(items[*].price) AS totalAmount
FROM `commerce`.`orders` AS o
WHERE status = 'PAID'
  AND ANY item IN items SATISFIES item.category = 'Electronics' END
ORDER BY totalAmount DESC
LIMIT 20;

Scopes and Collections Multi-Tenancy

Organizes documents into logical namespaces mimicking relational databases:

[ Bucket: enterprise ]
  ├── [ Scope: billing ]
  │   ├── [ Collection: invoices ]
  │   └── [ Collection: payments ]
  └── [ Scope: catalog ]
      └── [ Collection: products ]

Common Patterns

SQL++ (N1QL) Secondary Indexing and Querying

Problem: Ad-hoc JSON document filtering results in slow primary index table scans.

Solution:

Build covering secondary GSI indexes for high-throughput queries:

CREATE INDEX idx_orders_status ON `ecommerce`.`sales`.`orders` (customerId, orderStatus, orderDate)
WHERE orderStatus != 'cancelled';

SELECT customerId, orderDate, total
FROM `ecommerce`.`sales`.`orders`
WHERE customerId = "cust_987" AND orderStatus = "completed"
ORDER BY orderDate DESC;

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :---------------------------------- | :------------------------------------------------------------------- | :------------------------------------------------------------------------------------ |

| No index available for query | Missing secondary index matching the WHERE predicate in SQL++ query. | Create an index covering query filter and projection columns. |

| Temporary failure / Out of memory | Couchbase bucket memory quota exhausted by high item count. | Increase bucket RAM quota or configure eviction policy (value-only vs full eviction). |

| DocumentExistsException | Key already exists in bucket during insert operation. | Use upsert instead of insert if overwrite semantics are intended. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.