couponservice

package
v0.0.0-...-754da63 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2024 License: MIT Imports: 9 Imported by: 0

README

Redis Database Usage with Minikube

Temporary Link - https://docs.google.com/document/d/10-Jr488fS3_iUGBnu8iyh10Ou1BL8Hija0T9f3bDAvk/edit?usp=sharing

We are building a coupon service which is essentially like a streaming service for some requests. We will use Redis Streams for Queuing. We will then make the image of the service and deploy the redis instance and image to a minikube kubernetes cluster. The essential commands will be in this Readme.

Working of the Coupon Service

We have 4 major regions (APAC, NA, SA, EU) with running service who want coupons to distribute to their local users. They constantly demand coupon codes from our server. As merchants add the coupons, they become available for redemption.

Workflow of the Architecture

coupon-redis-architecture drawio

A look indise the Minikube Cluster

k8-micro-redis drawio

What we are Building?

We are making a microservice that has a coupon code distribution instance. This can be demanded from different consumers. The coupons are stored in our database categorized based on merchants. The merchants add their codes to our database using REST API. For each merchant we have a Redis list. After every coupon addition, messages are added to redis stream based on the coupons in the list. These streams are subscibed by consumers who then get code based on requests. We also purge all the older requests every 24 hours if they have not been fulfilled.

Redis Database

Redis is an open source database that can be treated as a key-value store that we can use for Database purposes(NoSQL), Caching and for Message brokering.

We will run redis in a Docker container. This way we will have the image ready and we can deploy this to minikube.

$ docker run --name coupon-redis-instance -p 6379:6379 -d redis

Redis Structure

For every new merchant, lets add a new list to the Redis database with vendorname as the key. Each time vendor adds a coupon list, we should add it to the redis db list corresponding to that vendor name.

curl http://localhost:9090/coupon/addcoupon --request POST --header 'Couponname:off_50_flat' --header 'Couponvendor:vendor1' --header 'Coupondescription:Avail flat 50 off on all products' --header 'Couponcode:EU778' --header 'Couponregion:EU'

curl http://localhost:9090/coupon/getvendorcoupons --request GET --header 'Vendorname:vendor1'

curl http://localhost:9090/coupon/delregionstream --request DELETE --header 'Region:EU'

Running the Redis Consumer Code [for testing with Minikube]

We are using consumer groups of redis to read from a stream. It is a good practice as we want each message to used just once and in FIFO order.

go run couponservice/couponregionclient/main.go

Minikube Instructions

We will push our image to dockerhub so that this can be downloaded by minikube. Do remember to login first.

docker push satriagitu/product-go-micro

Now, we will deploy the .yaml files to create deployment and services.

Lets start the minikube instance. minikube start

Remember to have minikube and kubectl installed on your local.

kubectl apply -f couponservice/deployments/redis-deployment.yaml

kubectl apply -f couponservice/deployments/redis-service.yaml

Now wait for the Redis Instances to get up and running. Then run the following commands.

kubectl apply -f couponservice/deployments/go-micro-deployment.yaml

kubectl apply -f couponservice/deployments/go-micro-service.yaml

Now, check the status of the services and pods by running

kubectl get pods

kubectl get services

Remember to stop your minikube instance.

minikube stop

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RedisInstanceGenerator

func RedisInstanceGenerator(logger *zap.Logger) *redis.Client

This function is called by the main server to get the redis instance. The instance is again returned to the controller for this package. Now all the functions can access this like zap logger.

Types

type StreamController

type StreamController struct {
	// contains filtered or unexported fields
}

StreamController is the Upload route handler

func NewCouponStreamController

func NewCouponStreamController(logger *zap.Logger, instance *redis.Client) *StreamController

NewCouponStreamController returns a frsh Stream controller

func (*StreamController) AddCouponList

func (ctrl *StreamController) AddCouponList(rw http.ResponseWriter, r *http.Request)

func (*StreamController) GetCouponForInternalValidation

func (ctrl *StreamController) GetCouponForInternalValidation(rw http.ResponseWriter, r *http.Request)

GetCouponForInternalValidation returns all the coupons of the certain vendor. we might need this for internal validation if the need arises. It must be remembered that the Redis database will be flushed every 24 hours. We dont want to fill the Database with excess data.

func (*StreamController) PurgeStream

func (ctrl *StreamController) PurgeStream(rw http.ResponseWriter, r *http.Request)

Lets assume that at any moment, You want to purge the stream. We need a method for that too. We should use role authentication here.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL