Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the memcached server

Tags:

memcached

I'm a beginner in learning memcached. The memcached server confused me most. Can I see it as a single server computer just like web server? I'm also confused about the relationship between memcached server and client, are they located at different computers?

like image 217
Allen Jee Avatar asked May 26 '12 11:05

Allen Jee


2 Answers

I agree with most things @phihag has answered but I must clarify some things.

Memcached stores data according to a key (phihag called it an id, not to be confused with database ids). Data can be of various sizes so you can store small bits (like 1 record pulled from the database) or you can store huge chunks of data (like hundreds of records, or entire finished html pages).

Memcached is not typically used on the same machine as the application server, the reason for this is because it is designed to be used via TCP (it would be accessible via sockets if it were designed to work on the same server) and it was designed as a pooling server.

The pooling part is interesting - you can have 10 machines running Memcached each allocating a maximum 10GB of ram for this purpose. 10*10 = 100GB ram space.

When you write a value into Memcached only one (randomly or via some algorithms) of the servers stores it. When you try to read a value from Memcached only the server that has stored it will send it to you.

So indeed you can put all database/memcached/application/fileserver on the same machine, and typically you do that for you development sandbox. But you also can put each on a separate machine and any other combination of the two.

If you only need one Memcached server you will probably be OK with hosting it on the same machine as the application code.

If you start using a front-end cache server such as varnish or you configure NginX as a front-end cache server, you will have to configure some Memcached servers to store the data that these front-end cache servers are caching.

If you distribute your database into multiple servers and file servers into a CDN, that means that your application handles a lot of data in a short period of time so you'll need a lot of RAM space that couldn't be available in one single application server.

And since extending a memory pool for a Memcached server is as easy as adding the IP of the new server to the list, you will be scaling horizontally as in many servers (which is Memcached's true typical use).

like image 200
Mihai Stancu Avatar answered Oct 01 '22 19:10

Mihai Stancu


The memcached server is a program which manages the data that memcached stores (not to be confused with a machine, which may also be called server). In theory, it can run on any computer. However, it is typically run on the same machine that the main application runs on.

The application then uses its memcached client to talk to the memcached server and ask for cached content. This is faster than querying data from a traditional database because

  1. A memcached server just maps IDs to values, and never needs to scan an entire table
  2. The memcached protocol is simpler. The server doesn't need to parse SQL or so, and the client doesn't need to craft it.
  3. Since memcached does not require the reliability of a database (think of backups, fault isolation, clustering, security etc.), it can be run on the same machine that the application runs on. While you could run a database on the same machine that the applications runs on, doing so is frowned upon for the above reasons.
like image 33
phihag Avatar answered Oct 01 '22 18:10

phihag