MongoDB to Couchbase for Developers, Part 1: Architecture
In this first installment of a new, detailed series for MongoDB developers to learn Couchbase by comparison, take a look at the architecture of each.
Introduction
With this article on MongoDB to Couchbase comparison, I'm starting a new, detailed series for MongoDB developers to learn Couchbase by comparison. Two years ago, I wrote a single high-level comparative article on MongoDB and Couchbase. I recommend you read that for a bird's view comparison. Both MongoDB and Couchbase have done multiple major releases since then, and developers need a deeper treatment of topics. As developers know, the devil is in the details :-)
This series will be similar to the series for Oracle developers to learn Couchbase by comparison, but for MongoDB developers.
Here are the topics for the planned articles.
- Architecture (This article)
- Database Objects
- Data Types
- Data Modeling
- Statements and Features
- Indexes
- Optimizer
- Search (Full-Text Search)
- Transactions
High-Level Language (MQL-N1QL) Comparison
Developers work on databases using multiple sets of abstraction: data models, APIs, and languages. We refer to RDBMS as SQL databases: SQL is the language. Couchbase has N1QL and MongoDB has MQL. We'll start with that high-level language and feature comparison to get you going. Here's the high-level comparison between MongoDB's MQL model versus Couchbase's N1QL model for objects, language, and query processing. We'll discuss these in detail in the rest of this blog series.
Architecture
MongoDB and Couchbase have many things in common. Both are modern NoSQL distributed databases, use the JSON model, have high-level query languages with support for select-join-project operations, have secondary indexes, have an optimizer that chooses the query plan automatically, and support intra-cluster and inter-cluster replication.
As you'd expect, there are differences. Some are more significant than others. Couchbase is designed to be distributed from the get-go. For example, the data containers' buckets and collections are always distributed. Simply add new nodes, and the system will automatically distribute. Intracluster replication requires no new servers are management: simply set the number of replicas, and you're all set. From the developer interaction perspective, the big difference is the query language itself. MongoDB has a proprietary query language MQL, and Couchbase has N1QL-SQL for JSON. MongoDB uses its B-Tree-based index for the on-prem product and Apache Lucene for the Atlas service; Couchbase has a built-in, lock-free, skiplist-based index and Bleve-based inverted tree index for Full-Text Search (FTS).
Deployment Examples
The deployment configurations and options are explained in the previous overview article. Here are the sample deployments.
Architectural Comparison by Topic
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 deployed 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 (e.g. 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 ->Databases -> Collections -> BSON document -> Fields One MongoDB instance can have multiple databases. |
Cluster -> 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. E.g: “cust.x817.022.4u2” |
Collection | Collection | Collection |
Field | Field | Field All the documents in Couchbase have to be JSON documents conforming to http://json.org/ 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 https://bsonspec.org/ |
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 efficiently. Details at: https://query.couchbase.com 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 http://bit.ly/2vbcbOF |
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 optimistic locking (using CAS) for its simple updates as well as multi-statement transactions. |
Drivers | SDKsvailable in most popular languages: https://api.mongodb.com/ |
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 (e.g. 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 (e.g. Foreign Key Constraint) in Couchbase. |
Resources
MongoDB |
Couchbase |
|
Docs |
||
Forums |
||
Latest Version: Feb, 2022 |
5.x |
7.0.3 |
License |
https://www.mongodb.com/licensing/server-side-public-license/faq |
|
Query Language |
Comments