使用Go在主机之间共享变量

I have an application write in golang,and it will load the basic data to global var,so make the application response fast,and export a http interface to update the var when user make changes to the database.

But i deploy another server,and used proxy.There comes a problem,when user send http request to the update url,it will load traffic to one of the servers.So that server update this var,but others not.

such as utils.go:

package utils

var BasicDatas map[string]*MyModel

func UpdateVar(){
// do some work
}
func PreLoadVar(){
// preload data to basicDatas
}

and the main.go

package main
import(
"codebase/utils"
)
func main(){
utils.PreLoadVar()
}

so if there anyway to share the var between multi hosts?Or any libiary can help do this work?

Nsq.io seem a good choice,but i want to seek a more simple one if there is. thanks:)

Your global variable is a very simple form of caching, which is well known to be problematic when you are running your application on multiple servers. There are several ways to fix this problem:

  1. As suggested by Bill Nelson: use your database as the central storage and do not cache your data in your application at all.
  2. Externalize your cache with a simple key-value store, for example using Redis together with one of the many Redis clients in Go.
  3. Use a distributed cache such as groupcache.

If you have too much time on your hands you could use a messaging solution like nsq to implement your own distributed cache, but I would advise against it - this is not an easy problem.