Memcached, Redis, MongoDB

Source: Internet
Author: User
Tags memcached

Memcached:
Memcache is an ordinary program, this program process first request a memory, pre-control the memory space to partition,
Each partition is divided into blocks of up to 1M, but in the same partition, the block length is fixed, and when the data comes through the socket
The socket has a listening port, sends a string to the port, parses the string if it is an insert operation, calculates the data.
Find the right length for yourself and insert it in
Why split blocks? Resolving memory fragmentation issues

When the client comes up with a key/value data, how does it allocate the data and put the data on which server machine?
1, first key a hashing algorithm
2, according to the hash value of the number of servers to take the remainder, according to the results of the remainder of the selected storage current Key-value value of the server
3, if the addition of a machine, the client to take the data at the time of the key to take the remainder may have problems, the original data may be stored on the second server, the new
It is possible to fetch data from a third machine after adding the machine, and the third machine does not have this data, how to solve this problem?
This problem can not be solved directly, only to minimize the impact, there is a consistent ha new algorithm

With memcached cache If there is not enough memory, when you want to expand this distributed cluster, how a
Way to solve the problem of this data?
Answer: consistent hashing algorithm


Redis:redis is a key-value storage system. Similar to memcached, it supports storing more value types, including string (string), list (linked list), set (set), Zset (sorted set-ordered collection), and HASHS (hash type). Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and Master-slave (Master-Slave) synchronization is implemented on this basis.

Memcache How to resolve data conflicts (concurrent access, simultaneous modification of files):
CAS protocol: Data comes up with a version of itself that allows the last person to get data from the server to modify
1. Cannot be persisted as storage
2, storage of data restrictions, up to 1M, more than 1M artificial segmentation
3, the stored data can only be Key-value
4, the cluster data does not have replication and synchronization mechanism (one machine hangs, the other machine data will be gone)
5, memory recovery is not timely LRU algorithm: Memory not used-"expired memory-" the least Recently used memory
Memchae differs from Redis:
1, the cluster is not the same
MM: Clustering via client Drive
Redis: Cluster implementation via server-side configuration
2. Redis can be persisted
1/Data File recovery data Rdb file
2/Recover Data via Log
3. Redis provides an advanced data structure. Support for queues, stacks, etc.
4, Redis is single-threaded. Processing data in small cases is not much different, processing big data mm
Higher performance, MM is multi-threaded

The updating mechanism design problem of the cache system.
The following is a workable solution (assuming our update mechanism is to update the cache every 5 minutes):
1) design two cache pools, recorded as a, B, and the contents of a and B are data obtained from the backend server database. Normally, the client's request is to get the cached content from cache Pool A, and a global variable ref is used to record the number of clients that are currently accessing cache A, to a client request to add a ref value of 1, and a client request after one of the clients requests is ref minus one.
2) when the cache update time is up, if ref is not 0, then we cannot update the cache directly because a client is fetching data from cache Pool A. Here, we can learn from the rehash idea of Redis, update the time, we will be the client access to the B buffer pool, at this time, the cache Pool A no longer accept new client data requests, A's ref variable only reduced, when the ref variable reduced to 0 o'clock, we can update the content of the a cache pool.

MongoDB is a set (collection)-oriented, schema-free document-type non-relational database, its advantage is that the query function is more powerful, can store massive data.
MongoDB does not support transactions, can execute arbitrary JavaScript functions on the server side, support JavaScript expression query, built-in automatic shard mechanism (Support level database cluster)
The so-called "set-oriented" (collection-oriented), meaning that data is grouped in a dataset, is called a collection (Collection).
Each collection has a unique identity name in the database and can contain an unlimited number of documents


MongoDB:
1, reading and writing performance is very high, adapt to big data [itself this storage structure, for the collection of storage]]
Columns are OK, no constraints, just one column, most operations are done in memory
Redis: Write to disk without writing a single piece of data, write to disk when more than one threshold
MongoDB: Writes are also written in memory, write requests are immediately returned, do not care if there is no write disk,
In Db. Users.getlasherror () to see if there is a write success, read also quickly: Using space-time technology,
Map the disk data into memory, and take up the memory of the lower priority, the official website said

2. Low learning cost, easy to get started, simple to start, easy to configure, can solve problems in our project very quickly

3, document-based database, no mode: Data structure is not pre-defined

4, can support some of the complex query

Why do I need a nosql non-relational database?

High performance-demand for highly concurrent reads and writes to the database
Huge Storage-The need for efficient storage and access to massive amounts of data
High Scalability && High availability-requirements for database scalability and availability

Why use NoSQL instead of a traditional database?
1, traditional database high concurrent access easy to cause deadlock
2, building a cluster is very simple, multi-client can share the cache
3, read and write performance is very good, 1s can read 1w times, write 10w times
4. Can realize master-slave data synchronization between server and multiple clients

Memcached, Redis, MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.