Oracle Database 11g: Streams Advanced Queuing

Transcription

Oracle Database 11g:Advanced QueuingAn Oracle White PaperNovember 2010

Advanced QueuingINTRODUCTIONBusinesses evolve rapidly; Information Technology (IT) systems need to adapt tochanging business requirements while managing costs. Businesses should designthe IT infrastructure to allow for automation of business process workflows andeasy integration of all information systems. The real-time flow of informationacross these systems is critical to the success of the overall business. Globalization,M & A and Supply Chain innovations such as new 3rd party suppliers or fulfillmentpartners introduce additional requirements to integrate disparate andgeographically distributed systems. Costs and time constraints due to an extremelycompetitive marketplace make investments in new platforms a non-starter. ITmanagers need a standards-based enterprise-messaging infrastructure that canintegrate the different systems and technologies on a scalable, reliable and powerfulplatform for the real-time flow of information.Oracle Database 11g provides enterprise messaging infrastructure with OracleAdvanced Queuing (AQ), which is a key component in automating businessprocess workflows for distributed applications. Using AQ, businesses can takeadvantage of the Oracle Database 11g for enterprise messaging needs without theneed for expensive, high-end message-oriented middleware products.Organizations not only can manage all the data inside the Oracle database, but alsomanage the flow and exchange of data using messages to different systems in onehighly reliable, available and scalable Oracle Database. AQ implements themessage queuing functionality natively inside the database and leverages its easymanageability, high availability, high performance and security. AQ supports pointto-point and publish/subscribe queues, persistent and buffered messaging, andmessage ordering priorities that offer flexibility and powerful messagingfunctionality to applications. With AQ in the Oracle database, Oracle hassignificantly lowered the barrier for including enterprise-messaging functionality inany applications.Oracle Advanced QueuingPage 2

This paper focuses on the typical requirements of an enterprise-messaginginfrastructure and discusses how Advanced Queuing technologies available in theOracle Database can help automate business workflows in a distributedenvironment. The paper highlights some of the advanced messaging, routing andpropagation features of AQ and how businesses can leverage the databaseintegrated messaging functionality in the Oracle Database to maximize the returnon their investments on infrastructure and build robust, highly scalable distributedapplications with better quality of service to users.MESSAGE QUEUINGMessage queuing infrastructure enables information sharing and integrationamongst different, in many cases distributed, applications. Producer applicationssend or enqueue messages into queues from which consumer applications receiveor dequeue messages. Producers and consumers interact with the queuesasynchronously and this “decoupling” is the centerpiece of message queuing.Messages often represent critical business events and impose certain characteristicson the underlying messaging infrastructure. The creation, consumption andpropagation of the messages must be handled with highest levels of integrity.Messages must be protected against failures in any component in the enterprisestack and be recoverable in all cases. Message content and attributes must be easilyretrievable through standard interfaces. Finally, the infrastructure should bescalable without compromising the performance and reliability of the system.ORACLE ADVANCED QUEUINGOracle Advanced Queuing (AQ) is a database-integrated messaging infrastructurein Oracle Database 11g. AQ leverages the functionality of the Oracle database tostore messages in persistent queues. All operational benefits of the Oracle databasesuch as High Availability, Scalability and Reliability are applicable to the messagesand queues in AQ. Standard database features such as backup & recovery, securityand manageability are available to AQ. Oracle technologies such as Data Guard,Real Application Clusters (RAC), Automatic Storage Management (ASM) can becombined with AQ to deliver a highly available and scalable messaging system.Using standard, off-the-shelf server and storage, customers can build AQ-basedmessaging systems that can scale linearly without sacrificing performance orreliability.Oracle Advanced QueuingPage 3

