Question

I have a dynamic php (Yii framework based) site. User has to login to do anything on the site. I am trying to understand how caching and CDN work; and I am a bit confused.

Caching (memcache):

My site has a good amount of css, js, and images. I've been given to understand that enabling caching ("memcache"?) will GREATLY speed up my site. But this has me confused. How does caching help? I mean, how can you cache something that's coming out of DB for each user separately? For instance, user-1 logs-in, he sees his control panel. User-2 logs-in, user 2 will see their control panel.

How do I determine what to cache? Plus, how do I enable caching (memcaching)?

CDN:

I have been told to use a content delivery network like CloudFlare. It is suppose to automatically cache my site. So, when my user-1 logs in, what will it cache? Will it cache only the homepage CSS, JS, and homepage images? Because everything else requires login? What happens when user logs-out? I mean, do "sessions" interfere with working of a CDN?

Does serving up images via CDN reduce significant load on my server? I don't have much cash for getting a clustered-server configuration. So, I just want my (shared) server to be able to devote all its resources in processing PHP code. So, how much load can I save by using "caching" (something like memcache) and/or "CDN" (something like CloudFlare)?

Finally,

What would be general strategy to implement in this scenario for caching, cdn, and basic performance optimization? do I need to make any changes to my php-code to enable CDN like CloudFlare and to enable/implement/configure caching? What can I do that would take least amount of developer/coding time and will make my site run much much faster?

Oh wait, some of my pages like "about us" page etc. are going to be static html too. But they won't get as many hits. Except for maybe the iFrame page that will be used for my Facebook Page.

Was it helpful?

Solution

Caching helps because instead of performing disk io for each user the data is stored in the memory, ie memcached. This provides a SIGNIFICANT increase in performance.

Memcache is generally used for cacheing data ie query results. http://pureform.wordpress.com/2008/05/21/using-memcache-with-mysql-and-php/ There are lots of tutorials.

I have only ever used amazon s3 which is is not quite a cdn. It is more of a storage platform but still it helps to take the load off of my own servers when serving media.

I would put all of your static resources on a CDN so your own server would not have to serve these. It would not require any modifcation to your php code. This includes JS, and CSS. For your static pages (your about page) I'd make sure that php isn't processing that since there is no reason for it. Your web server should serve it directly.

Cacheing will require changes to your code. For cacheing a normal flow is: 1) user makes a request 2) check if data is in cache 3) if it is not in cache do the DB query and put it in cache 4) if it is in cache retrieve it 5) return data.

You can cache anything that requires disk io and you should see a speed up.

OTHER TIPS

I actually work for CloudFlare & thought I would hop in to address some of the concerns.

"do I need to make any changes to my php-code to enable CDN like CloudFlare and to enable/implement/configure caching? What can I do that would take least amount of developer/coding time and will make my site run much much faster?"

No, nothing like a need to re-write urls, etc. We automatically cache static content by file extension. This does require changing your DNS to point to us, however.

Does serving up images via CDN reduce significant load on my server?

Yes, and it should also help most visitors access the site faster and save you a fair amount on bandwidth.

"Oh wait, some of my pages like "about us" page etc. are going to be static html too."

CloudFlare doesn't cache HTML by default. You use PageRules to setup more advanced caching options for things like static HTML.

Memcached works by storing database information (usually from a remote server or even a database engine on the same server) in a flat file format in the filesystem of the web server. Accessing a flat file directly to retrieve data stored in a regulated format is much much muuuuuch faster than accessing that data from a remote query each time. This is typically useful when you have data that can be safely stored for certain periods of time as it is not subject to regular changes.

The way this works is that if you want to store a user's account information in a cache to speed up loading pages where that user is logged in. You would load the information and cache it locally. On any subsequent requests for that data, it will load in a fraction of the time it normally would take to load that information from the database itself. Obviously you will need to make sure that you update/recache that information if the user changes it while logged in, but you will greatly reduce the time it takes to serve up pages if you implement a caching system that can minimize the time spent waiting on the database.

I'm personally not familiar with CloudFlare so I can't offer any advice to that effect, but in terms of implementing caching in your application, you should check out:

http://code.google.com/p/memcached/wiki/NewOverview

And read the rest of the Wiki entries there which cover installation/implementation/etc. That should get you started on the right track.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top