AWS ElastiCache Memcached cluster in a GO project

In this tutorial we’ll learn how to use AWS ElastiCache Memcached cluster in a GO project.
We’ll be using the most recommended GO library:

To follow this tutorial you’ll need a working ElastiCache Memcached cluster. Read the AWS getting started tutorial.


go get

Install git if you get the following error:

go: missing Git command. See
package exec: "git": executable file not found in $PATH

The code

package main

import (

func main() {

    if len(os.Args) < 3 {
        log.Fatalln(errors.New("Usage: main key value"))

    key := os.Args[1]
    value := os.Args[2]

    mc := memcache.New("")
    mc.Set(&memcache.Item{Key: key, Value: []byte(value)})

    it, err := mc.Get(key)

    if err != nil {
    } else {

You must replace the Memcached endpoints with your own. Go to your ElastiCache Dashboard, Cache Clusters & click on Nodes.
Click on “Copy Node Endpoint” to view all your available Memcached node endpoints.


If you have more than one node edit the memcache.New line:

mc := memcache.New("", "")

Build and run the application

go build main.go
./main Arg1 Arg2

Replace Arg1 & Arg2 with the key and value you wish to create.


You can test that your keys are being created by clicking on the node(s) and scroll to the “Current Items (Count)” graph.


From your EC2 instance you can telnet to your Memcached node and run commands to query the keys stored in that server.

· stats items retrieves stats from the node
· stats cachedump 1 5 retrieves keys 1 to 5

telnet 11211
[root@ip-10-0-1-39 ec2-user]# telnet 11211
Connected to
Escape character is '^]'.
stats items

STAT items:1:number 1
STAT items:1:age 276
STAT items:1:evicted 0
STAT items:1:evicted_nonzero 0
STAT items:1:evicted_time 0
STAT items:1:outofmemory 0
STAT items:1:tailrepairs 0
STAT items:1:reclaimed 0
STAT items:1:expired_unfetched 0
STAT items:1:evicted_unfetched 0
STAT items:1:crawler_reclaimed 0
STAT items:1:crawler_items_checked 0
STAT items:1:lrutail_reflocked 0
stats cachedump 1 5

ITEM foo [8 b; 1466415492 s]


In this simple example we connected to a Memcached server, created a key & retrieved its value. In the next tutorial we’ll learn how to add Auto Discovery to add/remove nodes dynamically.