Memcache is software that might make your Web site go faster. It is a last resort but something you should look at when you use multiple servers.
You can download memcache from memcached.org. They have some documentation in a wiki but a lot of it is just links elsewhere and the links often end at Flash presentations instead of useful pages. For most hosted Web sites, you either do not want memcache or their is an automated way to install memcache without a download.
What does memcache do? Memcache uses spare memory to store unchanged frequently used data so you do not have to drag that data off disk for every page. The key words are spare, unchanged, and frequently.
Spare memory
If you are visiting my site for the first time, you probably do not have spare memory to waste on memcache. Most computers are sold with less than their maximum memory installed. Long term readers of my articles will already know that you buy all your computers, including servers, with the maximum memory. Memcache might then be able to find some spare memory.
Virtual private Server users will not have spare memory because they will be one of 20 or 50 VPSs trying to squeeze into the available memory.
Pageing is an obsolete operating system idea that slows down your computer by trying to substitute disk for memory. If your Web server shows any sign of paging then you do not have enough memory to run memcache.
Your operating system can use memory to cache directories and frequently used files. If your Web site delivers lots of files, you start by allocating enough memory to the operating system so that directories and files are not dragged in from disk for every page access.
PHP can cache code in APC, saving both time and processing cycles. Expand the APC memory until APC does not have to drop current code through lack of memory.
Your database can cache frequently used queries and the results. Give the database all the memory it can effectively use. in many medium size Web sites, the first expansion step is to move the database from the Web server to a separate server so that the database can have more memory. If you are installing memcache before this step, you are wasting memory.
Memcache might be useful here after you have completed all the above memory allocations.
Unchanged data
The advantage of storing data in a database then retrieving the data from the database is the guarantee that the data is current. The database always gives you the latest data based on completed updates. When your database caches the result of a query, your database immediately removes the result from the cache if any of the data is changed. The database query result cache is good. Memcache does not know when the data changes in the database and is a bad alternative to the database result cache.
Memcache might be useful for storing things not retrieved from your database. Suppose you perform financial transactions using an exchange rate extracted from your back. You have a program that runs in the background and visits the bank Web site every minute to get the current exchange rate. You could store the exchange rate in memcache instead of your database. Memcache is good for that type of storage. Memcache does not have to know if the data is current because the background job updates the data in memcache.
Frequently used
Memory is for frequently used data. If something is used on every Web page or every second, store it in memory. If complex data takes three seconds to compute, you want to compute it once per day, not once per Web page. The cutoff point for frequency depends on the resources used to retrieve the data. Compare tow items. Item A takes 10 seconds to retrieve and is used 1 times per minute. Item B requires only 0.01 seconds to retrieve and is used 1000 times per minute. They both chew up 10 seconds per minute and should have equal access to memory.
The Internet is a cache
Web browsers and proxy servers can cache all sorts of files outside of your Web server, including images, CSS, and Javascript. If these files are frequently accessed from your Web site then you need to fix their HTTP headers instead of placing the files in memcache. The HTTP headers tell the Internet how long you want the files cached and a small mistake can result in a file access every page instead of just once every few hours. Memcache is not a substitute for good use of internet and Web browser cache.
A frequently changing file would not be cached out on the Internet and could be stored on disk then cached in memory by the operating system. The same file could be cached in memcache instead. The only difference is the copy on disk. Do you need to write the file to disk? Do you need to read the file after the Web server is switched off? usually you want the file on test when testing a new Web site so you can diagnose faults after the Web site stops. When a site reaches one hundred percent reliability, you do not need the copy on disk and can save the overhead of writing to disk by using memcache.
All of the above
Did your requirement pass all of the above tests? You allocated the maximum useful memory to everything else and still have memory left unused. You do not need a permanent copy of the data. You do not care about the data changing because the change process can update memcache. Memcache is that last thing you look at and it is starting to look good.
When you use cPanel or equivalent, memcache is just an option you tick on one page and it is installed. Lots of hosting companies will put it in when they set up a server for you. Installation is no big deal. The work is in monitoring the memcache memory usage. the ideal is to have one server monitoring page showing memory usage by paging, your operating system, Apache, APC, your database, and memcache then make sure they all have plenty of memory. If any of those services run out, split your Web site across multiple servers.
Multiple servers
Memcache works across multiple servers and shares one copy of each data item. the sharing can be direct in a single box cluster or across the network in a cluster of separate servers. Memcache across a network is slow because the network is the bottleneck. You can have a second network attached to your servers to share memcache data without the network bottleneck but it is still slower than local data and that second network should be dedicated to the database replication required for multiple copies of the database.
If you have memcache shared across several servers, consider using multiple copies of memcache not shared. Consider moving shred data into the database and using the database result cache to share the data at maximum speed. Again, put memcache down the bottom of the list. Look at everything else first.
Go back to the exchange rate example. Think about a Web site running on ten servers. The exchange rate might be used 100 times per second on each server. It is shared from server 1 across the network to the other servers 900 times per second. The exchange rate is retrieved just once per minute from the bank. You could have a separate copy of the exchange rate on every server and update each server just once per minute. The 100 times per second per server would always be retrieved locally. Instead of 900 network accesses every second, there would be just 9 network accesses each minute.
Conclusion
Memcache is easy to abuse and is no substitute for good design, monitoring memory usage, or fast hardware. Use memcache the way you would use chilli in cooking, add it last and in very small amounts.





- Facebook Like
- Log in or register to post comments