Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Platform cache

408 views

Published on

Boost your App with Platform Cache

Published in: Education
  • Be the first to comment

Platform cache

  1. 1. #SalesforceApexHourswww.ApexHours.com Salesforce Apex Hours Farmington Hill Salesforce Developer Group Boost your App with Platform Cache #SalesforceApexHours #SalesforceApexHours Speaker Date Venue/Link Daniel Stange Saturday, January 04, 2020 10:00 AM EST ( 8:30 PM IST ) https://zoom.us/j/476658815 www.apexhours.com
  2. 2. #SalesforceApexHourswww.ApexHours.com Who I am Amit Chaudhary (Salesforce MVP) • Active on Salesforce Developer Community • Blogging at http://amitsalesforce.blogspot.in/ • Co-Organizer of – FarmingtonHillsSFDCDug • Founder of www.ApexHours.com • Follow us @Amit_SFDC or @ApexHours
  3. 3. #SalesforceApexHourswww.ApexHours.com Upcoming Session 1. OAuth Concepts on JAN 11, 2020 10:00 AM EST (8:30 PM IST) with Susannah Kate St-Germain. 2. Become an Order of Execution Hero on JAN 18, 2020 10:00 AM EST (8:30 PM IST) with Daniel Stange and Marc B. Kirsch 3. Salesforce integration Pattern & Best Practices on FEB 1, 2019 10:00 AM EST (8:30 PM IST) with Jitendra Zaa 4. Microservices in Salesforce on FEB 22, 2020 10:00 AM EST (8:30 PM IST) with Jigar Shah All Upcoming session : http://www.apexhours.com/sessions-in-2020/
  4. 4. #SalesforceApexHourswww.ApexHours.com Our Speaker Daniel Stange Technical Architect DIA die.interaktiven 20x certified System & Application Architect Frankfurt User Group Leader @stangomat linked.in/daniel-stange github.com/dstdia
  5. 5. #SalesforceApexHourswww.ApexHours.com Agenda • Platform Cache - But why? • Key Concepts • Getting Started with Platform Cache (Demo / Code!) • Platform Cache as a key/value storage • Platform Cache as a readthrough cache • When to Use (and when not to use) Platform Cache • Q & A
  6. 6. #SalesforceApexHourswww.ApexHours.com Key Takeaways • Boost your app‘s performance significantly • Understand which kind of data to put in the cache • Know the obstacles when you use to the platform cache and prepare for cache misses and unexpected results
  7. 7. #SalesforceApexHourswww.ApexHours.com Platform Cache? But why?
  8. 8. #SalesforceApexHourswww.ApexHours.com Impact for read access to data (EE Scratch Org on CS83 / EE Dev Sandbox on CS86) Avg. to fetch 10000 records as a Map through SOQL 182-370ms 1.8 to 5.7x Faster Access 65-97ms Avg. to fetch the same map from the Org cache
  9. 9. #SalesforceApexHourswww.ApexHours.com DB vs. Cache Retrieve / per Item Impact of bulkification (100 retrieved values) DB: Initial 367ms => 3,67ms per item access Cache: Initial 117ms => 1,17ms per item access DB: avg. 11,52ms Cache: avg. 3.67ms
  10. 10. #SalesforceApexHourswww.ApexHours.com Other Gains • Handle Limits better when accessing frequently used data • Speed up data access (2x to 3x faster than Database access) • Reduce SOQL calls (access is „free“) • Reduce memory footprint • Trade-Offs: • Heap size limit (when working with maps in variables) • Cache access time (slower than accessing variables) • Less control over SOQL calls (if implemented for cache misses)
  11. 11. #SalesforceApexHourswww.ApexHours.com Big News for ISVs
  12. 12. #SalesforceApexHourswww.ApexHours.com Key Concepts
  13. 13. #SalesforceApexHourswww.ApexHours.com Platform Cache • Simple & temporary Key-Value-Storage (Map<String, Object>) • Quite Easy to implement ANY cache strategy => but fairly easy to design a GOOD, sustainable strategy • By default VISIBLE and MUTABLE => but can be RESTRICTED and set to IMMUTABLE
  14. 14. #SalesforceApexHourswww.ApexHours.com Data to Cache • PREPARABLE - REUSABLE • Static: Not changing frequently • Frequently needed in operations • Expensive to get (or keep) (in terms of system limits) • e.g. Taxonomies, Schedules, Mappings, Conversion Rates, etc.
  15. 15. #SalesforceApexHourswww.ApexHours.com Cache Types ORG one cache for all users and contexts variable TTL SESSION scoped per user Session TTL = Session length TTL: Time to Live
  16. 16. #SalesforceApexHourswww.ApexHours.com ORG: 24 hrs DEFAULT TTL Time-to-Live (TTL) ORG: 5 min MINIMUM TTL SESSION: 5 min ORG: 48 hrs MAXIMUM TTL SESSION: 8 hrs
  17. 17. #SalesforceApexHourswww.ApexHours.com Cache Eviction Strategy 1 4 3 7 5 6 ORDER OF LAST ACCESS 2 CACHE CAPACITY LRU: Least Recently Used Eviction Strategy
  18. 18. #SalesforceApexHourswww.ApexHours.com Considerations How (and when) to build / maintain your cache • Explicit Caching • Button: “Rebuild the Cache now“ • Scheduled Apex: „Rebuild the Cache every night at 1 a.m.“ • Implicit Caching • Eagerly: „Upon a Cache Miss, cache all items again“ • Lazy: „Upon a Cache Miss, cache the missing item again“
  19. 19. #SalesforceApexHourswww.ApexHours.com Considerations Invalidation • Requires careful consideration • Platform data vs. off-platform data • Frequency of change • Ability to „listen“ to change events • Strategies: • Rebuild • Evict / Replace • Combinations
  20. 20. #SalesforceApexHourswww.ApexHours.com Digging Into the Platform Cache
  21. 21. #SalesforceApexHourswww.ApexHours.com Preparation Work • Enterprise Editions Sandboxs come with a 10 MB capacity • Request a trial capacity if you are using a Developer Org or Scratch Org • SFDX / Scratch Orgs: "features": ["PlatformCache"] • Trailhead: “Platform Cache Basics” bit.ly/cachetrail
  22. 22. #SalesforceApexHourswww.ApexHours.com Place your screenshot here Cache .[Org/Session] .put( Namespace .PartitionName .Key, Object ); Store
  23. 23. #SalesforceApexHourswww.ApexHours.com Place your screenshot here Retrieve Cache .[Org/Session] .get( Namespace .PartitionName .Key );
  24. 24. #SalesforceApexHourswww.ApexHours.com Demo Sample Implementation and Benchmark Scripts at https://github.com/dstdia/PlatformCache
  25. 25. #SalesforceApexHourswww.ApexHours.com Implementation Catch miss Load from source Check for key Store Key / Value Retrieve Value Return Value
  26. 26. #SalesforceApexHourswww.ApexHours.com CacheBuilder Interface Catch miss Load from source Check for key Store Key / Value Retrieve Value Return Value
  27. 27. #SalesforceApexHourswww.ApexHours.com Two Implementation Paths • READ-THROUGH IMPLEMENTATION using CACHEBUILDER interface • Data sits in sObjects • Access can be expressed in one SOQL query • CUSTOM IMPLEMENTATION • Any off-platform data • Cross-object data that can‘t be accessed through one specific SOQL query
  28. 28. #SalesforceApexHourswww.ApexHours.com Place your screenshot here Sample Sales District Everything with Country = DE and PostalCode LIKE ‘35%’ should be assigned and have Nils as an Owner. https://github.com/ dstdia/PlatformCache
  29. 29. #SalesforceApexHourswww.ApexHours.com Place your screenshot here Creating an Account Postal Code starts with 35 and Country = DE Should end up in Central Sample District being assigned https://github.com/ dstdia/PlatformCache
  30. 30. #SalesforceApexHourswww.ApexHours.com Place your screenshot here And there it is! https://github.com/ dstdia/PlatformCache
  31. 31. #SalesforceApexHourswww.ApexHours.com https://github.com/ dstdia/PlatformCache
  32. 32. #SalesforceApexHourswww.ApexHours.com https://github.com/ dstdia/PlatformCache
  33. 33. #SalesforceApexHourswww.ApexHours.com Read-Through Cache
  34. 34. #SalesforceApexHourswww.ApexHours.com Custom Implementation • Design a way to POPULATE the cache • Design a way to manage changes in the cached data (INVALIDATE) • Design the request to the cache • Catch cache misses • Handle misses gracefully (load data, put into cache, return)
  35. 35. #SalesforceApexHourswww.ApexHours.com „Read Through" • Implement the standard Interface Cache.CacheBuilder • Implement the doLoad() method: • Design a Query to retrieve a record ... sd = (SalesDistrict__c) Cache.Org.get( Cache_SalesDistrict.class, salesDistrictKey); ...
  36. 36. #SalesforceApexHourswww.ApexHours.com https://github.com/ dstdia/PlatformCache
  37. 37. #SalesforceApexHourswww.ApexHours.com Considerations
  38. 38. #SalesforceApexHourswww.ApexHours.com Nice! Easy! Fast! Where‘s the catch? • It‘s still a cache, not a database. • It is short lived and per default visible and mutable • Understand the key concepts of the platform cache (in particular TTL, LRU) • Expect the cache to fail you. • What’s your strategy to invalidate cache?
  39. 39. #SalesforceApexHourswww.ApexHours.com Build your Strategies • The Cache will go away... Schedule a Cache Rebuild < TTL • Cached items will be pushed out… Use the CacheBuilder interface • Cached data goes stale… Rebuild From Triggers • Cached items must adhere to the 100kB size limits Reduce memory footprint by using Apex Classes instead of sObject or reduce the number of queried fields
  40. 40. #SalesforceApexHourswww.ApexHours.com Apex Classes vs. sObjects List<CachedProduct> cachedProducts; public class CachedProduct { String name; String productCode; Decimal leadTime; } List<Product2> products = [SELECT Name, ProductCode, LeadTime FROM Product2];
  41. 41. #SalesforceApexHourswww.ApexHours.com Size matters No cached item may exceed 100kB
  42. 42. #SalesforceApexHourswww.ApexHours.com Size matters 100kB Cache storage equals 12.5k objects same list, but stored in an Apex defined type 9k sObject records Product2 records with Id, Name, ProductCode queried
  43. 43. #SalesforceApexHourswww.ApexHours.com Repeat after me… :-) • Don‘t use the cache as a fast, limit-free database. • Don‘t use it as temporary storage for transactional data • No items larger than 100kB • Cached Items are not persisted – don‘t rely on them being there. • Unless flagged as immutable, don’t rely on the integrity of cached items
  44. 44. #SalesforceApexHourswww.ApexHours.com Best Practices • Cache lists or maps of objects rather than single objects. Tradeoff: Better performance of fewer, larger operations vs. 100kB item size limit • Use a wrapper class to reduce sObject overhead • Use the fully qualified name of your cache partition • Consider using the ‘immutable‘ flag and the visibility enum
  45. 45. #SalesforceApexHourswww.ApexHours.com Further Reading • Developer Documentation: bit.ly/cachedoc • Trailhead: bit.ly/cachetrail • Session Code: github.com/dstdia/PlatformCache/ • Keir Bowden’s blog post: bit.ly/cache-buzzard • Josh Kaplan‘s blog post: bit.ly/cache-sfblog • Amit Chaudhary: http://bit.ly/cache-amit
  46. 46. #SalesforceApexHourswww.ApexHours.com Q&A Connect with me • @stangomat • Daniel.stange@die.interaktiven.de • github.com/dstdia Sample Code from the session: https://github.com/dstdia/PlatformCache
  47. 47. #SalesforceApexHourswww.ApexHours.com Thank you! Q & A
  48. 48. #SalesforceApexHourswww.ApexHours.com Heading text goes here #SalesforceApexHours @ApexHours www.ApexHours.com https://trailblazercommunitygroups.com/farmingto n-mi-developers-group/ https://www.youtube.com/channel/UChTdRj6Yfwq hR_WEFepkcJw/videos https://www.facebook.com/FarmingtonHillsSfdcdu g/?ref=bookmarks

×