AQ ComponentsThe four main components of AQ are:1.Message - A message consists of message content, or payload, which canbe specified using typed or raw data and message attributes or controlinformation.2.Message Queue – Messages are stored in queues and these queues act as“postal boxes” where different applications can look for “mail” in theform of messages. Thus, when one application wants to contact certainapplications for certain tasks, it can leave messages in these queues, andthe receiving applications will be able to find these messages forprocessing. AQ supports enqueue, dequeue, and propagation operationswhere the queue type is an abstract datatype (ADT). AQ also supports theANYDATA queue type, which allows applications to enqueue differentmessage types in a single queue. A queue is persisted in the database usingone or more database tables where messages in a queue correspond torows in the underlying table.3.Message Interfaces – AQ can integrate seamlessly with the existingapplications through support for popular standards. AQ messages can becreated, queried, propagated and consumed using popular programminginterfaces (API) such as PL/SQL, C/C , Java and Visual Basic (OracleObjects for OLE). AQ provides support for the Java Message Service(JMS) API that allows Java applications to utilize the message queuingfunctionality using the oracle.jms Java package.4.Message Handling – Messages can be routed according to data in themessage payload or attributes. AQ also supports rules based messagerouting where complex rules can be created by combining payload-basedand attribute-based rules. Additionally, message transformations can beapplied to messages to re-format data and delivered automatically to targetapplications or subscribers. Oracle Database 11g can also exchange AQmessages with IBM WebSphere MQ through the Oracle MessagingGateway.The various components in AQ provide the functionality needed for enterpriseapplication integration or distributed applications. In a typical integratedenvironment as shown below in Figure 1, messages are created, propagated andconsumed between the Oracle Database server, applications and users.Oracle Advanced QueuingPage 4

Figure 1: Integrated Application Environment using Oracle AQAQ CapabilitiesAQ is an integrated messaging infrastructure inside the Oracle database and offersmany key capabilities for developing message-based distributed applications.Transaction supportQuality of Service (QoS)Queue ModelsSecurityMessage PropagationMessage TransformationRules-based Message RoutingTransaction SupportAQ provides the transactional semantics to messages using the same underlyinginfrastructure in the Oracle Database used for relational data. Queue operationssuch as enqueue and dequeue are atomic and the Oracle database guarantees theconsistency of the messages in persistent queues. Applications can manipulate theunderlying data and its exchange through messages in a single transaction. Whenexternal or third party messaging systems are used, applications often need to use2-phase commit algorithms to achieve transactional semantics, which could beOracle Advanced QueuingPage 5

expensive. Messages are stored along with the other data using the same storagedevices and do not require any special setup or management. With AQ, messagescan be retained for any length of time (infinite if required), and used fortracking/auditing purposes or for building message warehouses for data miningand analytics.Quality of ServiceMessages need to be persisted for various reasons – regulatory compliance,business process auditing, analytics are a few examples where messages need to beretained for different periods of time. Applications can rely on AQ‟s messagequeuing infrastructure for guaranteed exactly-once delivery mechanism. Financialservices hubs that integrate portfolio management systems with trade processingsystems need to retain client order messages to satisfy legal requirements.Integrating with partners or 3rd party fulfillment contractors for order processingrequires messages be exchanged and retained until order process is complete.Applications exchange data in the form of messages and need to retain thesemessages for guaranteeing business process workflows.In other cases, messages need to have the lowest latency, as measured by the timedelay in enqueing and dequeing the message. Such messages can be transient i.e.need not be retained. Subscribers to stock quotes need updates at regular intervals,however failures need not be re-tried as the most current stock update is sufficientfor most consumers. Cell phone coupons based on location, for example, shouldbe delivered to mobile subscribers as soon as possible. In case of errors, theapplication can decide to re-send the deals to those subscribers or send new dealsbased on new location of the subscribers. For these types of applications, AQprovides in-memory or buffered messaging for the lowest latency, highperformance message management infrastructure.Oracle Database 11g AQ offers comprehensive capabilities for both persistent andtransient messages.1. Persistent MessagingAQ provides the queues and the underlying queue tables to persist messagesthat must be guaranteed to be processed exactly once, even in the event ofnetwork, hardware or software failures. Applications can use AQ queues toprocess messages arriving simultaneously from external programs or frommodules within applications. AQ supports different mechanisms to controlthe order in which messages are processed. Applications can specify a „priority‟for each message at enqueue time, which can be used to control the order inwhich messages are consumed. Alternately, messages can also be sortedaccording to the enqueue time or commit time to get a LIFO or FIFO orderfor consuming the messages. Commit time is the time at which the transactionOracle Advanced QueuingPage 6

