couchbase chennai meetup: developing with couchbase- made easy
TRANSCRIPT
Developing with Couchbase – Made Easy
Karthik Babu SekarSolutionsArchitect,WorldWideTechnicalFieldOperations@Couchbase
- LinkedIn - https://www.linkedin.com/in/karthikbabusekar- Twitter- https://twitter.com/SKBhere
- E-mail – [email protected]
Agility to adapt to changes in demand
Responsive, context-aware customer experiences
Multi-channel, multi-device engagement
Ability to support a large and growing number of users
What businesses need to succeed in this new world
The Rise of the System of Engagement Database
8©2017 Couchbase Inc.
System of Record
1980’s
CRM ERP ECM
System of Record
1995
CACHE
CRM ERP ECM
NoSQLSystem of Record
2005
CACHE
CRM ERP ECM
TODAY
NoSQLSystem of
Engagement
MOBILEQUERY
INDEX
SEARCH REPLICATION
ANALYTICS
DATA CACHE
Database requirements to power engaging experiences
9
Adaptable. Flexible schema
Responsive. Consistent
High-Performance
Scale as needed. Able to handle any demand (operations, queries, data)
Secure.Device to cloud,
auditable, pluggable
Global.Data locality,
Geo-distributed, Multi-Data Center
Always on. Fault Tolerant. Able to continue operations
under failure conditions
Mobile experience. Offline, sync’d,
secure, low latency, personalized
Intelligent.Full Text Search, Real
Time Analytics, Big Data enabled
©2017 Couchbase Inc.
Couchbase Data Platform
Unified Administration
Unified Programming Interface
Data Query Index SearchMobileReplication Analytics
{N1QL}
The System of Engagement Database
Developing with Couchbase
Couchbase supports a wide range of frameworks, languages, platforms and infrastructure choices.
Frameworks Languages Platforms Infrastructure
MOBILE MOBILE
©2017 Couchbase Inc. 12
Languages and Interfaces for Couchbase
§ Official SDKs– Java– .NET– Node.js– Python
§ For each of these we have– Full Document support– Interoperability– Common Programming Model
Others: Erlang, Perl, TCL, Clojure, Scala
- PHP- C- Go- Ruby
JDBC and ODBC
©2017 Couchbase Inc. 14
Service-Based Architecture
§ Multi-Dimensional Scalability (MDS) – Optionally Scale each service independently:– Data– Index– Query
MDS is the architecture that enables independent
scaling of data, query, and indexing workloads while
being managed as one cluster.
©2017 Couchbase Inc. 15
Full Cluster Architecture
15
STORAGE
Couchbase Server 1
SHARD7
SHARD9
SHARD5
SHARDSHARDSHARD
Managed Cache
Cluster Manager
Cluster Manager
Managed Cache
Storage
Data Service
Index Service
Query Service STORAGE
Couchbase Server 2
SHARD7
SHARD9
SHARD5
SHARDSHARDSHARD
Managed Cache
Cluster Manager
Cluster Manager
Managed Cache
Storage
Data Service
Index Service
Query Service STORAGE
Couchbase Server 3
SHARD7
SHARD9
SHARD5
SHARDSHARDSHARD
Managed Cache
Cluster Manager
Cluster Manager
Managed Cache
Storage
Data Service
Index Service
Query Service STORAGE
Couchbase Server 4
SHARD7
SHARD9
SHARD5
SHARDSHARDSHARD
Managed Cache
Cluster Manager
Cluster Manager
Managed Cache
Storage
Data Service
Index Service
Query Service STORAGE
Couchbase Server 5
SHARD7
SHARD9
SHARD5
SHARDSHARDSHARD
Managed Cache
Cluster Manager
Cluster Manager
Managed Cache
Storage
Data Service
Index Service
Query Service STORAGE
Couchbase Server 6
SHARD7
SHARD9
SHARD5
SHARDSHARDSHARD
Managed Cache
Cluster Manager
Cluster Manager
Managed Cache
Storage
Data Service
Index Service
Query Service
©2017 Couchbase Inc. 16
Couchbase Data Access
• Everything is built on top of Key Value
• A Document store is a special case of Key-Value
• Views provide aggregation and real-time analytics through incremental map-reduce
• Global Secondary Indexes provide low latency/high throughput indexes
• N1QL is a language that provides a powerful and expressive way of accessing documents
©2017 Couchbase Inc. 18
Couchbase SDKs
§ What does it mean to be a Couchbase SDK?
Cluster
Bucket
CRUD
FunctionalGive the application developer a concurrent API for basic (k-v) or document management
APIget()insert()replace()upsert()remove()
©2017 Couchbase Inc. 19
The Document
§ Documents are integral to the SDKs.§ All SDKs support JSON format§ In addition: Serialized objects, Unquoted Strings, Binary pass-through§ A Document contains:
19
Property Description
ID The bucket-unique identifier
Content The value that is stored
Expiry An expiration time
CAS Check-and-Set identifier
©2017 Couchbase Inc. 20
Bucket API – Modifying Documents
§ insert() the document if it does not exist§ replace() the document if it does exist§ upsert() the document (insert or replace)§ remove() the document
§ append() data to the document§ prepend() data to the document
§ counter() for increment/decrement type operations
©2017 Couchbase Inc. 21
Bucket API – Retrieving Documents
§ get() the document§ getFromReplica() if the active is not available
§ getAndTouch() to load the document and reset the expiry§ getAndLock() to load the document and write-lock it
©2017 Couchbase Inc. 22
Unified API – DML, CAS Example1. Two Clients retrieve the same document "XYZ"2. Client A retrieves it first.3. Client B then retrieves XYZ. Both clients will have the same CAS value for document XYZ4. Client B tries to perform an update to document XYZ. The update succeeds as the CAS value
was unchanged from when Client B initially retrieved the document. Once the update succeeds, the CAS value for XYZ changes.
5. Client A then tries to perform an update on XYZ immediately after Client B. The update will fail as Client A's CAS value is out of date. When Client B updated XYZ, the CAS value changed.
©2014 Couchbase, Inc.
©2017 Couchbase Inc. 23
Durability Requirements – Mutations
§ By default, a write operation is successful when the data has been written to the
memory of the node with the active vBucket.
– Eventually (but pretty quickly), the data will also be written to persistent storage and to replica
vBuckets
§ persistTo – write operation is successful when data has been written to persistent
storage
– Can be set to require writes to persistent storage for replicas, too
– Using persistTo will slow performance
§ replicateTo – write operation is successful when data has been written to both the
active vBucket and replica vBuckets
– If more than one replica, can set the number of replicas that must be written for success
©2017 Couchbase Inc. 24
Bucket API – Querying
§ query() is possible for§ Views§ N1QL
§ Streams N response rows as they arrive from the server§ Loads more than one Document based on Criteria§ Typically used to satisfy secondary and advanced querying use cases
©2017 Couchbase Inc. 25©2016 Couchbase Inc. 25
Bootstrapping your application - Service List
25
• Dynamic Distributed Services
• Dynamic Configuration Updates – No additional
work from the developer
• Fault Tolerant/Durable Connectivity (we are paranoid)
Client Connectivity Characteristics:
©2017 Couchbase Inc. 26
Key/Value
§ Fastest way to access & mutate documents§ Access by Document ID only§ Includes subdocument manipulation§ Efficient binary protocol
Latency Flexibility
©2017 Couchbase Inc. 27
Key/Value – API DML Methods (Create/Delete/Update)
• insert-Insert a document or binary key/value. Fails if the item exists. • upsert-Stores a document or binary key/value to the bucket, or updates if a
document exists. • replace-Replaces a document or binary key/value in a bucket. Fails if the
item doesn’t exist. • remove-Deletes an item from the bucket. Fails if the item doesn’t exist• append/prepend-Appends or prepends in place the value of a binary k/v
item. Does NOT work with documents • touch-Updates the ttl of a documet.• getAndTouch-Retrieves a document or binary key/value and updates the
expiry of the item at the same time.• counter-Increments or decrements a key's numeric value.
©2017 Couchbase Inc. 28
Key/Value – API DML Methods (Retrieval)
• get-Retrieves a document or binary key/value.• getAndLock-Lock the document or binary key/value on the server and
retrieve it. When an document is locked, its CAS changes and subsequent operations on the document (without providing the current CAS) will fail until the lock is no longer held.
• getReplica-Get a document binary key/value from a replica server in your cluster.
• unlock-Unlock a previously locked document or binary key/value on within a bucket.
©2017 Couchbase Inc. 29
Key/Value – Durability Methods
§ By default, a write operation is successful when the data has been written to the
memory of the node with the active vBucket.
– Eventually (but pretty quickly), the data will also be written to persistent storage and to replica vBuckets
persistTo – A write operation is
successful when data has been
written to persistent storage.• Can be set to require writes to persistent
storage for replicas, too
• Using persistTo will slow performance
replicateTo – A write operation is
successful when data has been written
to both the active vBucket and replica
vBuckets.• If more than one replica, can set the number of
replicas that must be written to for success
©2017 Couchbase Inc. 30
Key/Value - Sub-Document Operations
§ Document Mutations:• Atomic Operate on individual
fields• Identical syntax behavior to
regular bucket methods (upsert, insert, get, replace)
• Support for JSON fragments.• Support for Arrays with
uniqueness guarantees and ordinal placement (front/back)
©2017 Couchbase Inc. 31
Views
§ Distributed Map-Reduce§ Great for Aggregations (fast)§ Spatial Support
Latency Flexibility
©2017 Couchbase Inc. 32
N1QL
§ Very flexible, but get your index right§ Global Index instead of Map/Reduce§ Similar to SQL
Latency Flexibility
©2017 Couchbase Inc. 33
Full Text Search
§ Very Flexible, but more concepts to learn initially§ Inverted Index§ Still Developer Preview§ Similar to Elasticsearch
Latency Flexibility
©2017 Couchbase Inc. 34
Show me some code ...
ü Create connection
ü Open bucket
ü Create JSON object
ü Create doc from object, assign key, and "upsert"• if exists, update• if not, insert
ü Get the document by key, and display
// Connect to localhostCluster cluster = CouchbaseCluster.create();
// Open a bucket connection
Bucket bucket = cluster.openBucket("customers");
// Create a document
JsonObject user = JsonObject.empty().put("first", "Walter")
.put("last", "White")
.put("job", "chemistry teacher")
.put("age", 50);
// Store a document
JsonDocument stored =bucket.upsert(JsonDocument.create("walter", user));
// Get the documentJsonDocument walter = bucket.get("walter");
System.out.println("Found: " + walter.getString(“first”));("first"));
§ Connect, create, and retrieve a document (Java)