Creating a Simple SQL Server 2005 Notification Application: Stock Quotes
In this chapter
- The SQL-NS Application Model
- Building the Stock Application's ADF
- Specifying Other Parts of the Stock Application
- Running the Stock Application
- Inside the Running Application
- What Has the SQL-NS Platform Provided?
- Cleaning Up the Instance and Application
In this chapter, you'll see your first SQL-NS application: a stock notification service similar to those offered by many real-world stockbrokers. The application allows subscribers to enter subscriptions for stocks in which they are interested and notifies them when those stocks cross the price targets they specify.
Think of this chapter as a tour. My intent is simply to show you around the various facilities that the SQL-NS platform offers so that you get a feel for the application model and the process of coding to it.
We will look at code in this chapter, but simply for the purpose of understanding the concepts behind the application. A line-by-line explanation of the code at this stage would drown out the simpler picture that I'm trying to show.
Instead, I will gloss over (or in some cases, completely ignore) some parts of the code you will see; I'll just highlight those pieces of the code that illustrate particularly important parts of the application model. Subsequent chapters will cover the rest in detail.
The SQL-NS Application Model
SQL-NS can be used to build a variety of notification applications with different uses and for different application domains. But, when viewed from the highest level, all these notification applications conform to the same basic model, shown in Figure 3.1.
Figure 3.1 High-level view of a notification application.
Data enters the application from the outside world. This data can be pulled in by the application or pushed in by an external source. In SQL-NS terms, each piece of data is referred to as an event because it represents some happening in the outside world that may potentially be of interest to some subscribers. An event may be a new price for a stock, a notice of a new concert listing, or a gate change for a flight.
The notification application maintains users’ subscriptions. Subscriptions are users’ declarations of what kinds of events interest them. When events arrive, the application matches them against the subscriptions and produces a set of notifications. These notifications are delivered to the end users.
Events as Data
Events are descriptions of things that happen in the real-world that can be represented as data. For example, a change in the price of a stock (an event potentially of interest to a stockbroker client) can be described as a piece of structured data containing a stock symbol field and a stock price field. A traffic incident event (for an application that notifies commuters about road conditions) might contain a field that describes the location of the incident and another describing the incident type (accident, road closure, weather warning, and so on).
Whatever the type of event, its description can be modeled as data. The structure of the data can be described with a schema that indicates the names of the fields and their data types. Given this schema, it’s easy to construct a database table to store the event data. For example, stock events can be stored as rows in a table, as shown in Figure 3.2.
Figure 3.2 Modeling stock events as rows in a table.
Subscriptions as Data
Thinking of events as data is usually quite natural. Although it may be somewhat less intuitive at first, subscriptions can be modeled as data, too. Think of the subscriptions in a stock notification application. Let’s say that all subscriptions will have the form
"Notify me when the price of stock S reaches price target T."
where S represents some stock symbol and T represents a price target. A subscription example might be
"Notify me when the price of stock XYZ reaches price target $50.00."
If all the subscriptions are constrained to this form, an individual subscription can be represented as a pair of values for S and T. Such subscriptions could be stored in a table, as shown in Figure 3.3.
Figure 3.3 Modeling stock subscriptions as rows in a table.
Each row identifies the subscriber, the stock symbol, and the price target of interest. For illustrative purposes, the subscriber is represented by a name, but in an actual application, a more appropriate identifier may be used.
Matching Events with Subscriptions
As mentioned in Chapter 1, "An Overview of Notification Applications," the matching of events against subscriptions is the key function of any notification application. If the matching can be implemented efficiently, the application will scale to large volumes.
With events and subscriptions both represented as data, matching can be accomplished by means of a SQL join. Given the table structures in Figures 3.2and 3.3 for events and subscriptions—and let’s say that we called the events table Events and the subscriptions table Subscriptions—the following SQL statement would determine the matches:
SELECT S.Subscriber, E.StockSymbol, E.StockPrice FROM Events E JOIN Subscriptions S ON E.StockSymbol = S.StockSymbol WHERE E.StockPrice >= S.PriceTarget
This statement joins the stock events table with the subscriptions table on stock symbol and then selects rows where the stock price in the event is greater than or equal to the stock price target specified in the subscription. The rows returned by this query represent the set of matches between the events and subscriptions that should result in notifications being sent. For the particular data shown in the preceding examples, Figure 3.4 shows the results of the query.
Figure 3.4 Results of matching events with subscriptions.
Note that only three of the four subscriptions matched: Jane’s subscription specified a price target for PQS of 100.00, and because the event for PQS indicated that the price was only 95.30, the matching query did not return a row for Jane.
Each of the rows in the results table is the raw data for a notification to be sent. Thus, notifications also can be modeled as rows of data in a table. The notification data can later be packaged into a readable message and delivered to the appropriate subscriber.
Scalability of the SQL-NS Application Model
The modeling of both events and subscriptions as data is a key innovation of SQL-NS and the basis for its inherent scalability. Because both events and subscriptions are rows in tables, SQL joins can be used to match them. In general, SQL joins are extremely efficient at matching large sets of data; more than 20 years of query processing and indexing developments make this possible. As long as a reasonable join query can be written for a particular event and subscription schema (in most cases, one can), the cost of matching (in terms of computing resources) will be low. Furthermore, this cost grows sublinearly with the amount of data. That is, if you double the number of events or subscriptions, the cost of matching increases by less than a factor of two.
This model is different from that used by most other pub-sub systems. Most other systems model individual subscriptions as queries, rather than data. The simplest of these systems evaluates the subscription queries one-by-one for a given set of events. This strategy is expensive, and as the number of subscriptions and events grows, the cost of evaluation grows—at best, linearly, at worst, exponentially. The more sophisticated of these systems attempt to obtain performance gains by indexing the subscription queries and looking for logical shortcuts in the query evaluation. But the effectiveness of these strategies is limited by the structural differences and complexity of the subscriptions and isn’t always assured. In many cases, systems that model subscriptions as queries can do little better than one-at-a-time evaluation.
When you build a SQL-NS application, you have a choice of two models for subscription evaluation. In the first model, developer-defined logic is used to determine whether a match between an event and a subscription has occurred. In this model, you, the application developer, write the SQL join query that finds the matches. This query might look something like the SQL statement shown earlier in the "Matching Events with Subscriptions" section.
By writing the query, you are defining the conditions that must be true for a match to occur. The subscribers can specify only values for the data referenced in the query. For example, the logic coded into the stock application’s query says that a stock event matches a stock subscription when the stock symbols are the same and the stock price in the event is greater than, or equal to, the stock price target in the subscription. Subscribers cannot change this logic; they can only provide values for the stock symbol and stock price target in individual subscriptions.
In the second subscription evaluation model, user-defined logic determines whether an event matches a subscription. The users of the application (the subscribers) choose the conditions that must be true for a match to occur. These conditions can be arbitrary combinations of Boolean predicates based on the event data. In this model, each subscription can specify a different matching condition. For example, given the stock event data, one subscription’s condition might state that a match occurs when the stock price in the event is less than a particular price target. Another subscription’s condition may stipulate that a match occurs when the stock price in the event is within a particular range of values.
Regardless of whether you choose developer-defined or user-defined logic for the subscriptions in your application, SQL-NS uses queries to evaluate groups of subscriptions at the same time. In the case of developer-defined logic, the query provided by the developer is associated with a named subscription type, and all subscriptions of that type are evaluated by the single query. When user-defined logic is used, SQL-NS also uses a single query to evaluate subscriptions of a common type. The difference is that subscriptions are grouped into types based on the logical structure of their conditions (rather than assigned to predefined, named subscription types), and the queries used to evaluate them are constructed dynamically by SQL-NS. For a description of how SQL-NS uses queries to evaluate subscriptions with user-defined logic, refer to the "Evaluating User-Defined Logic with Queries" sidebar (p. 45).
Your choice of subscription-evaluation model (developer-defined or user-defined logic) affects the scalability of your applications. Generally, applications that use developer-defined logic are more scalable because subscription grouping is enforced by the developer. With user-defined logic, the extent to which grouping is possible depends on the similarity of the subscriptions entered. In the worst case, if every subscription condition has a different structure, no grouping is possible and the application would have to evaluate one subscription at a time. In practice, it's unlikely that this worst-case scenario would actually play out; in most cases, some degree of grouping is achievable.
Just because you have the option of supporting user-defined logic in your SQL-NS applications does not mean you always should. It’s worth questioning whether the flexibility offered by user-defined logic, which requires a sacrifice in scalability, is really needed in your applications. The obvious drawbacks to using developer-defined logic are that all subscriptions must have the same structure, and the only kinds of subscriptions users can enter are those the developer has implemented. But do your users really need to be able to enter arbitrarily complex subscriptions? Does each user need to be able to enter a different kind of subscription? Or can you devise a set of subscription types that cover the overwhelming majority of subscriptions users will want to enter?
Remember that SQL-NS enables you to implement several types of subscriptions in a single application, each with a separate data schema and matching query. Experience has shown that in the vast majority of applications, developers can predict what subscriptions users will want to enter and implement those as predefined subscription types. Often, the lack of flexibility in the subscription-matching logic is not even noticed by the users of these applications.
All the sample applications in Parts I, II, and III of this book (including the sample application in this chapter) use only developer-defined matching logic. The use of user-defined matching logic is covered in Part IV, in Chapter 18, "User-Defined Matching Logic in SQL-NS Applications." At this point, you should focus on learning the fundamental SQL-NS concepts, the majority of which are the same, regardless of which subscription evaluation model you choose.
Programming to the SQL-NS Application Model
In essence, the SQL-NS application model views events and subscriptions as data and uses SQL joins to match them. As a developer building on the SQL-NS platform, you define the following aspects of your application:
Schemas for the event, subscription, and notification data
Logic that the application executes to perform matching and maintain state
Configuration of the SQL-NS execution engine components that run the application
You provide all this information in an XML document called an Application Definition File (ADF). Think of the ADF as the source code for the application: to create a new application, you author a new ADF.
When you write an ADF, you usually begin with the elements that define schemas. Each schema is a description of the size and shape of one kind of data. You provide the names of the fields and their data types, much as you would if you were defining a SQL table. The schema for the events specifies the structure of the data your application receives from its event sources. The schema for the subscription data describes the information each subscriber will provide when creating a subscription. The notification schema describes the data content of the notifications your application will deliver.
In addition to the data schemas, the ADF also specifies how events and subscriptions are matched to form notifications. If you choose developer-defined logic as your subscription evaluation model, the matching logic you provide in the ADF consists of actual SQL statements that find the matches. These statements operate on the event and subscription data (as defined by their respective schemas) and produce a row of data for each notification to be sent (the columns in the resultsets correspond to the fields in the notification schemas). If instead you choose user-defined logic as your evaluation model, in the logic sections of the ADF, you provide SQL statements that produce notification data (conforming to the notification schemas) from event and subscription data that has already been determined to match (according to the user-defined conditions).
In the component configuration section of the ADF, you specify how the SQL-NS engine components should run your application. These components perform such functions as gathering event data from event sources, coordinating the execution of the matching logic, and delivering notifications to their destinations. You can specify which components should be used in your application, how they are distributed across various servers, what resources they should use, and when they should run.
When your ADF is complete, you compile it using the SQL-NS compiler. The compiler translates the XML application definition into a set of database objects that will be used to run the application. These include tables for configuration settings, events, subscriptions, and notifications, as well as stored procedures that execute the SQL statements you provided in the ADF. The compiler installs these database objects into a database on your SQL server and populates some of the tables with the configuration information from the ADF. When you run your application, the SQL-NS engine connects to this database, reads the configuration information, and starts its various components; those components then interact with the database (reading and writing data) as they perform their execution functions. This process is illustrated in Figure 3.5.
Figure 3.5 The ADF is compiled into database structures, and the SQL-NS engine runs the application.
In the remainder of this chapter, we’ll create and run the stock notification application. In doing so, we’ll look at the data schemas, matching logic, and component configuration, as specified in the application’s ADF. Although there are other aspects to the application, we’ll focus on these for now because they form the application’s core and are the most illustrative of the SQL-NS application model.