Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 37 ยท Memcached Basics

Storage Commands: set, add, replace, get and delete

Master the core Memcached commands set, add, replace, get and delete: what each returns, how they differ, and how to use add as a simple lock or first-writer-wins.

Memcached has a small vocabulary, and five words cover most of daily use: set, add, replace, get and delete. They look similar, but they answer different questions: Do I want to overwrite no matter what? Only if it is new? Only if it exists? Choosing the right one removes whole classes of race conditions.

Every example on this page runs against MiniMemcached, the stand-in described in your first client. Its methods return the same words as the real server: STORED, NOT_STORED, DELETED, NOT_FOUND.

set: store no matter what

set is the workhorse. It stores the value under the key, replacing whatever was there, and resets the expiry and CAS token.

TEXT
set <key> <flags> <exptime> <bytes>

Use set when you have a fresh, authoritative value and you do not care what the cache held before.

add: store only if the key is absent

add stores the item only if the key does not exist; otherwise it replies NOT_STORED and leaves the old value alone. That makes it useful for two things: seeding a default without overwriting a newer value, and acting as a lightweight lock, since only one caller can win the add.

replace: store only if the key exists

replace is the mirror image: it updates the value only if the key is already present, otherwise NOT_STORED. Use it when you want to refresh an existing entry but never create one, for instance updating a cached profile only if somebody has already loaded it.

get and delete

get returns the value or a miss. delete removes a key and answers DELETED or NOT_FOUND. Deleting a key that is not there is harmless; just do not treat NOT_FOUND as an error.

Python The five commands side by side

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.

The decision table

Situation Command
I just loaded fresh data and want it cached set
Create only if nobody has created it add
Update only if it is already cached replace
The source data changed; drop the cached copy delete

add as a lock

Because add succeeds for exactly one caller, you can build a tiny mutual-exclusion lock: whoever wins the add does the work, everyone else waits or serves stale data. The lock has an expiry, so a crashed worker cannot hold it forever.

Python Using add as a lock

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.

This pattern is the basis of stampede protection in a later lesson. Note its limit: it is advisory and has no fencing. If worker A is slow (not dead) and its lock expires, two workers will work at once. That is acceptable for cache rebuilds, where doing the work twice is only wasteful, but never use it to guard something that must be done exactly once, like charging a card.

Return values matter

A beginner often ignores what the commands return. Ignoring them is fine for set in most cache code (a failed set just means a later miss), but for add, replace and cas the return value is the answer.

Python First writer wins

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.

Remember that this is a cache: the username index can disappear. A real application still needs a unique database constraint as the source of truth.

Overwrite semantics to remember

  • set resets the expiry and flags along with the value. If you want to keep the old TTL, use touch-based tools or compute the remaining time.
  • Storing a value does not change other keys. There are no transactions across keys.
  • delete followed by set leaves a short window where the key is absent; another request can fill it with older data. Prefer a plain set of the new value when you have it.
  • The real server stores bytes, up to 1 MB per item by default.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A key already exists. What does add return?

    add only stores when the key is absent. Otherwise it answers NOT_STORED and changes nothing.
  2. 2. Which command updates a key only if it is already cached?

    replace requires the key to exist; if it is missing the answer is NOT_STORED.
  3. 3. Why can add act as a lock?

    The single successful add marks ownership; the expiry prevents a dead worker from holding the lock forever.
  4. 4. What does delete return for a key that is not in the cache?

    NOT_FOUND. It is not an error, the goal (key absent) is already true.

Summary

  • set always stores; add stores only if absent; replace stores only if present.
  • get returns a value or a miss; delete answers DELETED or NOT_FOUND.
  • Use the return value of add for first-writer-wins and advisory locks.
  • A cache lock is advisory; do not use it where exactly-once matters.
  • set resets expiry and flags along with the value.

Next: Append, Prepend, Incr, Decr and Touch