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?


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).

  • 2
    Amazing answer. It really helps me a lot. Thank you and also @phihag. – Allen Jee May 26 '12 at 14:38
  • @AllenJee could you please mark the answer as accepted? – Mihai Stancu Oct 3 '13 at 23:55
  • how does Memcached server backup its data? – trillions Mar 7 '16 at 22:25
  • It doesn't. Cache servers usually don't have a "backup" feature. – Mihai Stancu Mar 8 '16 at 6:50
  • Your application should expect cache-miss scenarios (if some value is not in the cache the application will rebuild it). – Mihai Stancu Mar 8 '16 at 6:52

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.
  • 3
    Thanks very much! The application talks to its memcached client, then the client ask for the memcached server to get data. Typically, the application, memcached client and memcached server are on the same machine. And different memcached clients can communicate with other memcached servers which are running on different machines according to their protocol. Is the above right? – Allen Jee May 26 '12 at 12:07
  • 1
    Yes, that's precisely it. – phihag May 26 '12 at 12:16
  • 1
    Can I divide them into two parts? One is the web server which includes the application, memcached client and memcached server. The other is the db server. – Allen Jee May 26 '12 at 12:23
  • Exactly, that's the basic 2-tier setup. Larger services usually also have a proxying web servers in front of the application that serves static resources and does load balancing. Smaller services can also run everything on just one machine. – phihag May 26 '12 at 12:42

Your Answer

