ArticleslgStudy

computer science

Memcached

Memcached is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Memcached rather than just read about it. In short: In computing, Memcached (pronounced either /mɛmkæʃˈdiː/ mem-cash-DEE ["dee/d" being for daemon], or /ˈmɛmkæʃt/ MEM-cashed) is a general-purpose distributed memory-caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read.

Memcached — main illustration
Memcached — illustration

Key takeaways

  • Memcached belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Memcached to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Memcached from memory before moving on to harder problems.

Reference excerpt

In computing, Memcached (pronounced either /mɛmkæʃˈdiː/ mem-cash-DEE ["dee/d" being for daemon], or /ˈmɛmkæʃt/ MEM-cashed) is a general-purpose distributed memory-caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read. Memcached is free and open-source software, licensed under the Revised BSD license. Memcached runs on Unix-like operating systems (Linux and macOS) and on Microsoft Windows. It depends on the libevent library. Memcached's APIs provide a large 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. Applications using Memcached typically layer requests and additions into RAM before falling back on a slower backing store, such as a database. Memcached has no internal mechanism to track misses that may happen. However, some third-party utilities provide this functionality. Memcached was first developed by Brad Fitzpatrick for his website LiveJournal, on May 22, 2003. It was originally written in Perl, then later rewritten in C by Anatoly Vorobey, who was employed by LiveJournal at the time. Memcached is now used by many other systems, including YouTube, Reddit, Facebook, Pinterest, Twitter, Wikipedia, and Method Studios. Google App Engine, Google Cloud Platform, Microsoft Azure, IBM Bluemix and Amazon Web Services also offer a Memcached service through an API.

Software architecture

The system uses a client–server architecture. The servers maintain a key–value associative array; the clients populate this array and query it by key. Keys are up to 250 bytes long and values can be at most 1 megabyte in size. Clients use client-side libraries to contact the servers, which, by default, expose their service at port 11211. Both TCP and UDP are supported. Each client knows all servers; the servers do not communicate with each other. If a client wishes to set or read the value corresponding to a certain key, the client's library first computes a hash of the key to determine which server to use. This gives a simple form of sharding and scalable shared-nothing architecture across the servers. The server computes a second hash of the key to determine where to store or read the corresponding value. The servers keep the values in RAM (and, starting in 1.6.0, in auxiliary cache on disk using an external storage server option); if a server runs out of available memory or disk, 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. Other databases, such as MemcacheDB and Couchbase Server, provide persistent storage while maintaining Memcached protocol compatibility. If all client libraries use the same hashing algorithm to determine servers, then clients can read each other's cached data. A typical deployment has several servers and many clients. However, it is possible to use Memcached on a single computer, acting simultaneously as client and server. The size of its hash table is often very large. It is limited to the available memory across all the servers in the cluster of servers in a data center. Where high-volume, wide-audience Web publishing requires it, this may stretch to many gigabytes. Memcached can be equally valuable for situations where either the number of requests for content is high, or the cost of generating a particular piece of content is high. Applications with particularly high-demand caching needs can use a built-in proxy to define and configure complex client-server routes.

Security Most deployments of Memcached are within trusted networks where clients may freely connect to any server. However, sometimes Memcached is deployed in untrusted networks or where administrators want to exercise control over the clients that are connecting. For this purpose, Memcached can be compiled with optional SASL authentication support. The SASL support requires the binary protocol. A presentation at BlackHat USA 2010 revealed that a number of large public websites had left Memcached open to inspection, analysis, retrieval, and modification of data. Even within a trusted organisation, the flat trust model of memcached may have security implications. For efficient simplicity, all Memcached operations are treated equally. Clients with a valid need for access to low-security entries within the cache gain access to all entries within the cache, even when these are higher-security and that client has no justifiable need for them. If the cache key can be predicted, guessed, or found by exhaustive searching, its cache entry may be retrieved. Some attempts to isolate the setting and reading data may be made in situations such as high-volume web publishing. A farm of outward-facing content servers has read access to memcached containing published pages or page components, but no write access. Where new content is published (and is not yet in memcached), a request is instead sent to content generation servers that are not publicly accessible to create the content unit and add it to memcached. The content server then retries to retrieve it and serve it outwards.

Used as a DDoS attack vector In February 2018, CloudFlare reported that misconfigured memcached servers were used to launch DDoS attacks in large scale. The memcached protocol over UDP has a huge amplification factor, of more than 51000. Victims of the DDoS attacks include GitHub, which was flooded with 1.35 Tbit/s peak incoming traffic. This issue was mitigated in Memcached version 1.5.6, which disabled UDP protocol by default.

Example code Note that all functions described on this page are pseudocode only. Memcached calls and programming languages may vary based on the API used. Converting database or object creation queries to use Memcached is simple. Typically, when using straight database queries, example code would be as follows:

After conversion to Memcached, the same call might look like the following

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Memcached

Start with the simplest possible case. Write down what Memcached claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Memcached before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Memcached ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Memcached

In research
Memcached appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Memcached in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Memcached is common in secondary-school and first-year university syllabi. It links to neighbouring topics 2003 software, Cross-platform software, Database caching, so understanding it makes those chapters shorter.
In everyday life
Look for Memcached outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Memcached” →

How to study Memcached in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Memcached means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Memcached out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Memcached in simple terms?

In computing, Memcached (pronounced either /mɛmkæʃˈdiː/ mem-cash-DEE ["dee/d" being for daemon], or /ˈmɛmkæʃt/ MEM-cashed) is a general-purpose distributed memory-caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of…

Why does Memcached matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Memcached?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Memcached.

Tags

  • 2003 software
  • Cross-platform software
  • Database caching
  • Free memory management software
  • Key-value databases
  • Software using the BSD license
  • Structured storage

Keep exploring