Eventing with Couchbase

Reading Time: 3 minutes

In this blog, I am going to discuss Eventing with Couchbase. And to understand eventing, let us first understand what is a mutation? Any changes to the data or metadata in a database are called mutation.

Mutation in Couchbase

The term ‘mutation’ generally refers to document changes happening in the Couchbase cluster. The Couchbase cluster considers create, update, expiry, or delete operations as a data mutation. 

Let us understand by an example: Consider the data cluster of an e-Commerce application. Every action is a mutation like when a customer places a new order, a new transaction record gets created, a new purchase-transaction gets appended with the User account, the stock-inventory of the seller gets updated with an every purchase order. Each of these data mutations qualifies as an event in the cluster.

Why is capturing the happening of mutation value-able?

If put simply “Updating data is usually not the end, but usually progress of a workflow cycle.” So knowing the instance of the occurrence of a mutation can help to trigger a lot of following workflows and this indeed can help in real time server-side processing. So to see it let us take a few examples :

  • Set alerts to a preconfigured threshold breached
  • Monitor specific parameters in a document/json
  • Propagate changes to other systems
  • Enrich a document in real time
  • Cascade deletes to downflow applications
  • Streamlining business workflows

What is Couchbase Eventing?

Eventing Service is natively integrated with the Couchbase Data Platform, Couchbase organically manages the data mutations and does not require any third party solutions. In an event-based architecture, all data changes are reactive and in real-time.

Couchbase Eventing is a highly available, performant, and scalable service which enables user-defined business logic to be triggered in real time on the server when application interactions create changes in data. Eventing makes it easy to develop, deploy, and maintain data-driven business logic from a centralized platform.

eventing-idea
Capture the instance of every mutation

Problems without Eventing?

  • Difficulty capturing changes: Business requirements and workflows change constantly, and developers find it difficult to capture these changes across all business-critical applications in a timely manner.
  • Unreliable execution at scale: It’s difficult to develop an infrastructure that can reliably execute business logic under high-velocity changes in data.
  • Inconsistent business logic: When data-driven business logic is not managed centrally, maintaining consistency across multiple client applications is difficult and especially when those applications are managed by different business owners.

Eventing with Couchbase

Couchbase has ‘Functions’, that offers a computing paradigm that can be used to handle data changes and these Functions allow data handling using Event-Condition-Action model. These Functions offers a platform using which you can focus on building the business logic, then over configuration or infrastructure. Functions are used to handle the event that are generated when a document is created, updated, or deleted. The serverless computing infrastructure gets closer to the datastore as the Couchbase Functions integrates with the Couchbase Data Platform.

Secure, manage, and develop with ease

What to do with the Eventing ‘Functions’?

These functions can be used to various kinds to transformations and operations like :

  • generate threshold-based alerts, trigger an alert upon a threshold breach
  • monitor specific parameters inside a document
  • propagate data changes inside or outside couchbase

Create Eventing Function

Setting up eventing is a simple Five Step process: Start with Addition of a new Function (Import is also possible)

Eventing Tab -> Add Function -> Specify the Source Bucket for Eventing -> Specify Bucket to store metadata -> Provide name to the function

add-new

Provide implementation inside the two javascript methods onUpdate() and onDelete(). For example, added simple implementation, to post the data to some rest endpoint

Provide Implementation and Deploy Eventing Function

impl

Save the function and deploy.

deploy

And now the data on every event shall be posted to the rest endpoint specified in the javascript code.

Note : Eventing is available only with the enterprise version of Couchbase. and Eventing is only supported in Javascript through web-console only

References


knoldus-advt-sticker

Written by 

Pallavi is a Software Consultant, with more than 3 years of experience. She is very dedicated, hardworking and adaptive. She is Technology agnostic and knows languages like Scala and Java. Her areas of interests include microservices, Akka, Kafka, Play, Lagom, Graphql, Couchbase etc. Her hobbies include art & craft and photography.

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!