Think Big code small

Often as a developer you find yourself maintaining legacy code that just was not built for the large stresses it is now under. Writing scalable applications should cost no more than non scalable ones, in-fact they can be smaller! And when you are planning your next web application that will take over the world you have to start somewhere. This article should demonstrate techniques for design that allow for horizontal and vertical scaling. In this first part we’ll talk about the principles of database abstraction, in the next in the series we will discover more about implementation using PHP’s oci8 extensions with example classes for your use.

PHP Strengths

PHP is a fast web scripting language especially when compiled as an apache module, and is an excellent thin web layer that scales well horizontally. PHP and Apache can handle many transactions quickly, compared to a more heavy weight application server such as JBoss, hungry in terms of CPU and memory per request.

Oracle Strengths

Oracle is a robust, high performance transactional DBMS. It can also perform complex data processing through its native PL/SQL language. MySQL achilles heal is its transactional processing capabilities, although ISAM is fast on selects a single update or insert locks the entire table. Increasingly with user interactive and user generated content of the WEB 2.0 era transaction processing is king.

Implementation

We can build scalability through abstraction, we can handle many requests with apache and PHP and offload much of the data intensive computational work to Oracle. We can achieve this with the use of re used persistent connections and a wrapper abstraction layer to Oracle. For instance our online library allows people to renew their books online. To abstract the computational work involved in renewing a book we can farm it out to oracle.

Example:

This calls the corresponding procedure in oracle called renewBook and oracle takes care of looking up the book applying control logic and updating the relevant records.

This approach separates the control logic of the application from the web tier. You can now scale your web tier and your data and logic tier separately.

Benefits of Abstraction

Each procedure is compiled once and the sql contained within it is optimised and reused so the query execution planner does not have to work each time. Oracle scales nicely, oracle have many products filling the gaps from low end to high end big iron enterprise offerings, eventually your applications logic could be running on a 64 core CPU! Now with persistent connections the connections to oracle can be reused and therefore there is little overhead on each script having to open and close heavy weight connections. You can have multiple front ends on the application, allowing a java swing client to be lightweight and has access to all the logic that is within the procedures. You can scale where your bottle necks are occurring, if the hold up is with the number of connections non worker apache can handle than you can load balance apache and add more nodes.

Natural Path of a Growing PHP Oracle Application

Here is an example of the natural growth pattern of a PHP Oracle Application:

  • Shared Oracle and PHP space.
  • Dedicated Oracle and PHP server with Express Edition.
  • Separate Oracle and PHP servers.
  • Load balanced Apache PHP servers with a single Oracle back end.
  • Load balanced Apache PHP servers with a Clustered and Partitioned Oracle Backend.

This article should give you the basic overview of abstraction with Oracle, you can download Oracle Express Edition and PHP for free and you can follow the examples later in the series.

About the Author

Gareth Fiford has been a PHP and Oracle developer with leading telecoms companies and gambling companies and now is the owner of www.phporaclehosting.com