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: https://github.com/bradfitz/gomemcache

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

Configuring

go get github.com/bradfitz/gomemcache/memcache

Install git if you get the following error:

go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/bradfitz/gomemcache/memcache: exec: "git": executable file not found in $PATH

The code

package main

import (
    "fmt"
    "github.com/bradfitz/gomemcache/memcache"
    "log"
    "os"
    "errors"
)

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("eticket-cache.4xznmr.0001.euw1.cache.amazonaws.com:11211")
    mc.Set(&memcache.Item{Key: key, Value: []byte(value)})

    it, err := mc.Get(key)

    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(it)
        fmt.Println(string(it.Value[:]))
    }
}

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.

memcached4

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

mc := memcache.New("eticket-cache.4xznmr.0001.euw1.cache.amazonaws.com:11211", "eticket-cache.4xznmr.0002.euw1.cache.amazonaws.com:11211")

Build and run the application

go build main.go
./main Arg1 Arg2

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

Testing

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

memcached3

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 eticket-cache.4xznmr.0001.euw1.cache.amazonaws.com 11211
[root@ip-10-0-1-39 ec2-user]# telnet eticket-cache.4xznmr.0001.euw1.cache.amazonaws.com 11211
Trying 10.0.1.20...
Connected to eticket-cache.4xznmr.0001.euw1.cache.amazonaws.com.
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
END
stats cachedump 1 5

ITEM foo [8 b; 1466415492 s]
END

Conclusion

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.