Cache Layer

June 11, 2010

Cache Layer


Lately I found myself in some architecture consulting sessions at
some customers. In every one of those customers I found myself
explaining how to implement a cache layer in order to decrease
the amount of round trips to the database and for better scalability.
In this post I’ll try to explain in high level how to build a cache layer.


Deciding to Build a Cache Layer


Every application that performance is important to its developers
and managers must contain some sort of caching. The cache is a very
fast in memory resources container which holds relevant data close to the
application itself. I explained in the past the candidate resource types
for caching in this post. So you have decide to implement caching
in your application, what next?
The answer is simple – create a cache layer.


Cache Layer Position in Application Architecture


The next thing to understand is the location of the cache layer in your
application. The next diagram shows were you should place the cache layer:
Cache Layer
As you can see the cache layer‘s best place is between the business
logic layer and the data access layer. The business logic responsibility
will be to check whether an entity or some data exists in the cache
and then if not to make a round trip to the database (using the data
access layer of course) and bring the data to application. When the data
exists we first put it in the cache and then we have the data in hand
and also it exists our the cache. This is called the cache retrieval pattern.
There are many ways to create a cache layer for example by using
distributed cache like AppFabric caching services, Enterprise Library
Caching Application Block, ASP.NET caching and many more cache services.
It’s your responsibility to evaluate which cache technology to use in your
application by answering to your application requirements.


Summary


Caching in applications is a very vital way to improve the application
performance and make the application more scalable. It’s our
responsibility to create a cache layer which will exists between
your business logic and data access layer. By using the cache retrieval
pattern
you will be able to use your cache in a very efficient way.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

2 comments

  1. DimkinJuly 3, 2010 ב 13:09

    I was wondering if maybe it would be better to place the cache layer as part of (or parallel to) the DAL (data access layer)and give the DAL the responsibility to fill and check the cache. This way, the one who implements the BLL (business logic layer) won’t have to worry about where to take the data from?

    Reply
  2. Gil FinkJuly 3, 2010 ב 13:40

    @Dimkin,

    The cache layer location is between the BL and the DAL. When you go and implement it you can put the check in the DAL before a retrieval method is called. Another implementation could be by puting the cache logic in the BL. In this case the BL’s responsibility is to check the cache. There are cons and pros for every one of these methods.

    Reply