MongoDB to Couchbase for Developers: Architecture

Topic MongoDB Couchbase Deployment MongoDB was designed to handle humongous data while keeping the developer experience simple. Its query language MQL has the look and feel of a JavaScript API. Laptop single node deployment is as easy as starting up the mongod process and simply starting using databases and collections: No CREATE required! The system will automatically create the databases and collections you reference!

Single node deployments were enormously popular prior to MongoDB Atlas. Multi-node deployments require config servers and more mongod nodes. Additional coordinator piece of software (mongos) needs to be run on the application layer to manage the query processing and result management for distributed query processing. Once you add new nodes, you need to explicitly shard the collections.

The APP for server communication uses proprietary MongoDB protocol. Higher-level SDKs work on top of this.

Couchbase Server can be installed on a single server or a single node or a VM/container for development: for example macOS, Linux, or Windows. All of the database functions are abstracted into services (data, index, query, search, analytics) which work in concert to provide the database functionality. These services coordinate to expose the services via REST API. The applications use Couchbase SDKs or tools to create indexes, run N1QL queries, get results and visualize data.

This configuration is used mainly for development on your laptop, cloud, VM, or container. Single node deployment is rarely for production. In fact, Couchbase gives you a warning when you’ve on a single node there’s only one copy of your data! High availability is built into the core.

The API for server communication is via REST API and Memcached binary protocol. All SDKs use REST or Memcached protocols underneath.

Languages mql. It has the basic operational support for CRUD. aggregate() API supports extensive pipeline building as well as search(in MongoDB Atlas only). For anything more than simple get and set, aggregation is very useful. N1QL (SQL for JSON), Javascript, Python functions (with N1QL), Direct data access API, FTS (full-text search). Connections A persistent connection between the user process and database process/thread running on behalf of the client application. HTTP over TCP/IP connection.
Connection for Memcached. For each request for data fetch, and modification, the query is a REST call. Session A session starts from the initial connection to the disconnect. Each application “session” has a corresponding session on the MongoDB mongos or mongod. Within a session, you can execute 1 query or one million queries. Just like connection, in Couchbase, each request is a distinct request. Each request has to carry both bind parameters and any context parameters (eg timeout). All the N1QL statements for a single transaction execute on the same query node; concurrent transactions can execute on any number of nodes. User, authentication See here. Supports OS user, a database user. Authentication can be done by OS, database, third party like LDAP, Kerberos. Database MongoDB instance
-> Collections
-> BSON document
-> Fields

One MongoDB instance can have multiple databases.
Each database can have multiple collections. Each collection can have multiple partitions and indexes.

-> Buckets
-> Scopes
-> Collections
-> JSON documents
-> Fields
One Couchbase instance can have up to 30 BUCKETS. Each bucket can have many scopes, each scope many collections. Within each scope, you can also create functions (in SQL, Javascript, Python).

Each JSON document will have a user-provided unique document key.

Eg: “cust.x817.022.4u2”

Collection Collection Collection Field Field Field
All the documents in Couchbase have to be JSON documents conforming to
Consider a simple 1-level JSON document.
Document Key: “cust:2984”
Doc: {“a”:1, “b”: True, “c”: “Hello”}
In your mind map, you can think of this document as a row, individual attributes, “a”, “b”, “c” as columns, the document key as a primary key.
This document contains simple scalar values.JSON can contain arrays and objects, arrays of objects, objects containing arrays. Attribute names are referenced from top-down. JSON support BSON: Binary JSON JSON is the model for the whole document SDKs MongoDB SDKs: here Couchbase SDKs: here Multi-node architecture
(Homogeneous deployment). Multi-Dimensional Deployment Each node in MongoDB is identical to the other doing data, index, and query processing. There’s one exception: Mongo introduced compute-only nodes, called analytic nodes for running larger computations.

Each node in the cluster can have one or more combinations of the services: Data, Query, Index, Search, Analytics, and eventing. The cluster manager (co-ordinator) is aware of the distribution and availability of the services and will inform the services about each other.

The SDK is also aware of the data distribution, query, and FTS nodes. SDK tries to balance the workload among different query nodes for query workload, data nodes for data/KV workload, FTS nodes for search workload.

System Architecture MongoDB uses a hybrid shared-nothing architecture. By default, each collection is in a single node. Its location is virtualized by the query processing by mongod and mongos. Collections can be manually sharded by hash or range. All the respective indexes stay local to the data nodes (mongod). For query processing, each mongod node does partial query processing and the mongos merges the results, applies any sort and pagination clauses before returning the results to the user. Uses shared-nothing architecture from the ground up enabling Couchbase to scale out. Each node can scale up to use the resources in each node; database functionality is implemented as co-operating multiple services running on a single system. Coordination is done via message passing, even on a single system.

These services (data, index, query, search, analytics, and eventing) lend themselves to scale out seamlessly. You can have all of the services in each node and simply add new nodes with the same services. All the services will understand the multi-node topology. This makes Couchbase elastic.

Applications don’t use all of the services uniformly. The bottlenecks could be on data, indexer of the query. So, in Couchbase, your size and add new nodes simply to run the bottlenecked services. This will provide optimal resource utilization and better performance compared to a homogeneous deployment. It’ll give you flexibility and reduce your cost. The multi-dimensional scaling is as easy to deploy as any other configuration and manageability remains the same.

Query MQL: MongoDB Query language.
Supports the usual equality, range predicates, and projection of columns. The aggregate() API has enhanced the query expressiveness for the query by adding grouping, aggregation, search, and pipelining. See more here. N1QL (SQL for JSON) provides a declarative language that’s an enhanced version of SQL for JSON. It comes with indexes and a full-fledged query engine, patented cost-based optimizer, transaction support, UDF (user-defined functions – aka stored procedures) to execute queries efficiencies. Details at:
The same language is supported for the operational workload (OLTP) and analytical workload (OLAP), just like RDBMS. Developers also get a simple GET and SET API for each document in a collection.

Couchbase also supports built-in SQL and JavaScript functions in the query service to support the equivalent of PL/SQL functionality.

Couchbase FTS (Full-Text Search) helps you create text index and search. This is fully integrated with N1QL

High Availability MongoDB uses a replica set (set of servers continuously replicating the data from the primary mongod nodes to provide high availability. Within a single cluster, you can have multiple copies of data and index just by specifying the number of copies you’d like to have. You can have up to 3 copies of the data and any number of index copies. No additional setup is required.

Across-multiple clusters, you can replicate the data with built-in XDCR (cross-data center replication).

Transactions Multi-statement, ACID with
multiple isolation levels Supports multi-statement ACID transactions with READ-COMMITTED isolation level. Since these are distributed transactions, it comes with configurable durability as well. Couchbase consistently uses optimism locking (using CAS) for its simple updates as well as multi-statement transactions. Drivers SDKsvailable in most popular languages: Couchbase SDK (Java, .NET, LINQ, PHP, Python, Go), Simba JDBC/ODBC, JDBC driver for tableau Data Model Denormalized BSON Denormalized (aggregated) JSON model.
Relationships between multiple types of documents (eg Orders to Customers) can be represented and processed. Either the child (orders) or the parent (customer) can store the primary key of the related documents and then JOIN them. This relationship is implied, not enforced by a constraint (eg Foreign Key Constraint) in Couchbase.


Leave a Comment