Building Data-Centric Apps that Scale

Carolin Stanley
Freshworks Developer Platform Blog
4 min readOct 15, 2019
Photo by Tobias Fischer on Unsplash.

With the rise of data transactions that happen across the web these days, business applications (apps) now need to deal with increased data procurement, irrespective of its data type. As a result, we require operations that perform advanced data storage, access, and retrieval in a faster and accurate manner.

The Freshworks developer platform provides a key-value database with flexible schema to securely store data. In this post, we’ll walk you through recent data storage operations that are featured in our Advanced Data Storage app. This app demonstrates how to set aliases for links for a specific time period (a URL shortener) and also track the number of times an alias is modified.

Time to Live (TTL)

In certain cases, data, such as API keys, logs, and so on, may have a specific shelf life after which it needs to be purged from the data store. With TTL, you can fix this time period as part of the SET operation and the data automatically expires in the specified time.

TTL could be used in the following cases:

  • Caching data — To improve the performance of your app when making API requests, you may need to cache information for reuse within a specific time period. With TTL, the information is automatically deleted in the specified time.
  • Recording user activities — Apps that must track activity data on a daily basis, for example, will store user activities for the specified time period. This data is then replaced with new information the next day.

Note: If you do not set the TTL value or if the value is negative, the user key will exist forever.

In the given sample app, the addNewAlias method allows an agent to add new aliases for links and stores them in the data store. The expiration time (ttl) is specified in client.db.set() in the createAlias method.

Adding new aliases in the data store.

SetIf

You can use the “setIf” feature to determine if a specific record exists and overwrite the value for a key. When an app runs concurrently either in multiple user contexts in the front-end or in multiple event contexts in a serverless app, set operations to the data store can suffer from race conditions. “setIf” allows your app to avoid race conditions by applying an additional check before the operation completes.

The setIf feature could be used in the following example: when an agent wants to link a JIRA issue to a Helpdesk ticket, “setIf: not_exist” checks if the issue is already linked and if not proceeds to update the ticket.

In the given sample app, the createAlias method stores a key-value pair using the options returned from the constructOptions method. If the setIf condition fails, an error is thrown which is handled by triggering the alertParent method and data is sent to the modal’s parent.

Storing a key-value pair.

Update

You can now modify and manipulate existing records without having to first retrieve them and then saving the records after updating them in memory. The following actions are supported in the Update operation:

The update operation could be used in the following use cases:

  • Append — An app could maintain a list of update activities for a ticket by appending the agent-id and timestamp into a list mapped to the ticket-id every time an agent updates a ticket.
  • Increment — In a counting app, you can increment the variable that stores the counter value each time the specified condition occurs.

In the given sample app, we used the “set” parameter to update the key-value pair in the data store. Also, using the “increment” parameter, the “updates” value is incremented by one every time an alias is updated.

Increasing the updates value each time an alias is updated.

In this post, we discussed advanced data storage operations that will help you build powerful data-centric apps with seamless scalability in an easy and efficient manner. The app showcased in this post is available in the GitHub repository for your reference. Go ahead and customize these code snippets for the use-cases you are currently dealing with.

Please reach out to us at marketplace@freshworks.com if you have any questions or suggestions.

Thanks to Raviraj Subramanian and Satwik Hebbar.

--

--