NoSQL DB: Cassandra, MongoDB, Redis

1. What are NoSql Databases?

非關聯DB, 水平擴充, persist semi or unstructured data 和 flexible schemas.

2. Features of NoSql databases

2.1 Multiple Models Support

NoSql databases are ideal for persisting, managing, and accessing semi-structured and unstructured data.

2.2 Open Source

Most of the NoSql databases are open sourced. Most of the cloud providers are offering these databases as managed service by managing auto scaling, patch updates etc. behind-the-scenes.

2.3 Scalability

NoSql databases are horizontally scalable by adding more servers to the cluster and the clusters can be spread across multiple geographic locations(regions) unlike relational databases where it can be scaled vertically.

2.4 Low Latency

Due to data replication to multiple nodes on database cluster the latency is low. The tradeoff between latency and consistency is important with web and mobile software applications. Regardless of the replication method employed, there will be a tradeoff between consistency and latency.

2.5 Flexible Schema

NoSql databases support flexible data model with eventual consistency and are inherently schema-free. This allows NoSql databases suitable for storing semi-structured and unstructured data efficiently.

2.6 Highly Performant, Available and Fault Tolerant

In NoSql databases, the data is replicated to multiple nodes in the cluster and also to nodes in clusters that are in other regions. This trait makes NoSql databases highly available and fault tolerant. No Sql databases are highly optimized for document, key-value, column -family, graph etc. data models and their access patterns that give higher performance.

3. Cassandra

  • 分散式水平擴充
  • 高可靠
  • 容錯
  • wide column NoSql database
    • 三維結構(行,列,時間), schema-free, 寬行, 多版本數據以及生命週期管理
    • 關聯模型: 二維(行,列)以及固定的schema
  • 所有節點都是peer, 不是master slave paradigm
  • 寫入速度快
    • 因為不用搜尋任何東西, 直接就寫入
    • The data is first written to the commit log and then the reflection of this data to the table is taken care by cassandra algorithm. In cassandra, model your data model around queries

4. MongoDB

  • 跨平台
  • 文件導向(半結構話資料 semistructured data)
  • 高可靠
  • It works on collections and documents and provides high availability through replica sets.
  • 使用JSON like document
  • schema less, you don’t need to create document structure before creating documents.

5. Redis (Remote Dictionary Server)

  • scalable data store
  • be used as a database, cache, and also as a message broker.
  • in-memory data store, 可保留資料到硬碟

6. Cassandra vs MongoDB vs Redis

  • Cassandra stores data in Column-Family structure, whereas MongoDB stores data in JSON document format.

    • column family,就像關聯式資料庫的一個資料表一樣
  • In Cassandra secondary indexes are not recommended as they degrade the performance. In MongoDB, indexes are preferred to avoid searching all document to find the requested document and for better performance.

    • Cassandre 次要的index 不建議使用, 會降低效能
    • MongoDB index 用來避免全文收尋, 可提高效能
  • Cassandra is a great choice for high write throughput, but if your application needs very high read concurrency, use MongoDB.

    • Cassandra 適合用於高寫入量
    • MongoDB 適合高併發讀取
  • Cassandra has no master node, all the nodes are peers, whereas in MongoDB, there is a single master.
    • Cassandra 每個node都是平等 沒有master node
    • MongoDB有一個master
  • Cassandra replicates the written data eventually to the number of nodes specified in replication factor within the cluster and also nodes on cluster in different region. MongoDB requires some setup to do the replication. You can setup secondary database, which can be auto-elected if primary database goes down. In MongoDB the reads are first committed to the primary first and then replicated to secondary replicas.
    • Cassandra 會複製所有節點
    • MongoDB 需要一些設定去備份,自動選擇一個
  • In these databases, you can set TTL (Time to Live) on each record, so the record can be evicted automatically after the expiration of TTL.
    • 可以設定TTL , record會自動回收當TTL過期
  • Redis is a key-value data store and is very efficient to use as a cache for improving application performance.
    • Redis 是一個key value儲存, 適合當作cache使用
  • Scaling Cassandra and Mongo is much simpler than Redis.
    • 擴充 Cassandra跟 MongoDB 比Redis簡單
  • In Redis, the size of the data store cannot exceed the total memory space on the system, i.e RAM plus swap space. There are no intrinsic limits on the size of a Mongo database.
    • Redis data儲存空間不能超越系統的memory.
    • MongoDB 沒有此限制
  • Cassandra, MongoDB and Redis databases can be clustered for high availability, backup and for increasing the overall size of the datastore.
  • If your application needs aggregation, use MongoDB. If your application needs key-value temporary storage, use Redis. If your application needs easily scalable high write throughput wide column storage, use Cassandra.
    • aggregate framework主要是建立在聚合管道(pipeline)基礎下,而這管道就是可以一連串的處理事件,以下列範例中你可以想成管道中有四節,『將每篇文章作者與like數抓取出來』為第一節,然後它處理完會產生資料,會再丟給第二節[依作者進行分類],直到最後產生結果。

7. When to use which NoSql database?

Following are some of the use cases where different NoSql databases fits best and give better performance.

Choose Cassandra for following use cases:

  • Linearly scalable, Highly available, fault tolerant
  • Multi data center deployment
  • Very high write throughput but a smaller number of reads.
  • You want to have a quite responsive reporting system on top of stored data
  • Real time data analytics
  • Your application doesn’t need ACID properties from DB
  • Your application needs integration with Hadoop, HBase, Spark

Choose MongoDB for following use cases:

  • Scalability on the fly
  • Document based storage
  • Very high read concurrency
  • Caching for real time analytics
  • Content management
  • Write payload is high, i.e document size is high (up to 16MB)
  • Very useful in rapid prototyping
  • Good for storing large texts, Videos, images, media files etc.

Choose Redis DB for following use cases:

  • To cache larger payload to increase application’s performance
  • Persistent cache to disk and needs to be recovered after restart
  • Key-value pairs store
  • Need very high performance
  • Temporary data storage such as user sessions
  • Can be used as a messaging queue using its Pub/Sub model

8. Cassandra vs Mongo vs Redis DB – Summary

Understanding of different NoSql databases is critical for choosing the right database for your application needs. Pick the right NoSql database based on your application use cases. NoSql databases are not a good choice if your data has too many relations and need ACID properties. To improve application performance,

use Redis as a cache as it has in-memory storage.

Use MongoDB for content management and document type of storage needs.

Use Cassandra for highly available, wide column storage cases.

Choosing the right database has direct impact on the application’s performance.

Reference

© Kimi Tsai all right reserved.            Updated : 2023-07-12 09:04:53

results matching ""

    No results matching ""

    results matching ""

      No results matching ""