Go Microservices, part 3 - embedded database and JSON

27 February 2017 // Erik Lupander

In part 3, we’ll make our Accountservice do something useful.

  • Declare an ‘Account’ struct
  • Embed a simple key-value store that we can store Account structs in.
  • Serialize a struct to JSON and serve over our /accounts/{accountId} HTTP service.

Source code

As in all upcoming parts of this blog series, you can get the complete source code of this part by cloning the source (see part 2) and switching to the P3 branch, i.e:

  1. git checkout P3

Declaring an Account struct

For a more elaborate introduction to Go structs, please check this guide.

In our project, create a folder named model under the /accountservice folder.

  1. mkdir model

Now, create a file named account.go in the model folder with the following content:

  1. package model
  2. type Account struct {
  3. Id string `json:"id"`
  4. Name string `json:"name"`
  5. }

This declares our Account abstraction that basically is an id and a name. The case of the first letter denotes scoping (Upper-case == public, lower-case package-scoped). We also use the built-in support for declaring how each field should be serialized by the json.Marshal function in Go.

Embedding a key-value store

For this, we’ll use the BoltDB key-value store. It’s simple, fast and easy to work with. We can actually preempt go get to retrieve the dependency before we’ve declared use of it:

  1. go get github.com/boltdb/bolt

Next, in the /goblog/accountservice folder, create a new folder named “dbclient” and a file named boltclient.go. To make mocking easier later on, we’ll start by declaring an interface that defines the contract we need implementors to fulfill:

  1. package dbclient
  2. import (
  3. "github.com/callistaenterprise/goblog/accountservice/model"
  4. )
  5. type IBoltClient interface {
  6. OpenBoltDb()
  7. QueryAccount(accountId string) (model.Account, error)
  8. Seed()
  9. }

In the same file, we’ll provide an implementation of this interface. Start by declaring a struct that encapsulates a pointer to a bolt.DB instance.

  1. // Real implementation
  2. type BoltClient struct {
  3. boltDB *bolt.DB
  4. }

Here is the implementation of OpenBoltDb(). We’ll add the two remaining functions a bit further down.

  1. func (bc *BoltClient) OpenBoltDb() {
  2. var err error
  3. bc.boltDB, err = bolt.Open("accounts.db", 0600, nil)
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. }

This part of Go syntax can feel a bit weird at first, where we bind a function to a struct. Our struct now implicitly implements one of the three methods.

We’ll need an instance of this “bolt client” somewhere. Let’s put it where it’s going to be used, in /goblog/accountservice/service/handlers.go. Create that file and add the instance of our struct:

  1. package service
  2. import (
  3. "github.com/callistaenterprise/goblog/accountservice/dbclient"
  4. )
  5. var DBClient dbclient.IBoltClient

Update main.go so it’ll open the DB on start:

  1. func main() {
  2. fmt.Printf("Starting %v\n", appName)
  3. initializeBoltClient() // NEW
  4. service.StartWebServer("6767")
  5. }
  6. // Creates instance and calls the OpenBoltDb and Seed funcs
  7. func initializeBoltClient() {
  8. service.DBClient = &dbclient.BoltClient{}
  9. service.DBClient.OpenBoltDb()
  10. service.DBClient.Seed()
  11. }

Our microservice should now create a database on start. However, before running we’ll add a piece of code that’ll bootstrap some accounts for us on startup.

Seed some Accounts on startup

Open boltclient.go again and add the following functions:

  1. // Start seeding accounts
  2. func (bc *BoltClient) Seed() {
  3. initializeBucket()
  4. seedAccounts()
  5. }
  6. // Creates an "AccountBucket" in our BoltDB. It will overwrite any existing bucket of the same name.
  7. func (bc *BoltClient) initializeBucket() {
  8. bc.boltDB.Update(func(tx *bolt.Tx) error {
  9. _, err := tx.CreateBucket([]byte("AccountBucket"))
  10. if err != nil {
  11. return fmt.Errorf("create bucket failed: %s", err)
  12. }
  13. return nil
  14. })
  15. }
  16. // Seed (n) make-believe account objects into the AcountBucket bucket.
  17. func (bc *BoltClient) seedAccounts() {
  18. total := 100
  19. for i := 0; i < total; i++ {
  20. // Generate a key 10000 or larger
  21. key := strconv.Itoa(10000 + i)
  22. // Create an instance of our Account struct
  23. acc := model.Account{
  24. Id: key,
  25. Name: "Person_" + strconv.Itoa(i),
  26. }
  27. // Serialize the struct to JSON
  28. jsonBytes, _ := json.Marshal(acc)
  29. // Write the data to the AccountBucket
  30. bc.boltDB.Update(func(tx *bolt.Tx) error {
  31. b := tx.Bucket([]byte("AccountBucket"))
  32. err := b.Put([]byte(key), jsonBytes)
  33. return err
  34. })
  35. }
  36. fmt.Printf("Seeded %v fake accounts...\n", total)
  37. }

For more details on the Bolt API and how the Update method accepts a func that does the work for us, see the BoltDB documentation.

We’re done with the BoltDB part for now. Let’s build and run again:

  1. > go run *.go
  2. Starting accountservice
  3. Seeded 100 fake accounts...
  4. 2017/01/31 16:30:59 Starting HTTP service at 6767

Lovely! Stop it using Ctrl+C.

Adding a Query method

