Apache Geode Best Practices data databases How-tos Tanzu GemFire tutorials

Speed Up Your Node.js Application with a VMware Tanzu GemFire Cache

This post is the first of a multipart series about the Node.js client for VMware Tanzu GemFire. Future posts will cover aspects such as improving app performance, querying data, and performing fast calculations with server-side functions. 

Many developers use Node.js to build scalable network applications, leveraging its asynchronous, event-driven JavaScript runtime. In order to respond to REST requests from remote Web apps, the Node.js apps often need access to information kept in a datastore, typically a SQL database. 

HTTP request/response via REST API

 However, SQL databases have several limitations, especially in cloud environments:  

  • Disk access is slow and latency is high 

  • The number of available connections are limited 

  • As request load increases, databases cannot dynamically scale 

  • If a server goes down, data availability can be impaired 

Using VMware Tanzu GemFire for storing and accessing data can dramatically improve both the performance and resilience of your Node.js apps. Tanzu GemFire is an in-memory, NoSQL, key-value distributed datastore. It delivers high-concurrency, low-latency data access for your applications while also maintaining internal data consistency. With Tanzu GemFire, you will see improvements across a range of factors:   

  • Performance – In-memory data access is typically 100x faster than disk-based lookups 

  • Scaling – You can dynamically add servers to your Tanzu GemFire cluster in response to increased load from remote web apps 

  • Resilience – Tanzu GemFire is a distributed system with configurable redundancy, which means you won’t lose access to your data when a server fails 

  • Consistency – Synchronous replication maintains data integrity across distributed Tanzu GemFire nodes 

The Tanzu GemFire developer website provides steps for getting started using Tanzu GemFire for Node.js developers. The Getting Started guide uses a book-service example app available from this node-examples GitHub repository. And in this post, we will walk through how to add the Tanzu GemFire APIs to your Node.js application.  

Start up a Tanzu GemFire cluster on your local machine 

First, you’ll need to get a Tanzu GemFire cluster started up on your local machine. The Set Up Your Local Environment instructions on the Tanzu GemFire dev site provide a step-by-step guide for how to install both Apache Geode, the open source version of Tanzu GemFire, and the Node.js client library. This includes setting environment variables and running a gfsh script—Tanzu GemFire’s CLI—to start a running Tanzu GemFire cluster. There are also instructions for how to run the book-service app. 

Note: Setting a VCAP_SERVICES environment variable on your local machine allows the application code to be pushed to the VMware Tanzu Application Service platform without modification. 

The Node.js client for the Tanzu GemFire API uses asynchronous functions that return a promise object whenever the response may be delayed due to a network hop. Using async/await will simplify your Node.js code by removing the need for nested callbacks. See this article for more information on the benefits of async/await. 

The book-service example uses a Tanzu GemFire cluster as the database, which holds information about books. In this example, we’re using international standard book numbers (ISBN) as keys for data operations.  

Let the app code handle the REST

The three portions of app code accomplish communication setup and handle REST queries. 

1. Importing dependencies 

 

After installing the Node.js client, the code statement require('gemfire') will include the module containing GemFire APIs. Also included is the Express module, a popular Node.js framework, to structure the app for responding to HTTP requests. 

The VCAP_SERVICES environment variable, which is manually set in your local machine environment and automatically set on Tanzu Application Service, is parsed to extract fields needed to authenticate (e.g., username and password) and connect (e.g., via locator IP addresses) to the Tanzu GemFire cluster. The region variable will be initialized in the init() function after establishing the connection to the Tanzu GemFire cluster. A region in Tanzu GemFire is the equivalent of a table in a SQL database. 

2.  The init() function 

 

The purpose of the init() function is to  

  • Process credentials and SSL certificates. 

  • Create a local cache object and establish the connection pool to the Tanzu GemFire cluster through locators. 

  • Configure the local cache to send and receive data from the region called "test" on the cluster's servers. The PROXY region type means no data is stored locally in the app; instead, all data is stored on the cluster's servers. 

3. The main code processing HTTP requests 

 

Starting at the bottom of this code segment, app.set specifies the port to listen for HTTP  requests. Next, the init() function's execution will establish the connection to the running Tanzu GemFire cluster. Finally, the app listens for requests on the specified port. 

Each app.put and app.get function is designed to respond to an HTTP request. The ISBN number in the book JSON object is used as the unique key for storing and retrieving a book entry in the “test” region. Each HTTP request results in a call to a Tanzu GemFire API region.put(), region.get(), or region.removeAll(). Note that each of these function calls uses  ‘await’ as the Tanzu GemFire functions are implemented as asynchronous functions. 

Get started today! 

To learn more about Tanzu GemFire features and use cases, visit the VMware Tanzu GemFire website. You can also review the Tanzu GemFire Node.js client documentation, and get the software from TanzuNet. We gave an overview of the Node.js client at the virtual ApacheCon @ Home 2020 event, so you can watch the replay of our Node.js session

Also, watch for these upcoming topics in this Node.js series: 

  • Improving app performance when using Tanzu GemFire as a look-aside cache 

  • How to quickly search through data stored in a Tanzu GemFire cluster using OQL 

  • Calling server-side functions from your Node.js app for fast data calculations