Now, this is not the end. It is not even the beginning of the end. But it is, perhaps, the end of the beginning. — Winston Churchill

Updating data is usually not the end, but usually a progress of a workflow.  Shipping follows ordering; inventory update follows shipping; adjusting credit follows returning;  The next step in the process is required to act to keep the workflow going. The workflows can be simple with few steps or complex with hundreds of steps.  Business Process Management (BPM) is an industry by itself.

Couchbase 5.5 introduced Evening service. Developers can write a Javascript function to execute upon a change to the data. We refer inserts, Updates, Merges, and Deletions together as mutations. Multiple specific use cases have been documented for developing these eventing functions.

JSON data model in Couchbase came from JavaScript.  N1QL is SQL for JSON. The eventing functions are written in Javascript and has integrated N1QL. Using the Eventing functions, writing procedural business logic with instant access to data easy.

Here’s the lifecycle of the functions from a developer perspective. For every mutation, you can define any number of these functions to be executed. It’s the developer responsibility to size the execution times of the functions, depending on the number of mutations.

Here’s the lifecycle of the Eventing functions:

 

For every insert, update or delete (direct or expiry), you can execute one or more javascript functions.  These functions can read the new data and type of action and then execute the subsequent action. This functionality is well described in Couchbase blogs, and articles.

Statement Type Eventing Function Invoked
SELECT None
INSERT OnUpdate().  The function is invoked once per document inserted. Simple insert inserts a single document. Inserts can have multiple documents using multiple documents in the VALUES clause or can insert multiple documents via INSERT INTO…SELECT statement.
UPDATE OnUpdate() is invoked once per document updated, except when multiple updates on the same document is deduped into a single update. Update statement can update multiple documents.
UPSERT OnUpdate().  The behavior is similar to INSERT.
DELETE OnDelete(). Invoked once per documented deleted.
MERGE OnUpdate() and/or OnDelete() depending on the insert, update, and delete actions.
CREATE INDEX, DROP INDEX, EXPLAIN, PREPARE, GRANT, REVOKE No eventing function is invoked.
EXECUTE Depends on the type of the statement executed.

These functions can also execute N1QL statements.  Rest of the article looks at all aspects of N1QL executed in Eventing Functions.

N1QL Statements in Eventing functions.

Statement Use cases for N1QL statements in Eventing Functions
CREATE INDEX Since the schema is flexible, you could potentially inspect the data often/periodically to detect new fields and then create indexes on it.
DELETE Cascade deletes.
DROP INDEX A corollary to the CREATE INDEX use case.
INFER Periodic introspection of the bucket for the structure.  Then take action (validate, create index, update the data model) if necessary.
INSERT Maintaining referential data (eventually).

Updating other documents with references to this data.  E.g Data from a new zip, state, etc.

Data copy (fully or partially) to secondary/tertiary documents. Similar to post-trigger action.

MERGE Keep the secondary data in sync.
SELECT Fetch any data, run any reports periodically, etc.

Check for various things like data quality, data validity,

When you do know the target document key, use the built-in direct document references. See examples at: https://docs.couchbase.com/server/5.5/eventing/eventing-examples.html

UPSERT Keeping secondary/tertiary data in sync.

Similar to post-trigger action.

UPDATE Keeping secondary/tertiary data in sync.

Similar to post-trigger action.

Examples:  Let’s try some eventing functions with N1QL.

  1. Simple N1QL in Functions for logging and cascade delete.

Save away every deleted document in a separate bucket.

  1. OnUpdate() function to keep the aggregate information ready periodically.

References:

  1. Couchbase documentation: https://docs.couchbase.com/server/5.5/eventing/eventing-overview.html
  2. Couchbase blogs on eventing: https://www.couchbase.com/blog/tag/eventing/

Author

Posted by Keshav Murthy

Keshav Murthy is a Vice President at Couchbase R&D. Previously, he was at MapR, IBM, Informix, Sybase, with more than 20 years of experience in database design & development. He lead the SQL and NoSQL R&D team at IBM Informix. He has received two President's Club awards at Couchbase, two Outstanding Technical Achievement Awards at IBM. Keshav has a bachelor's degree in Computer Science and Engineering from the University of Mysore, India, holds ten US patents and has three US patents pending.

Leave a reply