When implementing a decision service, you may require state. But what is state and what is a stateful decision service?
Sometimes a decision service needs to consider information or context from previous invocations of the service.For example, you might want to award a customer discount if they purchased more than two items in one week. The information you keep about each invocation is called the state, and a decision service that uses state in its business logic is stateful.
This article discusses three ways you can build stateful decision services with ODM:
- Build stateful rules with Decision Server Insights (DSI)
- Build a stateful decision service with ODM and a tightly coupled database
- Build a stateful decision service with ODM on Cloud and a loosely coupled database
This article will focus on ODM on Cloud and decision services with a loosely coupled database.
Identifying Stateful Rules
When designing a decision service, an important consideration is to determine whether it requires state.Â If decisions are made purely on input data, it is stateless. Take the following stateless rule:
if Â Â Â the customer name is "Jon" then Â Â Â set the discount of 'the customer' to 10%
The decision is based on the customer name.Â There is no historical data and the rule is stateless.Â Now consider this:
definitions Â Â Â set 'recent purchases' to the purchase history of 'the customer' Â Â Â Â Â Â Â Â Â Â Â where each purchase was made in the last week if Â Â Â the number of purchases in 'recent purchases' is more than 2 then Â Â Â set the discount of 'the customer' to 10%
This rule applies a discount only if the customer has made more than two previous purchases in the last week.Â The rule looks at the purchase history which makes it stateful.
Three Ways to Build Stateful Rule Applications
If your decision service requires state, you must decide how to implement it.Â Consider the following:
- The data throughput and performance of your stateful rules
- The amount of state to be stored
- Whether you are developing on premise, or on public or private cloud
This will narrow down your choice to three:
- DSI is your first choice if the solution requires high performance event processing. For example, DSI could be used for monitoring the state of engines.Â This application would require dozens of sensors to process hundreds of events per second from the engine.Â However, DSI is a powerful platform and it takes time to build and deploy applications. If your design goal is to create simple stateful decision services in short timescales you may wish to consider the next two options.
- Stateful XOM. It is possible to build stateful decision service with a XOM layered on a database.Â The XOM fetches the context during each rule invocation.Â Consider this approach if you are not using DSI and your rules require large amounts of contextual data.Â Â Use this approach with caution as you will need to add database query code to the XOM which makes it hard to debug.Â Also, be aware that accessing an external database from the XOM is incompatible with ODM on Cloud.
- Stateful Facade. Layer a faÃ§ade over a database and a stateless decision service to implement a stateful decision service.Â Consider this approach if you are storing a small amount of state and you are developing on ODM on Cloud.
We will now discuss the Stateful Facade design pattern.
The Stateful FaÃ§ade Design Pattern
The design pattern for creating a stateful faÃ§ade over a stateless decision service is shown below:
The key component of this design is the Stateful Controller.Â The Controller extracts state from a database and passes it to a stateless decision service.Â The Controller may also write state back to the database.Â The sequence of events is shown below:
- The client application requests a stateful decision via an API.
- The Stateful Controller extracts state associated to the request from the database.
- A stateless decision service is invoked with state passed in the input.
- The decision service executes business rules and passes back updated state in the output
- The updated state is stored in the database
- The Stateful Controller passes the response to the client
Now that you understand the concepts behind stateful decision services, you might be interested in the tutorial on GitHub.Â The tutorial takes you through building a stateful decision service using the Stateful FaÃ§ade Design Pattern.Â The tutorial uses free trials of ODM on Cloud, Cloudant and Node-JS.
The steps for the tutorial are described in Tutorial.pdf.