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