Skip to content

an example memcached server that simply handles get and set requests

License

Notifications You must be signed in to change notification settings

nickdesaulniers/cpp11-memcached

Repository files navigation

#cpp11-memcached This is a toy memcached server that simply supports set and get requests, written as an opportunity to write some more c++11.

##Building Requirements:

  • C++11 compatible compiler
  • CMake
  • Boost
  • Node.js (optional, for running some of the local tests)
mkdir build && cd buid
cmake ..
make
./main

For Boost & Cmake, you can install it on OSX with brew install boost cmake and on Ubuntu with sudo aptitude install cmake libboost-all-dev.

###CAS Field The CAS field is used to signal atomicity. From the memcached spec, section 4.3 "Set, Add, Replace":

If the Data Version Check (CAS) is nonzero, the requested operation
MUST only succeed if the item exists and has a CAS value identical to
the provided value.

CAS is short for Compare And Swap, a common atomic operation that only updates a value if the writer has the most up to date value. For example, if thread A writes to a location, then thread B writes to the same location, thread A's subsequent CAS operation will fail.

###Tradeoffs

  1. This code assumes all key value pairs can fit into memory on this machine. This doesn't scale well beyond a single machine. Moving to a distributed message queue might be better, but adds complexity in keeping all caches coherent.
  2. One thread is spawned per connection. This adds latency as the thread is being spawned, and can easily use up system resources in a DoS attack. Instead, a thread pool should be used.
  3. Connections are handled synchronously. This hurts throughput, but makes the code slightly simpler. Boost asio has support for async.
  4. All threads share a key value store. Shared memory is juggling razors. Also, I simply wrap access to the key value store in a mutex. A thread must acquire the mutex once for a set operation and twice for a get operation. The use of a properly programmed concurrent map would be better, like from libcds or even the basic example from C++ Concurrency in Action: Practical Multithreading.

###Formatting Each C++ source and header is run through clang-format -i find . -name '*.cpp'`.

About

an example memcached server that simply handles get and set requests

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published