Now we finish our little DB API by adding a Query method to the boltclient.go:

  1. func (bc *BoltClient) QueryAccount(accountId string) (model.Account, error) {
  2. // Allocate an empty Account instance we'll let json.Unmarhal populate for us in a bit.
  3. account := model.Account{}
  4. // Read an object from the bucket using boltDB.View
  5. err := bc.boltDB.View(func(tx *bolt.Tx) error {
  6. // Read the bucket from the DB
  7. b := tx.Bucket([]byte("AccountBucket"))
  8. // Read the value identified by our accountId supplied as []byte
  9. accountBytes := b.Get([]byte(accountId))
  10. if accountBytes == nil {
  11. return fmt.Errorf("No account found for " + accountId)
  12. }
  13. // Unmarshal the returned bytes into the account struct we created at
  14. // the top of the function
  15. json.Unmarshal(accountBytes, &account)
  16. // Return nil to indicate nothing went wrong, e.g no error
  17. return nil
  18. })
  19. // If there were an error, return the error
  20. if err != nil {
  21. return model.Account{}, err
  22. }
  23. // Return the Account struct and nil as error.
  24. return account, nil
  25. }

Follow the comments if the code doesn’t make sense. The function will query the BoltDB using a supplied accountId parameter and will return an Account struct or an error.

Serving the Account over HTTP

Let’s fix the /accounts/{accountId} route we declared in /service/routes.go so it actually returns one of the seeded Account structs. Open routes.go and replace the inlined func(w http.ResponseWriter, r *http.Request) { with a reference to a function GetAccount we’ll create in a moment:

  1. Route{
  2. "GetAccount", // Name
  3. "GET", // HTTP method
  4. "/accounts/{accountId}", // Route pattern
  5. GetAccount,
  6. },

Next, update /service/handlers.go with a GetAccount func that fulfills the HTTP handler func signature:

  1. var DBClient dbclient.IBoltClient
  2. func GetAccount(w http.ResponseWriter, r *http.Request) {
  3. // Read the 'accountId' path parameter from the mux map
  4. var accountId = mux.Vars(r)["accountId"]
  5. // Read the account struct BoltDB
  6. account, err := DBClient.QueryAccount(accountId)
  7. // If err, return a 404
  8. if err != nil {
  9. w.WriteHeader(http.StatusNotFound)
  10. return
  11. }
  12. // If found, marshal into JSON, write headers and content
  13. data, _ := json.Marshal(account)
  14. w.Header().Set("Content-Type", "application/json")
  15. w.Header().Set("Content-Length", strconv.Itoa(len(data)))
  16. w.WriteHeader(http.StatusOK)
  17. w.Write(data)
  18. }

The GetAccount func fulfills the handler func signature so when Gorilla detects a call to /accounts/{accountId} it will route the request into the GetAccount function. Let’s run it!

  1. > go run *.go
  2. Starting accountservice
  3. Seeded 100 fake accounts...
  4. 2017/01/31 16:30:59 Starting HTTP service at 6767

Call the API using curl. Remember, we seeded 100 accounts starting with an Id of 10000.

  1. > curl http://localhost:6767/accounts/10000
  2. {"id":"10000","name":"Person_0"}

Nice! Our microservice is now actually serving JSON data from an underlying store over HTTP.

Footprint and performance

Let’s check the same memory and CPU usage metrics as in part 2: Before, during and after our simple Gatling-based load test.

Memory usage after startup

mem use

2.1 mb, still not bad! Adding the embedded BoltDB and some more code to handle routing etc. added 300kb to our initial footprint. Let’s start the Gatling test running 1K req/s. Now we’re actually returning a real Account object fetched from the BoltDB which also is serialized to JSON:

Memory usage after load test

mem use231.2 mb of RAM. The extra overhead of serving 1K req/s using an embedded DB was really small compared to the naive service from Part 2.

Performance and CPU usage

cpu useServing 1K req/s uses about 10% of a single Core. The overhead of the BoltDB and JSON serialization is not very significant, good! By the way - the java process at the top is our Gatling test which actually uses ~3x the CPU resources as the software it is testing.

performanceMean response time is still less than one millisecond.

Perhaps we should test with a heavier load, shall we say 4K req/s? (Note that one may need to increase the number of available file handles on the OS level):

Memory use at 4K req/s

mem useApprox 120 mb. Almost exactly an increase by 4x. This memory scaling with n/o concurrent requests is almost certainly due to the Golang runtime or possibly Gorilla increasing the number of internal goroutines used to serve requests concurrently as load goes up.

Performance at 4K req/s

cpu useCPU use stays just below 30% at 4K req/s. At this point, i.e. running on a 16 GB RAM / Core i7 equipped laptop, I’d say that IO or file handles would bottleneck sooner than available CPU cycles.

performanceMean latency now finally rises above 1 ms with 95% of requests staying below 3ms. We do see latency starting to take a hit at 4K req/s, though I’d personally say that the little Accountservice with its embedded BoltDB performs really well.

Comparison to other platforms

One could probably write an interesting blog post about benchmarking this “accountservice” against an functionally equivalent microservice implemented on the JVM, NodeJS, CLR and others.

I did some naive inconclusive benchmarking (using a Gatling test) on this myself late 2015 comparing a HTTP/JSON service + MongoDB access implemented in Go 1.5 vs Spring Boot@Java 8 and NodeJS. In that particular case the JVM and Go-based solutions scaled equally well with a slight edge to the JVM-based solution regarding latencies. The NodeJS server performed quite similarly to the others up to the point where the CPU utilization reached 100% on a single core and things started going south regarding latencies.

Please don’t take the benchmarking mentioned above as some kind of fact as it was just a quick and dirty thing I did for my own pleasure.

So while the numbers I’ve shown regarding performance at 4K req/s using Go 1.7 for the “accountservice” may seem very impressive, they can probably be matched by other platforms as well, though I doubt their memory use will be as pleasant. I guess your milage may vary.

Final words

In the next part of this blog series we’ll take a look at unit testing our service using GoConvey and mocking the BoltDB client.