Couchbase First Impressions (architectural features)
Byte stream for document saving always has a document ID (OBJECT_ID)
High concurrency, high flexibility, high scalability, and good fault tolerance
Document-oriented cluster storage system
Each document with a unique doc ID
Balanced load
Buckets vs VBuckets1. Buckets
Couchbase Storage logic unit is called bucket
Every bucket has a name.
Couchbase a node is currently limited to 10 and below buckets
Buckets have two types of couchbase and memcached
Buckets have an optional password control SASL authentication
Each bucket has a controllable memory quota and replication policy settings that can be monitored and managed independently
2. Vbuckets
Time to Live (TTL)
Can be used to control the expiration time (default is one hour)
Can be used for seesion caching and other functions
Background management expires, automatically delete outdated data, free up memory and hard disk
Data consistency concurrency views, indexs, queriesviews
The concept of views here is completely different from the SQL view concept
Views as a data translation layer, transforming data into a format-specific form of data such as JSON
Views through the Map/reduce function, map the document structure into a table output structure, reduce for counting, statistics and other aggregation functions
Indexes
Whenever views are established, indexes is established, and index updates differ greatly from previous database index updates. For example, there are 1W data, updated 200, the index only need to update 200, without the need to update all the data, Map/reduce function based on index lazy update behavior, greatly benefited.
After establishing views and index can be used to queries
User Use Cases
Replaces the memcached cache layer, providing a compatible memcached protocol interface
Amazon S3 Similar Services
Session
Install, manage, and client interfaces think twice (install initialize) the path to the Datastore
Couchbase data needs to be persisted to hard disk, need to allocate hard disk space and path, on Linux and Windows default is couchbase installation path
Server Quota (memory allocation for service cache tiers)
Couchbase data is cached and the cache memory quota configuration of the service tier is shared by each node. If: Now one node allocates 2G RAM, then after adding three nodes, 6G RAM is added to cache the data, so a total of 8 G RAM is allocated for caching.
Bucket Quota
A cluster can use multiple Bucket,bucket to hold a special set of data or applications, such as a bucket for a session and another bucket to make a copy. Bucket RAM Quota strategy, the server quota setting is a complementary lotus root relationship, to a large extent affect the cache memory quotas.
Installation and Management
Memory Size
Replicas
The default bucket copy is 3. The index replica option is enabled by default, and the Couchbase strategy is more flexible. Backups begin only if the data and nodes are sufficient.
Flush
Disabled or turned on (disabled by default), used to empty buckets of data
Auto-compaction
Persistent data and view,index, long-term fragmentation, auto-defragment hard disk data when the function is turned on
Client interface
Python
> pip install couchbase
Php
$servers = array("192.168.0.72:8091","127.0.0.1:8091");foreach($servers as $server) { $cb = new Couchbase($server, "", "", "default"); if ($cb) { echo "Connected to $server"; break; }}
C
Java
Design and development based on document-oriented Modeljson
Associations between entities
Embedded
{ ‘author‘ : ‘Matin‘, ‘books‘ : [ {‘title‘ : ‘Refactor‘ , ‘IBSN‘ : ‘485-566‘},...]}
External document, referencing its docment ID
{ ‘author‘ : ‘Matin‘, ‘books‘ : [ ‘AD3D63D6-2FE0-11E2-93F9-898688C79046_book‘,...]}
Transaction store operation at the beginning Curd
- Add (ID, document [, expiry]) add an item if ID doesn ' t already exist
- Set (ID, document [, expiry]) Store a document with ID
- Replace (ID, document [, expiry]) Update the document for an existing ID
- CAS (ID, document, check [, expiry]) Update the document for an existing ID providing the check matches
- Get (ID): Get the specified document
- INCR (id [, offset]) Increment The document value by offset
- DECR (id [, offset] decrement the document value by offset
- Append (id, value) append the content to the end of the current document
- Prepend (ID, value) prepend the content to the beginning of the current document
- Delete (ID) Delete the specified document
Document Indentifer
Compared to MongoDB, Couchbase is stored in a single type of key/value,value and does not support arrays. The doc ID is also not created automatically, and couchbase needs to specify a document Indentifer for each docment for storage.
Create identifiers for document:
Use unique fields to make identifiers
If the session, you can use the user's email, username as a unique identifier
Object vs. self-increment policy
Create a single self-increment data (Document indentifer), such as Add (' user ', 0) each time you create a user, you can add ' user ', and then get ' user:1233 ' and so on
Using UUID
Naming conventions and best practices for identifiers
Bucket Policy customization using couchbase (prefix)
Practice list:
1. 网站访问量(PAGE VIW) 若我们需要用couchbase统计网站PV 那么命名如 ‘pv:index‘, ‘pv:blog‘, ‘pv:about‘。2. 用户朋友圈 为用户建立一个朋友圈 apend(‘user#1233‘,‘user#444,user#666,‘)
Time to Live (TTL)
Often used to do cache,session, shopping cart use, verification code and so on. The TTL is expressed by passing in a number, and the couchbase will judge the numbers as follows:
Numbers that are less than30 days (24*30) of one second, such as 600, indicate a 10-minute expiration.
Numbers greater than 30 days (seconds), with a count starting from A.D., such as 1381921696, representing 16th October 2013.
A representation of 0 never expires.
Save data
Both set and add are atomic operations and are safe for multithreaded environments.
Set (DocId, Docdata [, expiry])
The set does not exist when the DOCID data is created, and exists when the docid content is replaced. The expiration setting expiry is optional.
$cb->set(‘message‘, ‘Hello World!‘); $cb->set(‘message‘, ‘Hello World!‘, 10); /*设定过期时限*/ if ($cb->set("spoon", "Hello World!", 10)) { /*操作结果决断*/ echo "Message stored!"; }
Add (DocId, docdata [, expiry])
Unlike set, the update fails if the DOCID content is present.
$cb->add(‘message‘, ‘Hello World!‘); /*下面语句会失败*/ $cb->add(‘message‘, ‘I pushed the button, but nothing happened!‘);
Get Data Get (DOCID)
Single Document return
In PHP, if DocId does not exist, it will return undef:
$message = $cb->get(‘message‘)
Multiple file return (retrieving in Bulk)
Multiple document return implementations differ from one library to another, and multiple document returns are faster than individual returns because of delays in requests and responses. The PHP will return an associative array:
$ret = $cb->getMulti(array(‘recipe1‘,‘recipe2‘));
Update data replace (ID, document [, expiry])
You can use replace to update the data after Set,add:
$message = $cb->get(‘message‘); $message = $message . " How are you today?"; $cb->replace(‘message‘,$message);
Concurrent update CAS (ID, document, check [, expiry])
In a concurrent environment, it is necessary to ensure that concurrent updates
So the effort of a is completely covered by B.
To prevent this, you need to use the CAS feature to do a version check
CAS Yes: Check if the document you want to update is the "original version" (the original that was traded with get).
$value = client->get("customer", NULL, $casvalue); $response = client->cas($casvalue, "customer", "new string value");
The disadvantage of CAS is that data updates are much slower than set, and it is not perfect to do a lot of update transaction concurrency versioning. In addition, if a user uses set or replace operation without CAs, the CAS will be invalidated.
Force lock
Although CAs have some properties of locks, they do not prevent Set,replace from disrupting CAS versions during periods of time without CAS settings. Therefore, when a strong lock is required to use a forced lock, other users can obtain the data, but the document cannot be updated without the corresponding CAs.
$article = $ai->getAndLock(‘樱花盛开‘, &$cas); $article = ‘樱花飘落的速度是每秒五厘米哦‘; # This will fail, because we are not supplying the CAS value $ai->set(‘樱花盛开‘, $article); # This will succeed and then unlock document $ai->set(‘樱花盛开‘, $article, 0, $cas);
The lock can also set the expiration time, and the lock can also be released:
$ai->unlock(‘recipe1‘,$cas);
Art and literature in the same (asynchronous)
Different client interface implementations are different:
/*使用异步*/ $format_recipe = function($key, $value) { return (‘<li>‘ . $value[‘title‘] . ‘</li>‘); }; $ret = $cb->getDelayed(array(‘recipe1‘,‘recipe2‘),0,$format_recipe); /*单独迭代获取*/ $ret = $cb->getDelayed(array(‘recipe1‘,‘recipe2‘),0,$format_recipe); while ($ret = $cb->fetch()) { echo(‘<li>‘ . $ret[value][‘title‘] . ‘</li>‘); }
Server-side update (server-side updates) Increment and decrement
For page access updates, data statistics and other functions:
/*To increment by 1*/ $cb->set(‘counter‘,10); $cb->increment(‘counter‘); /*To increment by 10:*/ $cb->set(‘counter‘,10); $cb->increment(‘counter‘, 10);
Append and Perpend
Append content to the document:
$user->set(‘userlist‘,‘martin,‘); $user->append(‘userlist‘, ‘stuart,‘); $user->append(‘userlist‘, ‘sharon,‘);
Delete data
When the data exists, you can use Delete to delete:
$cb->delete(‘message‘);
Couchbase First Impressions (architectural features)