caching business logic in the database

27
Caching Business Logic in the Database by Jonathan Levin

Upload: jonathan-levin

Post on 03-Jul-2015

1.381 views

Category:

Technology


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Caching Business Logic in the Database

Caching Business Logic in the Database

by Jonathan Levin

Page 2: Caching Business Logic in the Database

Typical Scaling Problem

DatabaseWeb Server

ServerServer

Page 3: Caching Business Logic in the Database

Typical Scaling Problem

DatabaseWeb Server

ServerServer

Too Many Visitors

Page 4: Caching Business Logic in the Database

Typical Scaling Problem

DatabaseWeb Server

Server 1Server 1Server 2Server 2

Page 5: Caching Business Logic in the Database

Typical Scaling Problem

DatabaseWeb Server

Server 1Server 1Server 2Server 2

Slow Network

Page 6: Caching Business Logic in the Database

Typical Scaling Problem

DatabaseWeb Server

Server 1Server 1Server 2Server 2

Page 7: Caching Business Logic in the Database

Typical Scaling Problem

DatabaseWeb Server

Server 1Server 1Server 2Server 2

Slow Processor

Page 8: Caching Business Logic in the Database

Typical Scaling Problem

Database

Web Server

Server 1Server 1Server 2Server 2

Web Server

Server 3Server 3

Page 9: Caching Business Logic in the Database

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.

Page 10: Caching Business Logic in the Database

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

Page 11: Caching Business Logic in the Database

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.

Page 12: Caching Business Logic in the Database

Explaining with an Example

This takes time and server resources to keep producing over and over again.

But what if we have….?

Page 13: Caching Business Logic in the Database

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)

Page 14: Caching Business Logic in the Database

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)

Page 15: Caching Business Logic in the Database

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

Page 16: Caching Business Logic in the Database

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

Page 17: Caching Business Logic in the 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?

Page 18: Caching Business Logic in the Database

Order Class Example

Page 19: Caching Business Logic in the Database

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?

Page 20: Caching Business Logic in the Database

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?

Page 21: Caching Business Logic in the Database

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?

Page 22: Caching Business Logic in the Database

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?

Page 23: Caching Business Logic in the Database

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.

Page 24: Caching Business Logic in the Database

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.

Page 25: Caching Business Logic in 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

Page 26: Caching Business Logic in the Database

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)

Page 27: Caching Business Logic in the Database

The End

Q&AThank you

(and I hope you found this presentation interesting)