was committed and this is especially useful when transactions areinterdependent. The persistent quality of service is the default in AQ.2. Buffered MessagingCertain applications require higher performance and are willing to tradeoff thereliability and the transactional support offered by the AQ PersistentMessaging. In such cases, AQ‟s Buffered Messaging is an ideal solution as allthe queue operations such as enqueue and dequeue can be much fastercompared to persistent queues. Message retention is only available topersistent messages and not to buffered messages. Queues setup for bufferedmessages store messages in memory and do not involve disk I/O. Thememory for buffered queues is allocated from the SGA and can be controlledusing the „streams pool size‟ parameter. Alternately, Oracle can automaticallyallocate the appropriate memory using SGA auto-tuning. All message orderingschemes available for persistent messages are available to buffered messages.AQ supports persistent and buffered messages through a common API andprovides a messaging infrastructure that effectively separates the application logicand the message integration logic. AQ queues can be setup under different queuingmodels such as point-to-point and publish-subscriber to enable businessapplications can communicate with each other flexibly and reliably.Queue ModelsAQ supports two queue models, namely point-to-point and publish/subscribequeues. A point-to-point or single-consumer queue is aimed at a specific target.Producers and consumers decide on a common queue in which to exchangemessages. A message in the point-to-point can be dequeued only once. Apublish/subscribe or multi-consumer queue is aimed at multiple targets. Messagesin a publish/subscribe queue can be dequeued by multiple consumers. This type ofqueue messaging can be used for broadcast or multicast dissemination.Applications can setup rules for delivery to consumers and these rules can bedefined on message payload, attributes or both. Subscriber applications can receivemessages that match the subscription rules automatically at dequeue time.Publishers need not be aware of the different consumers or rules and can continueto publish messages. AQ tracks the subscribers and can notify the subscriberapplications using the Oracle Call Interface (OCI) or PL/SQL notificationmechanism. This allows for a push mode of message delivery.Oracle Advanced QueuingPage 7

SecurityAQ supports flexible security mechanisms to separate administration and thequeue operational tasks. System-level access control allows the application designeror DBA to control access for all queue operations and designate certain users asqueue administrators. A queue administrator can perform both the administrativeand operational tasks on any queue in the database. AQ also supports queue-levelaccess control for enqueue and dequeue operations. Access to particular queuescan be limited to only the applications running in the same schema.Message PropagationAQ can propagate messages from one queue to another queue in the samedatabase or in a remote database. This allows applications to communicateasynchronously with each other in a distributed environment without beingconnected to the same database or to the same queue. The source queue is a multiconsumer queue while the target queue can be either a single-consumer or multiconsumer queue. Messages enqueued in the source queue are propagatedautomatically and are available for dequeuing at the destination queue or queues.Propagation can be setup to run either continuously as a background process orrun only if there is a message to be propagated. With queue-to-queue propagation,a separate job is created to propagate messages for each source and destinationqueue pair. With queue to dblink propagation, propagation to all queues at a dblinkwill share the same propagation job.Message TransformationMost business-to-business (B2B) applications need to manipulate data in differentformats to integrate disparate applications and systems. AQ provides a completedata transformation engine to transform messages from one data type to another.AQ supports message transformations between different Oracle and user-defineddata types. These transformations can be SQL expressions, PL/SQL functions orJava stored procedures. AQ also supports transformations of XML documentsusing XSLT.Transformations change the format of a message, so that a message created by oneapplication can be understood by another application. AQ messagetransformations can be automatically applied to messages during enqueuing,dequeuing or subscribing to queues. A single transformation must be specifiedwhen enqueuing or dequeuing a message, irrespective of the number of therecipients of the message. In the case of remote subscriptions, a singletransformation must be specified for all messages sent to a particular queue at thedestination. Message transformations can be applied to both persistent andbuffered messages. Transformations are exported with a schema or a full databaseOracle Advanced QueuingPage 8

