Caching Business Logic in the Database

1,727 views

Published on

A presentation give in the Cloud Expo Europe on the 21st of May 2009

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,727
On SlideShare
0
From Embeds
0
Number of Embeds
918
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Caching Business Logic in the Database

  1. 1. Caching Business Logic in the Database by Jonathan Levin
  2. 2. Typical Scaling Problem Database Web Server Server
  3. 3. Typical Scaling Problem Database Web Server Server Too Many Visitors
  4. 4. Typical Scaling Problem Database Web Server Server 1 Server 2
  5. 5. Typical Scaling Problem Database Web Server Server 1 Server 2 Slow Network
  6. 6. Typical Scaling Problem Database Web Server Server 1 Server 2
  7. 7. Typical Scaling Problem Database Web Server Server 1 Server 2 Slow Processor
  8. 8. Typical Scaling Problem Database Web Server Server 1 Server 2 Web Server Server 3
  9. 9. So what is it? <ul><li>Caching Business Logic : </li></ul><ul><ul><li>Caching the results of processed </li></ul></ul><ul><ul><li>data - data that had business logic </li></ul></ul><ul><ul><li>applied to it - to the database to be </li></ul></ul><ul><ul><li>re-used later. </li></ul></ul>
  10. 10. Explaining with an Example <ul><li>A website that sells products. </li></ul><ul><li>Every time the main page loads the web server calculates which products are: </li></ul><ul><ul><li>Visible (not discontinued) </li></ul></ul><ul><ul><li>Have discounts </li></ul></ul><ul><ul><li>Seasonal (for the current season) </li></ul></ul><ul><ul><li>Top Sellers </li></ul></ul>
  11. 11. Explaining with an Example <ul><li>It does this by looking at 4 different lists : </li></ul><ul><ul><li>Discontinued products list </li></ul></ul><ul><ul><li>Current list of discounted products </li></ul></ul><ul><ul><li>Current season’s list of seasonal products </li></ul></ul><ul><ul><li>Calculate the total of all products that were sold in the last week and organize them to find the top sellers. </li></ul></ul>
  12. 12. Explaining with an Example This takes time and server resources to keep producing over and over again. But what if we have….?
  13. 13. Explaining with an Example <ul><li>A different dataset to search from. </li></ul><ul><li>What if this was the way our products table looked like: </li></ul><ul><ul><li>Product ID </li></ul></ul><ul><ul><li>Product Description </li></ul></ul><ul><ul><li>Visible (True/False) </li></ul></ul><ul><ul><li>Discounted (True/False) </li></ul></ul><ul><ul><li>Seasonal (True/False) </li></ul></ul><ul><ul><li>Top Seller (True/False) </li></ul></ul>
  14. 14. Explaining with an Example <ul><li>A different dataset to search from. </li></ul><ul><li>What if this was the way our products table looked like: </li></ul><ul><ul><li>Product ID </li></ul></ul><ul><ul><li>Product Description </li></ul></ul><ul><ul><li>Visible (True/False) </li></ul></ul><ul><ul><li>Discounted (True/False) </li></ul></ul><ul><ul><li>Seasonal (True/False) </li></ul></ul><ul><ul><li>Top Seller (True/False) </li></ul></ul>
  15. 15. What are the Benefits? <ul><li>Its Green </li></ul><ul><li>Less CPU usage </li></ul><ul><li>Less electricity </li></ul><ul><li>Less hardware “wear and tear” </li></ul><ul><li>or </li></ul><ul><li>Less cloud-bills </li></ul><ul><li>Quick Response Time </li></ul><ul><li>Process once - Read lots </li></ul><ul><li>Scaling </li></ul><ul><li>Scale with the same hardware/virtual instance </li></ul>
  16. 16. How to do it? <ul><li>1) Get data from the database </li></ul><ul><li>2) Process it to apply business logic </li></ul><ul><li>3) Cache the results back to the database </li></ul><ul><li>4) Re-use the results when needed </li></ul><ul><li>5) Refresh accordingly </li></ul>Database
  17. 17. Order Class Example <ul><li>Our accounting department would like to know for each order: </li></ul><ul><ul><li>Has the order been paid? </li></ul></ul><ul><ul><li>Is the order outstanding – hasn’t been paid for over 30 days? </li></ul></ul>
  18. 18. Order Class Example
  19. 19. What to Cache? <ul><li>The caching can be more effective if you know how you will use your data. </li></ul><ul><ul><li>Tell the database how you plan to use it </li></ul></ul><ul><li>What are the right questions to ask from your data? </li></ul>
  20. 20. Caching Examples <ul><li>Orders: </li></ul><ul><ul><li>Has the order been paid? </li></ul></ul><ul><ul><li>Has the order been dispatched? </li></ul></ul><ul><ul><li>Is this order a promotional order? </li></ul></ul><ul><ul><li>Is this a big order? </li></ul></ul><ul><ul><li>Is this order a small order? </li></ul></ul>
  21. 21. Caching Examples <ul><li>Calendar (per day) </li></ul><ul><ul><li>Is this day a holiday? </li></ul></ul><ul><ul><li>Do we have a promotion on this day? </li></ul></ul><ul><ul><li>Is this day in a certain season? </li></ul></ul><ul><ul><li>What are the total sales for this day? </li></ul></ul><ul><ul><li>Where the sales for this day good, bad or average? </li></ul></ul>
  22. 22. Caching Examples <ul><li>User Logins </li></ul><ul><ul><li>Has this user bought a product from the website? </li></ul></ul><ul><ul><li>How many pages did they view? </li></ul></ul><ul><ul><li>Which landing page did the user arrive on? </li></ul></ul>
  23. 23. Caching Example <ul><li>Sales Person (incentive scheme) </li></ul><ul><ul><li>How much did the sales person sell on that day? </li></ul></ul><ul><ul><li>Did they sell more then average on that day? </li></ul></ul><ul><li>If the sales person performed well for a whole month, then give them a bonus. </li></ul><ul><li>If the sales person performed well for 1 year, then promote them. </li></ul>
  24. 24. How to Refresh the Cache <ul><li>Application code vs SQL/Triggers </li></ul><ul><li>Process on Insert/Update vs Scheduled Jobs </li></ul><ul><li>Mix and match code/languages/services to suit your needs - as long as the data ends up the database. </li></ul>
  25. 25. Some More Points <ul><li>Database will become a more holistic part of your application </li></ul><ul><li>Reusable code vs reusable data </li></ul><ul><ul><li>Order class can be used by </li></ul></ul><ul><ul><ul><li>website’s MyAccount page </li></ul></ul></ul><ul><ul><ul><li>Accounting department </li></ul></ul></ul>
  26. 26. Some More Points <ul><li>Generates reports quickly and easily </li></ul><ul><ul><li>BI for the future </li></ul></ul><ul><li>Maintainability </li></ul><ul><ul><li>Humans understand data structures better then code (Unix philosophy - Rule of Representation) </li></ul></ul>
  27. 27. The End <ul><li>Q&A </li></ul><ul><li>Thank you </li></ul><ul><li>(and I hope you found this presentation interesting) </li></ul>

×