How does memcache store data?
Asked Answered
L

3

19

I am a newbie to caching and have no idea how data is stored in caching. I have tried to read a few examples online, but everybody is providing code snippets of storing and getting data, rather than explaining how data is cached using memcache. I have read that it stores data in key, value pairs , but I am unable to understand where are those key-value pairs stored?

Also could someone explain why is data going into cache is hashed or encrypted? I am a little confused between serialising data and hashing data.

Liggett answered 9/12, 2010 at 16:39 Comment(0)
S
23

A couple of quotes from the Memcache page on Wikipedia:

Memcached's APIs provide a giant hash table distributed across multiple machines. When the table is full, subsequent inserts cause older data to be purged in least recently used (LRU) order.

And

The servers keep the values in RAM; if a server runs out of RAM, it discards the oldest values. Therefore, clients must treat Memcached as a transitory cache; they cannot assume that data stored in Memcached is still there when they need it.

The rest of the page on Wikipedia is pretty informative, and it might help you get started.

Sena answered 9/12, 2010 at 16:55 Comment(1)
What you might be calling "encrypted" could be a result of the Binary Protocol: code.google.com/p/memcached/wiki/MemcacheBinaryProtocolSena
N
2

They are stored in memory on the server, that way if you use the same key/value often and you know they won't change for a while you can store them in memory for faster access.

Nephograph answered 9/12, 2010 at 16:43 Comment(1)
ok to be a little specific, would those key-value pairs be stored on the ram of the server or would it be on the rom?? Please do not think differently, I am just trying to get this thing out of my head, and am not testing anyone.Liggett
G
2

I'm not deeply familiar with memcached, so take what I have to say with a grain of salt :-)

Memcached is a separate process or set of processes that store a key-value store in-memory so they can be easily accessed later. In a sense, they provide another global scope that can be shared by different aspects of your program, enabling a value to be calculated once, and used in many distinct and separate areas of your program. In another sense, they provide a fast, forgetful database that can be used to store transient data. The data is not stored permanently, but in general it will be stored beyond the life of a particular request (it is possible for Memcached to never store your data, so every read will be a miss, but that's generally an indication that you do not have it set up correctly for your use case).

The data going into cache does not have to be hashed or encrypted (but both things can happen to the data, depending on the caching mechanism.)

Serializing data actually has nothing to do with either concept -- instead, it is the process of changing data from one format (generally one suited for in-memory storage) to another one (generally suitable for storage in a persistent medium.) Another term for this process is marshalling and unmarshalling.

Greatniece answered 9/12, 2010 at 16:49 Comment(2)
-1 because the Wikipedia page for Memcached says: "Therefore, clients must treat Memcached as a transitory cache; they cannot assume that data stored in Memcached is still there when they need it."Griqua
@LasseBunk - thanks for the comment along with the vote :-) - I have tweaked the answer to clearly indicate that Memcached is a transient data store. Let me know if it can be improved more!Greatniece

© 2022 - 2024 — McMap. All rights reserved.