export. If an AQ table is exported the transformation corresponding to the queuetable will also be exported.Rules-based Message RoutingAQ can intelligently route messages to the right subscribers in a multi-consumerqueue or propagate messages to the right queues based on rules specified by eachapplication. The rules can be defined on message properties, message data content,or both. Similar in syntax to the WHERE clause of a SQL query, rules can beexpressed in terms of attributes that represent message properties or messagecontent.AQ DeploymentsAQ is a popular infrastructure for building enterprise messaging functionalityacross many industries. The following provides examples of AQ deployments.A leading online retailer integrated its CRM system that was hosted by a third partyprovider with its backend Order processing system using AQ‟s robust and reliabledatabase-integrated messaging infrastructure. Customer and order data weresynchronized in real-time between the two systems in geographically distributedsites. Message persistence with AQ allowed the two systems to send and receivedata changes through persistent queues. This asynchronous message passing decoupled the two systems and allowed the online store to be available to customersto collect orders even if the order-processing site was down. With AQ, thecompany leveraged the reliability and scalability of the Oracle Database to handlepeak traffic during holiday seasons and developed the integration in a matter ofweeks using AQ‟s standards based interfaces.A European financial services firm implemented AQ as the core platform tointegrate the firm‟s global IT infrastructure. Enterprise messaging provided by AQwas used to connect the hubs in London, New York, Singapore, Hong Kong andTokyo. The core applications in the hubs exchange financial transactions and otherinformation through XML messages. Due to the sensitive nature of theinformation, the customer required 100% reliable messaging with zero messageloss in the event of failure or malfunction of any software or hardware component.Messages had to be delivered in the same order of creation and also be available ina disaster recovery (DR) location for each hub. This customer used multiconsumer queues with persistent messaging in each hub. In addition, using OracleData Guard, messages were synchronously copied to the DR locations. Messageswere propagated from the local hubs to remote hubs using AQ propagation andappropriate locale-specific transformations for messages were applied at thedestination hubs. AQ and the Oracle Database provided the robust, scalable andOracle Advanced QueuingPage 9

reliable messaging infrastructure to satisfy the customers‟ extremely stringentrequirements for guaranteed messaging at high throughputs.BEST PRACTICESAQ implements queues using user tables, index organized tables (IOTs) andindexes in the Oracle database. AQ operations such as enqueue and dequeuegenerate corresponding database activity. Performance of the underlying databaseoperations significantly impacts the overall performance of AQ. This sectiondetails Oracle‟s best practices, recommendations and tuning tips for optimalperformance of the AQ messaging infrastructure. Oracle recommends using automatic segment-space management (ASSM)tablespaces for the AQ queue tables, especially for high concurrencyapplications. Otherwise, initrans, freelists and freelist groups must betuned to achieve better AQ performance. Storage parameters can bespecified during creation of the queue table using the storage clauseparameter. When persistent messages are enqueued, they are stored in databasetables. The performance characteristics of queue operations on persistentmessages are similar to underlying database operations. The code path ofan enqueue operation is comparable to SELECT and INSERT into amulticolumn queue table with three index-organized tables. The code pathof a dequeue operation is comparable to a SELECT operation on themulti-column table and a DELETE operation on the dequeue indexorganized table. In many scenarios, for example when Oracle RealApplication Clusters (Oracle RAC) is not used and there is adequatestreams pool memory, the dequeue operation is optimized and iscomparable to a SELECT operation on a multi-column table. To takeadvantage of the optimized dequeue operations, increaseSTREAMS POOL SIZE to allocate up to 15M per queue. The queue table indexes and IOTs are automatically coalesced by AQbackground processes. However, they must continue to be monitored andcoalesced if needed. In 10.2, with automatic space segment management(ASSM), an online shrink operation may be used for the same purpose. Awell balanced index reduces queue monitor CPU consumption, andensures optimal enqueue-dequeue performance. Oracle RAC can be used to provide high availability and scalability to AQ.The performance of AQ can be improved by allowing different queues tobe managed by different RAC instances. Different instance affinities orpreferences can be specified for the queue tables that allows forparallelization of queue operations on different queues. Setting instanceaffinities allows for the partitioning the queue tables for queue-monitorOracle Advanced QueuingPage 10

