High Reliability DML and Concurrency Design Patterns for Apex

19
High reliability DML and concurrency design patterns for Apex Dan Appleman, Full Circle CRM, CTO Author: Advanced Apex Programming for Salesforce.com and Force.com @danappleman

description

Join us to learn several strategies for dealing with concurrency errors like the UNABLE_TO_LOCK_ROW error in Apex . We'll also review design patterns that can fail gracefully, and even recover silently when these errors occur ? all without compromising your data integrity.

Transcript of High Reliability DML and Concurrency Design Patterns for Apex

Page 1: High Reliability DML and Concurrency Design Patterns for Apex

High reliability DML and concurrency design patterns for Apex

Dan Appleman, Full Circle CRM, CTOAuthor: Advanced Apex Programming for Salesforce.com and Force.com@danappleman

Page 2: High Reliability DML and Concurrency Design Patterns for Apex

Safe HarborSafe harbor statement under the Private Securities Litigation Reform Act of 1995: This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties materialize or if any of the assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results expressed or implied by the forward-looking statements we make. All statements other than statements of historical fact could be deemed forward-looking, including any projections of product or service availability, subscriber growth, earnings, revenues, or other financial items and any statements regarding strategies or plans of management for future operations, statements of belief, any statements concerning new, planned, or upgraded services or technology developments and customer contracts or use of our services. The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new functionality for our service, new products and services, our new business model, our past operating losses, possible fluctuations in our operating results and rate of growth, interruptions or delays in our Web hosting, breach of our security measures, the outcome of any litigation, risks associated with completed and any possible mergers and acquisitions, the immature market in which we operate, our relatively limited operating history, our ability to expand, retain, and motivate our employees and manage our growth, new releases of our service and successful customer deployment, our limited history reselling non-salesforce.com products, and utilization and selling to larger enterprise customers. Further information on potential factors that could affect the financial results of salesforce.com, inc. is included in our annual report on Form 10-K for the most recent fiscal year and in our quarterly report on Form 10-Q for the most recent fiscal quarter. These documents and others containing important disclosures are available on the SEC Filings section of the Investor Information section of our Web site. Any unreleased services or features referenced in this or other presentations, press releases or public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make the purchase decisions based upon features that are currently available. Salesforce.com, inc. assumes no obligation and does not intend to update these forward-looking statements.

Page 3: High Reliability DML and Concurrency Design Patterns for Apex

Have you ever seen…

EXCEPTION_THROWN [32]|System.QueryException: Record Currently Unavailable: The record you are attempting to edit, or one of its related records, is currently being modified by another user. Please try again.

FATAL_ERROR System.DmlException: Update failed. First exception on row 0 with id ……………; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: []

Or

Page 4: High Reliability DML and Concurrency Design Patterns for Apex

Concurrency ProblemsThe most dangerous kind of bug in all of computer science.Why? Because concurrency problems and bugs can be:▪Virtually impossible to detect▪Virtually impossible to reproduce

Page 5: High Reliability DML and Concurrency Design Patterns for Apex

Most of us learn about concurrency errors as children…

Dad, can we go to the party?

Let me think: NO!

Page 6: High Reliability DML and Concurrency Design Patterns for Apex

Most of us learn about concurrency errors as children…

Dad, can we go to the party?

Let me think: NO!

Mom can we go tothe party?

15 seconds later

Sure, why not?

Page 7: High Reliability DML and Concurrency Design Patterns for Apex

… or as parents

Dad, can we go to the party?

Let me check withyour mom

Mom can we go tothe party?

What did your dad say?

Pessimistic locking

Optimistic locking

Page 8: High Reliability DML and Concurrency Design Patterns for Apex

…or in Salesforce

Two Salespeople receive simultaneous instructions from two different contacts on an opportunity…

This would be very bad

Page 9: High Reliability DML and Concurrency Design Patterns for Apex

…or in Salesforce

Two Salespeople receive simultaneous instructions from two different contacts on an opportunity…

Salesforce detects theother users’ modification

Optimistic locking (so to speak)

APEX DOES NOT DO THIS!

Page 10: High Reliability DML and Concurrency Design Patterns for Apex

…or in Salesforce

Two Salespeople receive simultaneous instructions from two different contacts on an opportunity…

SOQL “For Update” QueryPessimistic locking

Block another thread > 10 seconds = UNABLE_TO_LOCK_ROW error

Page 11: High Reliability DML and Concurrency Design Patterns for Apex

Common Lock Conditions▪ Data skew

▪ Insert or owner change can lock a parent during sharing calculations

▪ After a record update▪ Records have to be locked to allow rollback in case of an exception

▪ “For Update” query▪ Explicit lock

Page 12: High Reliability DML and Concurrency Design Patterns for Apex

Common Lock Solutions▪ Data skew

▪ Reassign record ownership

▪ After a record update▪ Defer record updates to the end of the execution context

▪ “For Update” query▪ Avoid or delay where possible

▪ You cannot absolutely prevent these errors▪ More common on high traffic systems with lots of code

Page 13: High Reliability DML and Concurrency Design Patterns for Apex

Reproducing Lock Errors▪ Use future calls and anonymous Apex

▪ You can simulate them in unit tests

Page 14: High Reliability DML and Concurrency Design Patterns for Apex

Handling Lock Errors▪ In triggers and VisualForce

▪ Let the user deal with them

▪ In asynchronous code▪ Logging▪ Recovery

Page 15: High Reliability DML and Concurrency Design Patterns for Apex

For More InformationAdvanced Apex Programming for Salesforce.com and Force.com – Second Edition

• Incorporates changes through Winter 14• Designing for CPU Time limits• Extended chapter on triggers• Extended chapter on asynchronous patterns• New chapter on concurrency patterns

Page 16: High Reliability DML and Concurrency Design Patterns for Apex

All About Full Circle CRM

Full Circle CRM develops native Salesforce.com applications that enable marketers to accurately track and measure marketing campaign performance directly in Salesforce

▪ Bridge the information gap between leads and contacts

▪ Obtain complete and detailed campaign performance metrics

▪ Develop advanced campaign attribution and influence analytics

Booth 1324 Moscone North

Page 17: High Reliability DML and Concurrency Design Patterns for Apex

Download sample code for this session atwww.AdvancedApex.com/Dreamforce13

Page 18: High Reliability DML and Concurrency Design Patterns for Apex

We want to hear from YOU!

Please take a moment to complete our session survey

Surveys can be found in the “My Agenda” portion of the Dreamforce app

Page 19: High Reliability DML and Concurrency Design Patterns for Apex