# Compare-And-Set in Memcache

DevFeed: [Compare-And-Set in Memcache](<https://devfeed.tech/articles/compare-and-set-in-memcache-38892.md>)

Original publisher: [Read original article](<http://neopythonic.blogspot.com/2011/08/compare-and-set-in-memcache.html>)

Author: Guido van Rossum (noreply@blogger.com)

Published: 2011-08-25T16:59:00Z

Content type: tutorial

Language: en

Sources: [Guido van Rossum](<https://devfeed.tech/sources/guido-van-rossum.md>)

Topics: [content addressed store](<https://devfeed.tech/topics/content-addressed-store.md>), [Python](<https://devfeed.tech/topics/python.md>), [bug](<https://devfeed.tech/topics/bug.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [algorithms](<https://devfeed.tech/tags/algorithms.md>), [appengine](<https://devfeed.tech/tags/appengine.md>), [cas](<https://devfeed.tech/tags/cas.md>), [code](<https://devfeed.tech/tags/code.md>), [compare](<https://devfeed.tech/tags/compare.md>), [google](<https://devfeed.tech/tags/google.md>), [memcache](<https://devfeed.tech/tags/memcache.md>), [new-feature](<https://devfeed.tech/tags/new-feature.md>), [python](<https://devfeed.tech/tags/python.md>), [race-condition](<https://devfeed.tech/tags/race-condition.md>)

## AI overview

This tutorial explains Compare-And-Set in memcache, including its atomic update use case and how it prevents lost updates caused by race conditions when concurrent requests modify the same key.

## Source excerpt

With the most recent release (1.5.3, last week) App Engine's Python API for Memcache has added a new feature, Compare-And-Set. This feature (with a different API) was already available in Java; it has also been available in the non-App-Engine pure-Python memcache client. In fact, I designed the App Engine Python API for this feature to be compatible with the latter, since most of the rest of the App Engine Python API also strives to be at least a superset of that package. But what is it? There seems to be little information on how to use Compare-And-Set with memcache. It is also sometimes (incorrectly) referred to as Compare-And-Swap -- incorrect, because the cas() operation does not actually "swap" anything. The first response when we closed the bug requesting this feature was "Some examples of usage are appreciated." So here goes. The basic use case for Compare-And-Set is when multiple requests that are being handled concurrently need to update the same memcache key in an atomic fashion. Let's assume you are managing a counter in memcache. (Actually, you could use the incr() and decr() operations to update 64-bit integer counters atomically, but just for argument's sake assume you cannot use those -- there are other data types for which the memcache service does not have built-in support.) The naive code to update a counter would be something like this: def init_counter(key): . memcache.set(key, 0) def bump_counter(key): . counter = memcache.get(key) . assert counter is not None, 'Uninitialized counter' . memcache.set(key, counter+1) (Aside: I don't want to have to think about how to get blogger to properly format Python code. I really don't. So just bear with the dots I use for indentation. Okay? Comments pointing me to solutions will be DELETED.) (Aside 2: The assert is kind of naive; in practice you'll have to somehow deal with counter initialization. You also should implement a backup for your counter using the App Engine datastore, so that it can survive evic