This document discusses how to solve the C20K problem of handling 20,000 simultaneous PHP users on a single database server using built-in database mechanisms in PHP. It describes how database resident connection pooling, query change notification, client-side query result caching, scaling with stored procedures, database partitioning, and fast application notification can be used to improve performance and scalability. Configuring these features and case studies demonstrating their effectiveness in achieving the C20K goal are provided.
7. PHP Lacks Proper Connection Pool
Current choices:
2. Persistent Connections
• Connection not automatically closed at
script completion
• Fast for subsequent connections but
holds resources when application idle
⇒ Overallocation – Waste of system resources
• Non Persistent Connections
• Connection closed at script completion
⇒ High connect times
⇒ Unnecessary connect/disconnect CPU load
8. Database Resident Connection Pool
Dedicated servers
Connection Connection
( DB handle) Broker
Session
Session
Session
(User Handle)
1 Session
(User Handle)
Session
(User Handle)
(User Handle)
Oracle Net (User Handle)
2
• Pool of dedicated servers
• (1) Server allocated/locked on Appl. Connect
• (2) Direct server access after handoff
• (3) Server released on Appl. ConnClose .
• No maninthemiddle, low latency
9. Database Resident Connection Pool
• Pools a set of dedicated servers on each database
instance
• Designed primarily for process systems (PHP)
1/ Just Change the Connect String
<?php
$c = oci_pconnect("phpweb", "phpweb", "//localhost/orcl:pooled");
$s = oci_parse($c, 'select * from employees');
oci_execute($s);
oci_fetch_all($s, $res);
var_dump($res);
?>
2/ Zero code change: change TNS alias
• Currently in OCI, C/C++, PHP (OCI8), Python
10. DRCP in Action – PHP Connection
Pooled Database
Servers
PHP 3 Busy Server
Idle Server
PGA memory
oci Session memory
_ pco
n nec
t()
35k Idle Server
Apache Processes 1 2 .
.
35k
ct(
) .
ne
c on Connection
i_p
PHP oc
Broker
11. DRCP in Action – Closing Connection
Pooled Database
Servers
oci_close() Busy Server
PHP
PGA memory
Connection Session memory
Broker
Idle Server
35k
Apache Processes
4
.
.
PHP 35k
oci_close()
.
12. DRCP in Action – After Close
Pooled Database
Servers
Idle Server
PHP
PGA memory
Connection Session memory
socket Broker
connection
35k Idle Server
Apache Processes 5 .
.
PHP 35k
.
socket
connection
13. Configuring and Starting DRCP
• Configure the Pool (Optional)
SQL> execute dbms_connection_pool.configure_pool(pool_name
=> 'SYS_DEFAULT_CONNECTION_POOL',
minsize => 4,
maxsize => 40,
incrsize => 2,
session_cached_cursors => 20,
inactivity_timeout => 300,
max_think_time => 600,
max_use_session => 500000,
max_lifetime_session => 86400);
• Start the pool
SQL> execute dbms_connection_pool.start_pool();
14. C20K, Yes We Did!
PHP DRCP Benchmark
• PHP script
• connect, query, disconnect, sleep 1 second
• Database Server
• Dual CPU Intel P4/Xeon 3.00GHz 2GB RAM
• 32bit Red Hat Enterprise Linux 4
• DRCP
• 100 pooled servers, one connection broker
• 0.5% nb users simultaneously active
• PHP Hosts
• 3 machines similar to Db Server
• Apache
• PHP DRCP Whitepaper: http://tinyurl.com/554cz4
28. PL/SQL Stored Proc. (Bulk insert)
create or replace package mypkg as
type arrtype is table of varchar2(20)
index by pls_integer;
procedure myproc(p1 in arrtype);
end mypkg;
create or replace package body mypkg as
procedure myproc(p1 in arrtype) is
begin
forall i in indices of p1
insert into ptab values (p1(i));
end myproc;
end mypkg;
29. Using Java Stored Procedures in PHP
• Secure CreditCard Processing • Implement Parsers for various File
using JSSE Formats (txt, zip, xml, binary)
• Custom Alert applications that • Implement Image Transformation and
monitor business data Format Conversion (GIF, PNG,
• Sending emails with attachment JPEG, etc)
from within the database
• Implement databaseresident Content
• Produce PDF files from Result
Set Management System
• Execute external OS commands • HTTP CallOut
and external procedures • JDBC CallOut
• Implement Md5 CRC • RMI CallOut to SAP
• Publish Repository Content to • Web Services CallOut
Portal
• Messaging across Tiers
• Portable Logistic Applications
• RESTful Database Web Services*
• Db Resident Lucene*
* http://marceloochoa.blogspot.com/
37. Scaling w. Advanced Data Compression
Going Green
2500
Storage
2000
Reduction
More than 70% (up to 4X)
Compression Storage Savings
1500
MB
No Compression 1000
500
0
Table Scan Performance DML Performance
Less than
0.4 40
3% Overhead
2.5 x Faster
0.3 (seconds) 30
(seconds)
Time
Time
0.2 20
0.1 10
0 0
42. Configuring Fast Application
Notification for PHP
• High Availability feature with RAC or DG
• Usable with or without DRCP
• Available from Oracle 10gR2
• OCI8 1.3 supports FAN
5.Tell DB to broadcast FAN Events
SQL> execute
dbms_service.modify_service(service_name =>'SALES',
aq_ha_notifications =>TRUE);
6.Subscribe to FAN events
Example: Configure PHP for OCI8 to listen for FAN events
oci8.events = On
7.Application to Reconnect
43. Database Failover in PHP
When DB node or network fails
• Database generates FAN events
• Oracle error returned without TCP timeout delay
• PHP application is not blocked for TCP timeout – it can
immediately reconnect to surviving DB instance
$conn = doConnect();
$err = doSomeWork($conn);
if (isConnectionError($err)) {
// reconnect, find what was committed, and retry
$conn = doConnect();
$err = checkApplicationStateAndContinueWork($conn);
}
if ($err)
handleError($err);
44. C(N*20)K Using DRCP and RAC
• Scale your database horizontally to N*C20K with N
RAC nodes!!
• DRCP starts on all RAC instances
• Same pool limits apply to each individual RAC
instance
• min, max
• number of brokers
• max connections per broker
• DRCP connections benefit from TNS Listener
connection load balancing across RAC instances
www.oracle.com/technology/tech/php/pdf/phpscalabilityhatwp.pdf