CFDJ Feature: Web Applications Are Client/Server
CFDJ Feature: Web Applications Are Client/Server
BY TED BLUE
there are two major categories of databases to choose from: local, such as Microsoft Access, dBASE, and FoxPro, and client/server, such as Oracle, SQL server, and Sybase. Dont kid yourself, this is not a trivial decision. Changing database platforms after development has begun can be difficult, and it becomes even more so the further along you get. Theres a point of no return for most complex development projects where its no longer cost effective to change the database. At this point a major rewrite would be in order. In addition, some features your Web site might require may or may not be supported by the underlying database platform. This article explores the fundamental differences between local and client/server databases, and the issues that often arise when choosing a database platform
W
CFDJ MARCH
fundamental choices that must be made is which database platform to use. Each platform offers its own unique capabilities. Basically,
for a ColdFusion Web application. For discussion purposes, well assume the ColdFusion application is for e-commerce and the site will be heavily trafficked.
36
Data
Web Server
Template (*.cfm)
In Figure 1 there are two separate client/server relationships: between the Web browser and the Web server where the browser is the client, and between the Web server and the ColdFusion Application Server where the Web server is the client. A third client/server relationship may also exist between the ColdFusion Application Server and the database server if the data is stored on a database management system (DBMS). Well discuss this in more detail in a later section.
is directly affected by the time required to conduct the database operation, this is a rather critical limitation. If you combine this limitation with the fact that most Web databases gradually increase in size over time, you have a situation that will eventually bring your Web site to a crawl. Wheres the load limit for local databases? This depends on a number of factors, not the least of which is the code you write to access and manage the data. However, most experienced Web developers would probably agree that a local database starts having performance problems at somewhere between 1020 simultaneous users. Not a lot if youre trying to build a Web site to handle tens of thousands of hits per day or more. Also, this problem cant be easily alleviated by throwing more hardware at the Web server (or database file server, as the case may be). The limitation is a symptom of a deeper more fundamental problem, rooted in the way local database formats are structured.
Local Databases
Lets start the discussion with local databases. Technically, a local database is any data stored in an unmanaged database format that is, a database that doesnt have or require dedicated database management software to access its data. Examples of local database formats include Microsoft Access, dBASE, FoxPro, and Paradox; other less-used formats are ISAM, B-Tree, and Delimited Text. Connecting to an unmanaged, local data format requires that the appropriate ODBC driver is set up and configured correctly on the Web server and any essential passwords are specified. The database files can be located elsewhere. Assuming the ODBC driver is configured correctly, no additional preparation is needed you can access the data using appropriate ColdFusion techniques, such as the CFQUERY tag. One of the most attractive features of using a local database format is cost. The only real cost associated with a local database format is the tool you would use to create and manage the data files. For example, you might purchase a copy of Microsoft Access for creating and managing your .MDB files. In some cases the data format has no inherent cost, as the file format is commonly used by a number of other tools. The dBASE file format, for instance, can be easily created from an Excel spreadsheet or from a variety of other software applications. In almost all cases there are no licensing fees or royalties for using these database formats.
www. ColdFusionJournal.com
Performance
Local Database
37
This problem exists, again, because a local database has no database management system to prevent such problems.
Any performance decreases due to load in a client/server database arent nearly as dramatic as those encountered in local database formats. In addition, throwing more hardware at the database server usually improves its performance. This is far more efficient than relying on the underlying operating system to queue requests for access to a shared local data source. In most client/server systems, significant performance drops dont occur until there are hundreds, or in some cases thousands, of concurrent users. When performance is affected, the performance decline is much more gradual than with local databases.
38
Performance
Another common problem with local database formats is that when data is written, theres nothing to prevent multiple users from accessing the same data at the same time. Some local database formats provide record-locking mechanisms to prevent simultaneous access to the same data, but this assumes the application can lock a record for the entire period of time its being edited (e.g., pessimistic record locking). Web applications are essentially stateless, and users that are editing data dont (should not, cannot) retain locks on records. This is because theres no way for the server to detect if a user abandons the editing session, thereby leaving the record in a locked state. This allows multiple users to potentially change the same data at the same time, which in turn can corrupt the data. Even if the absence of pessimistic locking doesnt corrupt the data, another potential problem exists with local database formats since theres no database management system to prevent concurrency problems. Consider a record with a field that represents an account balance for a particular client thats updated in the following sequence: 1. User A reads the account balance of $100. 2. User B reads the account balance of $100. 3. User A increases the account balance by $10, and updates the database with this value. The database now contains the value $110. 4. User B increases the account balance by $10, and updates the database with this value The database now contains the value $110, but it should be $120, as it has been increased by $10 two times.
throwing more hardware at the database server usually improves its performance
Performance Under Load
Since these are managed systems, many of the problems associated with local database formats are simply nonissues with a client/server database system. For example, most client/server systems can handle huge numbers of records (billions of records is not uncommon) without a dramatic performance penalty (see Figure 3). In many systems performance can actually increase under additional user load, as the DBMS can reuse previously cached queries and service multiple client requests concurrently.
Transaction Control
Since a client/server database system allows many users to modify data offline, its perfectly suited to Web application use in which users read and modify the data remotely. A client/server DBMS does this with transaction control, logging all potential changes by all users and layering on the updates in the correct order. This alone is no trivial task, as it requires a sophisticated logging and analysis system to simultaneously keep track of all reads and writes for all users. Transaction control is implemented by logging client reads and writes to a transaction log, which the database system then uses to ensure the correct changes are applied, in the correct order, for each user. Another advantage of using a transaction processing system is that multiple changes can be treated as if they were a single unit of work. For example, consider a simple transfer from your checking account to your savings account. Two updates must occur reduce the checking account by a specified amount, and increase the savings account by the same amount. If either transaction fails to complete (for whatever reason), someone will lose some cash. If the checking account was updated but the savings account update failed, you lose. If the savings account was updated but the checking account update failed, you win. Both updates must occur for the accounts to balance correctly. Transaction processing allows the database server to track changes for both updates, then commit them both once theyre successfully done. If either update fails, both records are rolled back to their state just prior to the updates. Another useful side effect of using transactions to track all changes for all clients is that if a disaster occurs at any point, such as a power failure, the database system can detect these open transactions at restart and roll them back automatically.
www. ColdFusionJournal.com
Client/Server Database
CFDJ MARCH
Making a Choice
Are you scared of local databases yet? Dont be. They simply werent intended for high volume, heavily accessed Web sites. The choice of a local database as a permanent database for your Web site might be appropriate if the following conditions are true: The site will provide read-only data that wont be modified remotely by clients. The site wont experience high levels of traffic. The site wont manage large amounts of data. The site wont implement complex transactions. If your site doesnt fit into all of these categories, you should seriously consider using a client/server database system. Choose wisely
About the Author
Are you scared of local databases yet? Dont be. They simply werent intended for high volume
Another cost associated with client/server databases is the cost of implementation and maintenance. While some systems, like Microsoft SQL Server, are relatively simple to install and maintain, others, like Oracle, can require a highly paid professional database administrator in order to operate. Again, most fall somewhere in between.
Ted Blue is president of Blue Star Training (www.bluestarcorp.com), where he conducts training for Internet, software development, and database-related topics. Hes the Webmaster for a number of sites, including the San Diego ColdFusion User Group site (www.sdcfug.com). Hes also the founder of CERTIFIABLE Internet Services (www.certifiable.net), a Web hosting company.
[email protected]
40
CFDJ MARCH
www. ColdFusionJournal.com