Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 37 ยท Getting Started

What Is Memcached?

Learn what Memcached is, what problem it solves, how a cache sits between your app and its database, and what this course will and will not run in your browser.

Most web applications spend their time waiting. They wait for a database to answer a query, for another service to return JSON, or for a template to render. If a thousand visitors ask for the same product page in one minute, doing that work a thousand times is wasteful, because the answer is the same every time.

Memcached is a small, very fast server that keeps data in memory so you can skip repeated work. You hand it a key and a value; later you ask for the key and get the value back in well under a millisecond. It is one of the oldest and most widely deployed caches on the internet, and its simplicity is the reason it has lasted.

A cache in one picture

Your application asks Memcached first. If the answer is there (a hit), you are done. If not (a miss), you do the slow work, then put the answer in Memcached for next time.

flowchart LR
  A["Application"] -->|"1. get key"| M["Memcached<br/>(RAM)"]
  M -->|"2a. hit: value"| A
  M -.->|"2b. miss"| A
  A -->|"3. slow query"| D["Database"]
  D -->|"4. rows"| A
  A -->|"5. set key"| M

The application, not Memcached, decides what to cache and how to fill it after a miss.

Memcached never talks to your database. It does not know what a "user" or a "product" is. It stores opaque blobs of bytes under string keys, and that is the whole idea.

What Memcached is

  • An in-memory key-value store. All data lives in RAM. Restart the server and it is empty.
  • A cache, not a database. Any item can disappear at any time: it may expire, or be evicted when memory is full. Your code must always be able to rebuild the value.
  • Simple. The core commands are set, get, delete, plus a handful of helpers. There are no queries, no joins, no secondary indexes.
  • Multi-threaded and fast. One server handles hundreds of thousands of operations per second on ordinary hardware.
  • Distributed by the client. Servers do not know about each other. The client library picks which server owns each key, so you scale by adding machines. You will see how in the chapter on distributed caching.

What Memcached is not

It is not durable storage: never keep the only copy of anything in it. It does not replicate data between servers, so losing a server loses its share of the cache. It has no built-in lists, sets or sorted sets (that is Redis territory), and no authentication in its simplest setup, so it must never be exposed to the public internet.

Never trust the cache as the source of truth. If your code reads a value from Memcached and the program breaks when it is missing, you are using it as a database. Always have a fallback that rebuilds the value from the real data source.

What runs on this site (and what does not)

This course is honest about its tools. A browser page cannot run a Memcached server, so the live examples here use Python in your browser with small, clearly labelled stand-ins: a MiniMemcached class that follows the real command semantics (what add does when a key exists, what incr does on a non-number, how cas detects a conflict) but runs inside the page. Real commands, real server output and installation steps are shown as read-only blocks. When a stand-in differs from the real server, the lesson says so.

That trade-off is deliberate: the behaviour of a cache is what you must understand, and a simulation lets you change the clock, fill the memory and force failures in a way a real server will not let you do on demand.

The cost of not caching

Suppose a page needs a database query that takes 40 ms, and the same page is requested 500 times per second. The database performs 500 queries per second for identical results. With a cache that holds the result for 60 seconds, the database does the work about once a minute.

Python How much work does a cache save?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Change ttl_seconds to 5 and requests_per_second to 2 and run it again. A cache helps most when the same data is read often and changes rarely.

Your first (fake) cache

Before using Memcached commands, here is the idea in a dozen lines of plain Python: a dictionary plays the cache, and a slow function plays the database.

Python Cache-aside in miniature

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A Python dictionary only works inside one process. Memcached gives the same idea to many processes and machines at once: all your web workers share one cache over the network. That sharing is why it exists.

Where Memcached is used

  • Caching database query results and rendered HTML fragments.
  • Storing short-lived data such as API responses, rate-limit counters and session data (with care about durability).
  • Absorbing read traffic for popular items so the database stays calm.
  • Large sites run fleets of Memcached servers; the technique of spreading keys across them is covered later in Distributed Caching.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens to data in Memcached when the server restarts?

    Memcached keeps all data in memory and has no persistence, so a restart gives an empty cache.
  2. 2. Who decides what to store in the cache after a miss?

    Memcached never talks to your database. The application loads the data and stores it with set.
  3. 3. Which description fits Memcached best?

    It is a volatile key-value cache. Items can expire or be evicted, so the app must be able to rebuild them.
  4. 4. Why does this course use a MiniMemcached class in Python?

    The stand-in follows the real command semantics inside the page; real server output is shown read-only.

Summary

  • Memcached is an in-memory key-value cache shared by many application processes over the network.
  • The application checks the cache, and on a miss loads the data and stores it for next time.
  • Anything in the cache can vanish, so there must always be a way to rebuild the value.
  • It is simple by design: no queries, no durability, no built-in replication.
  • Live examples here use a labelled Python stand-in; real commands appear as read-only blocks.

Next: Installing and Running Memcached