Reactive Rules For Database Logic
Reactive Rules For Database Logic
Executive Summary
Introduction
Since the advent of relational databases, enterprises have been devising methods for extracting data in
a useful fashion for application development. However, traditional data-driven app dev has been relatively
unchanged since the first application of object-oriented functional programming to these systems. Even
with the recent popularity of API-driven interfaces, much of the development burden has involved
back-end effort.
Creating application back-ends involves interacting with databases to retrieve data through getters
and setters that map to SQL statements on single tables, writing more complex SQL to do manual JOINs
across database tables, and defining database triggers or client-side logic to manage relationships
between fields and maintain data consistency. While some progress has been made using code generation
techniques to automate single-table API creation, the results are complex, hard to maintain and
inadequate for real-world use cases that require application logic.
At the same time, the need for agile app development in the Application Economy has made the ability to
rapidly deliver robust data-driven APIs more important than ever. These initiatives are also being driven by
Lines of Business rather than IT organizations, meaning the API Owners are likely to be more familiar with a
spreadsheet than with server-side programming languages. The way we build application back-ends, interact
with data sources and define logic needs to change to keep up with these ever-changing requirements.
This white paper takes a fresh look at defining application logic and interacting with databases using
Reactive Logic rather than traditional functional code-based approaches. It explores the concept of reactive
programming and how this paradigm can be applied to declarative rules for maintaining relationships across
data tables. It demonstrates the power of Reactive Logic to simplify database interaction and logic definition
for business users while maintaining robust performance and optimization characteristics. And it shows how
CA Technologies will forever change the way you look at application creation.
3 | WHITE PAPER: REACTIVE RULES FOR DATABASE LOGIC ca.com
This is an abstract definition that can be adapted to various disciplines as described below. Regardless, the
system needs to establish watches on the referenced values and react when changes are observed. The watch
mechanism may be automatic or manual wherein you must explicitly invoke operations to establish watches.
Parent rolewe use the foreign key constraint name for references to the one side
(as in purchaseorder.customer).
Child rolethe table name is for the many side (as in purchaseorder.lineitems).
Actual support provides customized child role names but that is beyond the scope of this document.
4. Recalculate multi-table.
The system must recalculate the referencing data when the watched data changes. Note that changes must
be interpreted broadly: not just updates but also inserts and deletes. The real power occurs when you consider
multi-table derivations. For true declarative/reactive support, you should not need to code SQL commands
to access the datathis should be fully automated. So, deleting a purchaseorder should adjust the balance.
5. Automate SQL.
SQLs required for multi-table recalculation should be automated.
Pruningno SQL should occur if the watched data is not altered (e.g., changing an orders date
should not require access to the customer).
AdjustmentSQLs are expensive but aggregate SQLs are even more so. Accordingly, recalculations
of aggregates (e.g., sums) should be done with one row updates.
Observe that aggregates can nest. So, a simple non-optimized transaction to pay an order would
require a select sum of all the orders and items. Optimized adjustment logic can reduce these SQLs
by orders of magnitude.
This is very important since it provides the basis for real-time analytics, such as real-time
accounting (instant posting of new transactions) and real-time medical records.
Cachingan order transaction with several line items should issue one adjustment to the order,
not one per item.
Architecture
While this paper is primarily focused on the concepts of reactive database Logic, its only interesting
if it actually runs. It can do so in a number of ways:
Database SupportNative database support could be provided though DDL extensions with
corresponding runtime support. Many advantages but a long process to add to the SQL standard
and it would still not provide natural support for remote data.
Code Generation of Triggers and Stored ProceduresThis is, after all, what triggers do, so it would
clearly be possible to generate these. This provides excellent active enforcement but does not deal
well with integrating multiple databases.
App Serverstraditional app servers also enforce business logic with support for integrating
multiple databases. The language-specific interfaces, however, are not architecture independent
and the solution would be burdened by a significant coding overhead and low re-use.
API ServersA more recent approach is to build API servers that provide REST-based interfaces
around these reactive logic rules, typically with JavaScript support for accessing multiple databases
and external services.
Consider the use case, place order, with the requirement that a Customers balance may not exceed their
credit limit. The cocktail napkin requirements are illustrated above. The following logic, which looks
rather like a requirements document, should be fully executable:
Validation Logic: Validations are expressions that must be satisfied to commit transactions (else an
exception is thrown). Far beyond simple single-attribute validations, the requirement is multi-attribute
validations, such as this Customer Validation: balance < credit_limit.
7 | WHITE PAPER: REACTIVE RULES FOR DATABASE LOGIC ca.com
Derivation Logic: Validations are the most familiar form of business logic but derivations and events
are the most valuable for dependency management and re-use, as noted below. Derivations here include
multi-table derivation rules such as in this rule which defines the balance as the sum of the unpaid
order totals: balance is sum(purchaseorder.amount_total where paid = false).
Derivations are conceptually similar to spreadsheet cell formulas. Note that: Derivations are multi-table,
as in the example above. Derivations can chain, both to validations and other derivations:
Purchaseorder.amount_total = sum(lineitem.amount)
Lineitem.amount = qty_ordered * product_price
Lineitem.product_price = copy(product.price)
Chaining, ordering and dependencies are automatically handled (further described below).
Derivations can include if/else logic, state transition logic and reference related data.
Derivations are highly optimized to eliminate/optimize SQLs. For example, changing an orders date does
not retrieve the customer and paying an order adjusts the customer with a one row updatenot an
aggregate query.
Declaring Logic
You declare the logic in a browser-based IDE as shown below. Dialogs are provided to make it simple
to define (point and click) and the system produces this documentation automatically.
8 | WHITE PAPER: REACTIVE RULES FOR DATABASE LOGIC ca.com
Reactive in Action
Dependency management means that when the client submits some altered data, the server detects
all changes and adjusts any dependent dataincluding related tables. This incurs ordering and requires
optimization. The diagram below illustrates the server processing for the change in line item quantity
(represented by the green circle at the bottom):
The same processing is automatically applied to all use cases, as described below
in the reactive reuse section.
Reactive Agility
By the time you boil off change detection and change propagation and SQL, you have burned off all
the noise. What is revealed is the pure, crystallized logic. As it turns out, this noise is most of the
code of a traditional program. The five lines of reactive rules require literally 500 lines of imperative
code. This proportion holdsin fact improvesas system size increases. You are operating at a two
orders of magnitude higher level of abstraction.
9 | WHITE PAPER: REACTIVE RULES FOR DATABASE LOGIC ca.com
Active Integrity
Imperative code runs when you call it. Code for integrity may exist but it is of no value unless its called
in every required circumstance. So, verifying compliance entails checking all the paths of all the existing
code. By contrast, you dont call reactive logic. You just state it. It is the systems job to react as required
in all required circumstances. So, if you want to verify compliance, just scan the logic. If its there, you
can be certain it is being applied.
Reactive Reuse
The customer.balance rule, perhaps declared for place order, simply states that its value is the sum of the
unpaid orders. This design intent is encapsulated into purchaseorder and then automatically re-used over
all related transactions. So, the balance is increased if an order is added, decreased when it is deleted or
paid and so forth. Put another way, logic is associated with your tables, not with a specific transaction,
request type or method. This is what enables logic re-use. So our logic above, perhaps conceived for place
order, is automatically re-used for all these related transactions:
This design one/solve many applies to maintenance as well; when you change your logic, the system
automatically reflects it in all the relevant transactions. Reactive objects factor logic from the methods
(still clumsy imperative code) into declarative reactive expressions:
Maintenance
Imperative programming is distinguished by orderingyour code must be executed in a correct order.
The pain of this is most evident in the archaeology of maintenance, where most of the time is spent
studying existing code to determine where to insert a few new lines. This entire process is eliminated
by automatic ordering. You just alter the logic and the system computes a new execution order
(and optimization strategy) based on the new dependencies.
10 | WHITE PAPER: REACTIVE RULES FOR DATABASE LOGIC ca.com
Automatic Documentation
As shown in the earlier screenshot, this logic is virtually a direct entry of the cocktail napkin spec.
So logic is not only executable; it is transparent documentation that enables business users and
IT to partner and find missing/incorrect elements.
Reactive Performance
The optimizations discussed abovepruning, caching and adjustmentare applied to every maintenance
change. So, unlike conventional systems that slow down over time due to maintenance patches, reactive
systems sustain a consistently high level of performance.
Conclusion
Reactive programming concepts have been around for a long time and were first applied to the
spreadsheetdependency management done right by computers automatically rather than by
programmers who dont appreciate doing the repetitive coding again and again. CA Technologies
is the first to apply reactive programming techniques to databases and it brings the simplicity of
the spreadsheet to API developmentspecs that can almost be executed as-is, live APIs that can
be understood by business and programming staff alike and an easier maintenance process and
automatic documentation. In addition, JavaScript is provided to enhance and complement reactive
rules to ensure 100 percent coverage of application requiriements.
CA Technologies (NASDAQ: CA) creates software that fuels transformation for companies and enables
them to seize the opportunities of the application economy. Software is at the heart of every business,
in every industry. From planning to development to management and security, CA is working with
companies worldwide to change the way we live, transact and communicate across mobile, private
and public cloud, distributed and mainframe environments. Learn more at ca.com.
Copyright 2015 CA. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies. CS200_160153_1115