Introducing Kalix - High-performance microservices and APIs with no operations required. Learn More.
Support
akka-serverless serverless

Developing Serverless Applications - Part 4: Data Access Patterns for Serverless

NOTE

Akka Serverless is now Kalix. Learn more at Kalix.io.

Leveraging Stateful Entities, Stateless Actions, Event Sourcing, CQRS, and CRUD

In part 4 of our series Developing Serverless Applications, we take a look at various data access patterns for serverless.

With Akka Serverless, developers can choose between various stateful and stateless entities depending on their needs–meaning that state models like Event Sourcing, CQRS, and CRUD are all available to you using Entities (a.k.a. Domain Objects), Actions, and Views:

  • Value Entities - Durable in-memory state with a key-value store
  • Event Sourced Entities - Durable in-memory state with an event journal
  • Views - Real-time SQL querying for events and entities
  • Actions - Stateless functions for connecting components

Let’s talk about Stateful Entities

The easiest way to think of an entity is to imagine a shopping cart (real or virtual). The state of the shopping cart that we’re interested in contains information about who is buying, what they’re buying, and how they’re paying, for example.

The shopping cart is the entity and it receives requests–e.g. add socks to this cart. An entity is responsible for receiving and processing commands, making changes to its state, and persisting them in a data store. You can do this with Value Entities and Event Sourced Entities.

Value Entities - Durable In-Memory State with a Key-Value Store

Akka Serverless provides Value Entities, stateful entities that only carry the current state–this follows the familiar key-value store processing flow (often using CRUD). Value Entities do not capture information about past changes or events. Value Entities are useful when it is not necessary to capture a history of state changes when the requirement is the current state at any point in time–e.g. there are three pairs of socks in a shopping cart. Here are a few examples of what Value Entities can be used to do:

  • eCommerce: Add three pairs of socks to the shopping cart
  • IoT: Activate a new IoT device as part of a wireless mesh system
  • Financial Services: Change PIN number

Once an entity with a particular ID has been instantiated, Akka Serverless stores its state and subsequent requests can be satisfied without accessing the data store. After an entity has been idle for a period of time, Akka Serverless passivates it to conserve resources.

While you focus on the code, Akka Serverless handles all of these update processes for Value Entities in the background for you.

Above: While you focus on the code, Akka Serverless handles all of these update processes for Value Entities in the background for you.

Event Sourced Entities - Durable In-Memory State with an Event Journal

The second type of entity provided by Akka Serverless is called an Event Sourced Entity. Event Sourced Entities carry state in-memory and are backed up by a persistent event log. This means you can perform more data-intensive operations because the outcome and history of the entity is saved and accessible whenever needed. Unlike Value Entities, Event Sourced Entities include the accumulation of all events that occurred leading up to the current state–a shopping cart now contains 5 pairs of socks, but had previously contained three pairs, and two were added. Here are some data-centric activities that can be performed with Event Sourced Entities:

  • eCommerce: Apply a 10% discount to all socks if more than 5 pairs are added to cart
  • IoT: Record the time when an IoT device becomes unresponsive and then the time that it becomes responsive again
  • Financial Services: Record the number of secure account access attempts that collectively indicate a possible security breach attempt

While you focus on the code, Akka Serverless manages all the state changes for Event Sourced Entities in the background for you.

Above: While you focus on the code, Akka Serverless manages all the state changes for Event Sourced Entities in the background for you.

Like with Value Entities, once an entity with a particular ID has been instantiated, Akka Serverless stores its state in a durable event log and subsequent requests can be satisfied without accessing the data store. After an entity has been idle for a period of time, Akka Serverless passivates it to conserve resources.

Views - Real-Time SQL Querying for Events and Entities

Akka Serverless provides Views, which lets developers query domain data (i.e. the entities), using SQL–removing the limitations of only being able to query it by key/index. Views act as handlers for the events processed by entities–they are the results of either snapshots of Value Entities or the full event history of Event Sourced Entities.

The benefit of views is that data contained in otherwise unfriendly formats can be processed in various ways depending on which data you need for what purpose, and how fast. Views are designed to receive entity state changes or messages received from subscribing to topics on a broker and process them in different ways.

One common way for views to process entity state changes is to project them into SQL. This lets us dig into data in more familiar, business-driven ways that you cannot get directly from keys, indexes, event logs or entity snapshots.

Here are some examples of what you can do with Views:

  • eCommerce: Calculate revenue from sales of black socks, in the last 3 months, in France
  • IoT: Investigate serial number ranges for faulty wireless mesh devices to track to original production line
  • Financial Services: List all ATM locations with failed PIN attempts in the last 48 hours

Actions - Stateless Functions for Connecting Components

Akka Serverless supports the same types of functions that other stateless platforms use. Actions are stateless functions that can perform different functions, like transforming requests and responses between an action's client and their downstream components.

Actions receive incoming requests from various sources, such as HTTP and gRPC service calls, topics, or event messages from the Value Entities and Event Sourced Entities mentioned above.

When forwarding requests to components, the action receives replies that are transformed. The transformed reply is then replied from the action. When emitting requests to other components, there is no reply from the component. Therefore the action replies as defined in the action service definition protobuf.

Here are some examples of what you can do with Actions:

  • eCommerce: Forward entity state changes to external services
  • IoT: Convert a forwarded HTTP/gRPC call from another component
  • Financial Services: Transform incoming events from a message bus, such as transform order submitted event messages to credit validation commands

Get started with Akka Serverless

This brings us to the end of our series Developing Serverless Applications. As we've done our best to make clear, trying Akka Serverless is simple and fast, and you can choose between different learning paths to get started:

SCHEDULE A DEMO

 

NOTE

Akka Serverless is now Kalix. Learn more at Kalix.io.

Share

The Total Economic Impact™
Of Lightbend Akka Platform

  • 139% ROI
  • 50% to 75% faster time-to-market
  • 20x increase in developer throughput
  • <6 months Akka Platform pays for itself