scheduling and propagation. Oracle recommends setting instance affinitiesfor the queue tables. If an instance affinity is not set, the queue tables arepartitioned arbitrarily amongst the available instances that causes pingingbetween the application accessing the queue tables and the queue-monitorprocess monitoring the queue. For more information, please refer to theRAC Best Practices for AQ document n2-131805.pdf Ensure that statistics are being gathered so that the optimal query plansfor retrieving messages are being chosen. By default, queue tables arelocked out from automatic gathering of statistics. The recommended useis to gather statistics with a representative queue message load and lockthem. Ensure that there are enough queue monitor processes running toperform the background tasks. The queue monitor must also be runningfor other crucial background activity. Multiple qmn processes share theload; make sure that there are enough of them. These are auto-tuned, butcan be forced to a minimum number, if needed. Dequeue with a wait time should only be used with dedicated serverprocesses. In a shared server environment, the shared server process isdedicated to the dequeue operation for the duration of the call, includingthe wait time. The presence of many such processes can cause severeperformance and scalability problems and can result in deadlocking theshared server processes. Other performance best practices include batching multiple dequeueoperations on multi-consumer queues into a single transaction, usingNEXT as the navigation mode if not using message priorities, and usingthe REMOVE NODATA dequeue mode if dequeuing in BROWSEmode followed by a REMOVE. Please see the AQ documentation foradditional performance hints.PERFORMANCE VIEWSNew views for persistent messaging statistics, notification statistics andsubscription management improve monitoring of system performance andtroubleshooting in 11g. The Automatic Workload Repository (AWR) displays themost active queues for persistent messaging operations, allowing for easierdiagnosability of AQ performance problems. Users can generate a report basedon two AWR snapshots to compute enqueue rate, dequeue rate, and otherstatistics per queue or per subscriber. In addition, a performance monitoringPL/SQL package for AQ is available through Support Document 1163083.1.Oracle Advanced QueuingPage 11

CONCLUSIONOracle Advanced Queuing, built into the Oracle Database, offers a robustplatform to standardize and integrate the various technologies and applicationsinside the datacenter. Businesses can leverage the AQ‟s enterprise messaginginfrastructure to build highly scalable and reliable distributed applications.Powerful AQ features such as differing qualities of service, automatic messagetransformations, and propagations give businesses the tools needed to design apowerful and flexible messaging platform. Database integrated Advanced Queuingprovides smooth, real-time flow of critical information, less management and moreproductivity for your ever growing, scalable, highly available business.Oracle Advanced QueuingPage 12

ORACLE ADVANCED QUEUING – SHORT TUTORIALThe following is a short tutorial on how to configure and use Oracle AdvancedQueuing (AQ). This section demonstrates the basic functionality and the simplicityof using AQ.An electronic store needs to route the customer orders from its online store to theright warehouse in US or Europe for order fulfillment. The central CRMapplication collects the order along with the customer information and stores theorder data in the Oracle Database. The order details are enqueued as ADTmessages in AQ. The fulfillment applications for the different warehouses thendequeue the order messages (deleted from queue automatically) and process thecustomer orders. The CRM, Europe Fulfillment and US Fulfillment applicationswork asynchronously and should work even if other applications are down. Forexample, even if the Europe site is down, the online store should continue toprocess new customer orders through the CRM application and the US warehouseshould continue to process the orders for the US region.New entAppCRMApplicationThis tutorial explains the steps needed to setup and use the messaginginfrastructure in AQ.1.Configure AQ Administrator accountThe AQ Administrator user („aq admin‟) creates and owns the queuinginfrastructure. The role AQ ADMINISTATOR ROLE that allows for thecreation and administration of the queuing infrastructure needs to be grantedto the „aq admin‟ user.--create aq admin administrator accountCREATE USER aq admin IDENTIFIED BY aq adminDEFAULT TABLESPACE usersTEMPORARY TABLESPACE temp;ALTER USER aq admin QUOTA UNLIMITED ON users;Oracle Advanced QueuingPage 13

--grant roles to aq adminGRANT aq administrator role TO aq admin;GRANT connect TO aq admin;GRANT create type TO aq admin;2.Setup Order message payload and Orders queuesThe following steps must be executed as the aq admin user.Create the content or the payload of the message.CREATE TYPE orders message type AS OBJECT (order idNUMBER(15),Product codeVARCHAR2(10),Customer idVARCHAR2(10),order detailsVARCHAR2(4000),priceNUMBER(4,2),region codeVARCHAR2(100));Create Queue Table and QueueAfter creating the payload, the queuing infrastructure can be created.Queues are implemented using a queue table that can hold multiplequeues with the same payload type. The following creates a queue table„orders qt‟ and a queue „orders msg queue‟.DBMS AQADM.CREATE QUEUE TABLE (queue table 'aq admin.orders qt',queue payload type 'aq admin.orders message type');DBMS AQADM.CREATE QUEUE (queue name 'orders msg queue',queue table 'aq admin.orders msg qt',queue type DBMS AQADM.NORMAL QUEUE,max retries 0,retry delay 0,retention time 1209600,dependency tracking FALSE,comment 'Test Object Type Queue',auto commit FALSE);Start the queueOracle Advanced QueuingPage 14

