Build Your Own Memcached CLI Tool | Coding Challenges (2024)

This challenge is to build your own Memcached CLI client. Memcached is a free, open source, high-performance, distributed memory object caching system. It is intended for use in speeding up dynamic web applications by reducing database load.

Memcached is an in-memory key-value store for small chunks of arbitrary data retrieved from back end systems with higher latency. It is simple yet powerful.

Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its relatively simple API is available for most popular languages.

It’s simple text based network protocol makes it a great platform to use to learn how to build network clients and servers. In this coding challenge we’re going to focus on a client, but if you fancy learning how to build a server, check out the build your own memcached and build your own Redis server coding challenges.

The Challenge - Building A Memcached CLI Client

Memcached is a simple yet powerful server with client libraries in many programming languages. But sometimes it’s useful to be able to interact with a server from the command line. For example it’s useful to be able to use curl to fetch web pages or test/automate calls to RESTful API.

Redis comes with CLI tool, but Memcached doesn’t, so this coding challenge is to build a simple CLI tool for some of the most common Memcached use cases.

Step Zero

Like all good programming languages we’re zero indexed!

For this step, I’ll leave you to setup your IDE / editor of choice and programming language of choice. Keep in mind we’re building a network client so pick a stack you’re comfortable doing that in or would like to learn how to be comfortable doing it in.

If you haven’t built your own Memcached server you should install one that you can connect to for testing.

Step 1

In this step your goal is to build the functionality to serialise and de-serialise the memcached text based protocol. This is the protocol used to communicate with a Memcached Server. Throughout this step you may wish to refer to theMemcached protocol specification.

Storage commands look like this:

<command name> <key> <flags> <exptime> <bytes> [noreply]\r\n

Where <command name> is set, add, replace, append or prepend. The key is a string identifying the key to be used to store the value. Please refer to the protocol specification for the other fields - being able to read a protocol specification and implement it is a useful skill for software engineers.

After the command comes the value in the data block:

<data block>\r\n

The server will then respond with one of :

  • STORED\r\n, to indicate success.
  • NOT_STORED\r\n to indicate the data was not stored, but notbecause of an error. This normally means that thecondition for an "add" or a "replace" command wasn't met.
  • Another response, which you’ll find in the specification but don’t need to implement unless you decide to support Check And Set (CAS) commands.

Retrieval commands look like this:

get <key>*\r\n

In response to this command, the server will send back zero or more items. Each item is sent as a text line followed by a data block. After all the items have been sent, the server sends the string:

END\r\n

to indicate the end of response. Each item sent by the server consists of two lines, the first looks like this:

VALUE <key> <flags> <bytes>\r\n

And the second:

<data block>\r\n

Now that we have the basics of the protocol, your challenge is to write the code required to serialise and de-serialise messages. My personal approach to this would be to usetest-driven development(TDD) to build tests for some example messages. Turning the messages into one or more data structures I can use in my code.

Step 2

In this step your goal is to handle command line arguments. The user should be able to set the host server (either hostname or IP address) and the port to use, i.e.:

% ccmc -host memcached.codingchallenges.fyi -p 12121 <command> [<options>]

You should use the defaults of localhost and 11211 if no host or port are specified.

For this step you should also handle the commands set and get. For example:

% ccmc -host memcached.codingchallenges.fyi -p 12121 set testkey testvalue

% ccmc -host memcached.codingchallenges.fyi -p 12121 get testkey

Bonus points for supporting an option for help, i.e. -?, —-?, -h or --help that will display a suitable help message.

Step 3

In this step your goal is to connect to the server and send the set and get messages to the server and the display the result to the user. That would look like this:

% ccmc set test 1234
% ccmc get test
1234

Step 4

In this step your goal is to support for the commands: add and replace. The add command stores the data, but only if the serverdoesn'talready hold data for the key.

Thereplacecommand stores the data, but only if theserver does notalready hold data for the key. As forsetandgetcommands the general format is:

<command name> <key> <flags> <exptime> <byte cound> [noreply]\r\n
<data block>\r\n

