Having touched golang recently, I felt the need for a good project, that could give me an insight into golang programming and redis. So, I worked on Shortify – the url shortener. Though it is not a high level project, it gave a basic idea of getting started.
Redis: ( Blah Blah Blah .. )
In case you are reading this, you already know a little bit about redis. After reading some articles, I have the following conclusions.
Primary reason for using redis ?
Redis is a extremely fast.
Why fast ?
Redis stores data in Primary memory(RAM) of the computer(server) it is running on. Databases like MySQL, MongoDB etc stores data on hardisk(Secondary memory) which causes slow access time. This makes redis comparatively faster.
What is it like ?
REDIS is a Key value store. It is primarily designed to store data using Unique Keys. Similar to NoSQL … right ?
Some uses ?
1. Caching ( saving results to a fast bucket, so that it is not recomputed)
Principle of DP (Dynamic Programming) … ? Yes… Somewhat !!
2. Loading Ad’s ! You don’t want users to wait a lot to see the ad’s. It should be delivered fast. So Redis comes to the rescue !
Download these many Software’s ( Ya I hate setting up DEV ENV ) :
Google App Engine / Heroku :
Can be used to host web apps, for free (to some extend).
They are Platform as a Service.
Enough of Theory Lectures !!.. Lets get rolling .. 😛
Testing Redis Server(ONLINE) :
1. Create an account in redislabs (free 30 mb)
2. Open dashboard and switch to configuration tab.
3. Copy the endpoint.
5. The endpoint is a URL having Host and portnumber, something similar to
redis-XXXX.c9.us-east-1-2.ec2.cloud.redislabs.com – Host
XXXX – port number
4. Download & Install Redis Desktop Manager.
5. Click on Connect to Redis Server. Enter the detail and click ok.
6. Now double click the new connection and you will see db0( ie you have only one database) loaded up.
7. To test, you can right click on the db0 and “Add new key” etc to see if it is saved in the database.
Testing Redis Server(OFFLINE) :
1. Download redis for windows.
2. Run redis-server.exe from the Redis Installation directory
3. Continue from step 5 (above)
4. Enter the following details
5. You will have about 15 db’s numbered like db0 to db15. You can test it similarly.
6. By default,( when running web app etc) the data is saved in the db0.
1. Download golang here.
2. Add GOROOT and GOPATH. ( I guess GOROOT is added by golang installer) . Incase you are wondering, golang is short form for “go language” & go developer are called “gophers”.
3. GOPATH is where you create your projects and GOROOT is the Go installation directory. Always keep both as different directories.
4. Make src, pkg and bin (empty) folders in your GOPATH ( This is a the directory structure of organizing go projects)
5. Create a new project inside the src, lets name it as appengine_test1 ( I have created it using IDE)
6. A good IDE/ editors with plugin are always recommended as it shows us missing imports, syntax errors etc. Recommended Gogland by JetBrains.
7. Download the demo redis project from here.
8. Open it in the IDE. You will see some failed imports.
9. In the cmd/IDE terminal, cd to the appengine_test1 folder.
10. To import the libraries online, run the command
go get github.com/garyburd/redigo/redis
11. Similarly import all the depended libraries. Note that, these libraries go into a github.com or google.golang.org folder created in the $GOPATH/src.
12. To test the app on local environment, click the run icon near the func main().
13. Now run hello.go. Btw allow permission for the windows firewall.
14 Open http://localhost:8080/ in your browser and see the count increment on refresh.
Note 1 : But if you are using app.yaml‘s environment variables i.e. using the os.Getenv(“REDIS_ADDR”), you need to run it via the gcloud sdk.
Testing Redis on Google App Engine:
1. Download and install gcloud sdk.
2. Create a new google project here and note the project_id.
3. Enable billing for google app engine project. Because redis uses sockets which are not provided in the free tier. I have activated $300 free credit trail.
4. Btw I am using JetBrains Gogland IDE preview.
5. cd to the web app folder.
6. Run the following commands one by one. (NOTE 2: # refers to comments and should be omitted while running.)
gcloud init # To continue, you must log in. Would you like to log in (Y/n)? Y gcloud config set project project_ID # change the project_id dev_appserver.py app.yaml #Note 1 gcloud app deploy gcloud app browse
7. If everything worked fine, your app should be up and running in the new tab.
Testing Redis on Heroku:
1. You will need to download glide and add glide folder to the PATH variable.
2. Now cd to the app folder. Btw delete the app.yaml (used for google app engine).
3. Download & install heroku CLI.
4. Heroku uses git . I assume, you all have that installed.
5. Make the following updates to hello.go
Replace appengine.Main() with http.ListenAndServe(“:”+os.Getenv(“PORT”), nil)
Also remove unwanted imports. ie “google.golang.org/appengine”
6. Create a new project from heroku dashboard.
7. Run the following commands one by one.
cd $GOPATH\src\go_shortify_web_app_heroku #heroku part heroku login heroku buildpacks:set heroku/go #glide part glide create glide install #git part git init heroku git:remote -a project_id git add . git commit -m "adding files" git push heroku master heroku logs --tail
That’s pretty much it . Open the heroku web app to see it working.
This was a complete tutorial for setting up the Redis and Golang dev environment in your windows PC. I had a hard time configuring them in the beginning. Hope this would help someone.
In my next tutorial I will cover shortify – the url shortener in detail.
Thank you for reading.