DBMS AQADM.START QUEUE('orders msg queue');3.Configure AQ user accountThe AQ user („aq user‟) accesses the queuing infrastructure created in theabove step. The following creates the „aq user‟ account and grants thenecessary privileges.--create aq user user accountCREATE USER aq user IDENTIFIED BY aq user DEFAULTTABLESPACE users TEMPORARY TABLESPACE temp;--grant roles to aq userGRANT aq user role TO aq user;-grant EXECUTE on message type to aq userGRANT EXECUTE ON message type TO aq user;DBMS AQADM.GRANT QUEUE PRIVILEGE(privilege 'ALL',queue name 'aq admin.orders msg queue',grantee 'aq user',grant option FALSE);4.Subscriptions to the Orders queueThe orders queue has two subscriptions, one for orders made from withinthe US, and another for orders made from Europe. The region code in theorders message type distinguishes the two types of orders.-- need administrator privileges to add-- subscriberDBMS AQADM.ADD SUBSCRIBER(Queue name ‘aq admin.orders msg queue’,Subscriber ‘US ORDERS’,Rule ‘tab.user data.region code ‘ USA’’’);DBMS AQADM.ADD SUBSCRIBER(Queue name ‘aq admin.orders msg queue’,Subscriber ‘EUROPE ORDERS’,Rule ‘tab.user data.region code ‘ EUROPE’’’, Transformation ‘aq admin.Dollar to Euro’);Oracle Advanced QueuingPage 15

5.Create message transformations (optional)Message transformations can be automatically applied to messages in AQqueues. The code below shows an example of translating currency fromdollars to euros. The price field in the order message is specified indollars. When the European warehouse dequeues the message, the pricefield is automatically changed to euros as shown in the simple examplebelow.CREATE FUNCTIONFn Dollars to Euro(src aq admin.orders msg type)ReturnsTargetaq admin.orders msg type ASaq admin.orders msg type;BEGINTarget : aqadmin.orders msg type(src.order id,src.product code, src.customer id,src.order details, src.price*.5,src.region code);END;DBMS TRANSFORM.CREATE TRANSFORMATION(schema 'AQ ADMIN',name 'DOLLAR TO EURO’,from schema 'AQ ADMIN',from type 'ORDERS MSG TYPE',to schema 'AQ ADMIN',to type 'ORDERS MSG TYPE',transformation 'AQ ADMIN.Fn Dollars to Euro(source.user data)');6.Queue Operations – Enqueue and Dequeue MessagesThe following steps must be executed as the aq user user. The CRMapplication enqueues the order messages into the Orders queue that isthen dequeued by the Fulfillment applications.Enqueue Message - Enqueue a new order into the orders queueusing the DBMS AQ.ENQUEUE procedure. The order price isOracle Advanced QueuingPage 16

specified in dollars.DECLAREenqueue options dbms aq.enqueue options t;message propertiesdbms aq.message properties t;message handle RAW(16);message aq admin.orders message type;message id NUMBER;BEGINmessage : AQ ADMIN.MESSAGE TYPE (1, 325, 49,'Details: Digital Camera. Brand: ABC. Model:XYX' , 23.2, ‘Europe’ );-- default for enqueue options VISIBILITY is-- ON COMMIT. message has no delay and no-- expirationmessage properties.CORRELATION : message.order id;DBMS AQ.ENQUEUE (queue name 'aq admin.orders msg queue',enqueue option

Oracle Advanced Queuing (AQ) is a database-integrated messaging infrastructure in Oracle Database 11g. AQ leverages the functionality of the Oracle database to store messages in persistent queues. All operational benefits of the Oracle database such as High Availability, Scalability and Reliability are applicable to the messages and queues in AQ.