When the preconditions of add and replace are not met the server returns NOT_STORED otherwise it returnsSTORED.

Step 5

In this step your goal is to support theappendandprependcommands. Theappendcommand results in the data being added to an existing key after existing data. Whereas the prepend command results in adding the data to an existing key before the existing data.

Going Further

If you want to take this project further you can look at adding support for thecas(Check and Set) operation and thedelete,incrementanddecrementoperations.

Help Others by Sharing Your Solutions!

If you think your solution is an example other developers can learn from please share it, put it on GitHub, GitLab or elsewhere. Then let me know - ping me a message on the Discord Server or in the Coding Challenges Sub Reddit, via Twitter or LinkedIn or just post about it there and tag me.

Get The Challenges By Email

If you would like to recieve the coding challenges by email, you can subscribe to the weekly newsletter on SubStack here:

Build Your Own Memcached CLI Tool | Coding Challenges (2024)

References

Top Articles
Livvy Dunne Leaked: Everything You Need to Know - Women The Magazine
Fact Check: Max Loughan NOT Missing 'About Six Years' -- 'Energy Harvesting Device' Creator Was In Middle, High School | Lead Stories
The Advantages of Secure Single Sign-on on the BenQ Board
Extranet Landing Page Delta
Incredibox Deluxe
Used Trucks for Sale in Oneida, TN (with Photos)
Rick Steves Forum
Dsw Designer Shoe Warehouse Ann Arbor Photos
Urbfsdreamgirl
South Park Season 26 Kisscartoon
Pogo Express Recharge
Bingo Bling Promo Code 2023
Kimpton Hotels In Charleston Sc
Fkiqx Breakpoints
organization | QAssurance
Old Navy Student Discount Unidays
Anime Souls Trello
Stanley Steemer Medford Oregon
Comparing Each Tacoma Generation, Which is Best?
The Haunting Of A Dream House By Reeves Wiedeman
洗面台用 アクセサリー セットの商品検索結果 | メチャ買いたい.com
Fingerfang Rock Conan
One Piece Chapter 1077 Tcb
Www.dunkin Baskin Runs On You.com
Meine Erfahrung mit Textbroker als Autor (inkl. Beispiel zu Verdienst)
Wisconsin Volleyball Team Full Leaks
Oh The Pawsibilities Salon & Stay Plano
That Is No Sword X Kakushi By Nez_R
Fort Worth Craiglist
25+ Irresistible PowerXL Air Fryer Recipes for Every Occasion! – ChefsBliss
Apple iPhone SE 2nd Gen (2020) 128GB 4G (Very Good- Pre-Owned)
Nc Scratch Off Left
Bolly2Tolly Sale
La Times Jumble Answer Today
Women On Twitch Go Without Makeup To Support A Fellow Streamer
209-929-1099
Quarante ans après avoir arrêté, puis changé le temps
Frigjam
Cece Rose Facial
What Time Moon Rise Tomorrow
Savannah Schultz Leaked
Busted Newspaper Mugshot
Chalupp's Pizza Taos Menu
Espn Expert Picks Week 2
Documentaries About FLDS: Insightful Looks into the Fundamentalist Church
Linden Creek Golden Retrievers
The forgotten history of cats in the navy
Sak Pase Rental Reviews
Tillamook Headlight Herald Obituaries
Hurst Scott Funeral Home Obituaries Richlands Virginia
my Control Vitality Female Support Complex (10 ml)
Ticketmaster Lion King Chicago
Latest Posts
Article information

Author: Aron Pacocha

Last Updated:

Views: 5921

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Aron Pacocha

Birthday: 1999-08-12

Address: 3808 Moen Corner, Gorczanyport, FL 67364-2074

Phone: +393457723392

Job: Retail Consultant

Hobby: Jewelry making, Cooking, Gaming, Reading, Juggling, Cabaret, Origami

Introduction: My name is Aron Pacocha, I am a happy, tasty, innocent, proud, talented, courageous, magnificent person who loves writing and wants to share my knowledge and understanding with you.