Simple Spam Prevention using Redis in Golang

Kennard Wicoady
4 min readApr 1, 2018

--

Gophers, the Official Mascots for Golang

It is been one and half years, since I started to jumped into Golang as primary programming language in my professional work. It is quite a new experience for me, who have basic into C# in previous work, to have a nice open source programming language and the libraries are from same fellows, who are also into Golang. For the information, C# libraries only comes as C#.Net update and 100% relies on Microsoft for the library updates.

Redis, Memory-based Data Storage System

Today’s writing is about utilizing a memory-based data storage and can be treated as NoSQL storage type, the name is Redis. Redis stores all requested data into computer’s / server’s memory, which means faster access comparing to accessing storage data. It is a huge advantage if user requests same data into backend and the responses are barely changes (Reference: What’s the Diff: RAM vs Storage). More readings for Redis, please visit Redis Official Site.

In order to present how to implement Redis into Golang, I have pushed a small Golang in Github repository, here. Please do a clone and run according to repository’s readme file. (Also, I open for suggestions for fixing the code structures and efficiency, in order to make this repository is ready to be used in real working system.)

OK! Good to Go!

Finish setup anything? Good! Then let’s move into next part. Open any web browser, access http://localhost:5555/

This will appear on web browser

Just a simple textbox and button with “Click Me!” writings. Maybe we can try to click / push / touch the button?

Lookin’ Good…

Well, it was simple isn’t it? How about do a quick-double click onto the button? Or if double-click doesn’t suit you, try triple, quad, or more…

Uh, Oh? What happened?

System responds as an error. Well, let’s check the textbox.

System halts requests from the button and return an error message “value exists, please wait”. Looks like we have to wait a while, give it 10 seconds.

Ten seconds are passed. Try to click again, and it returns an OK.

That’s a wrap for simulation itself. The real question is, “How system acknowledges request and prevent to return OK status, when user does same request within 10 seconds?”.

Let’s check out this is code part.

Syntax m.Redis.Get(key) has same meaning with Redis command GET key. This syntax will check if the “key” exists in Redis’ storage and will return key’s value if exists, otherwise “nil” value (will be considered as error in Golang).

Meanwhile, for storing the data, it uses m.Redis.Setex(key, 10, “1")equivalent with SETEX key 10 "1". This syntax will store data as “key” in 10 seconds and with value as “1”. If 10 seconds expired, data will be automatically deleted and need to be registered again with same key, if needed.

And how to make those above syntaxes work like the previous simulation? When first request is occurred, system will check the key in Redis (GET). If there is any, it will be considered as “spam” and returns error. If there is none, system will store similar key into Redis’ storage (SETEX) in 10 seconds.

That’s it, a simple usage of Redis in Golang and explained. I am pretty sure there are lot more practical usage of Redis in productions, which need faster response (or maybe, almost instant) when required and tackling some data queries performance issues, such as prevents single data to be treated as double similar inputs, reducing database load from querying similar data with similar responses over and over, or storing user’s permissions when accessing system’s menu.

Hope this article can help you out about Redis implementation and as a basic to create a bigger and more practical system when it released into production.

--

--