counter

command module
v0.0.0-...-838b099 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2023 License: MIT Imports: 2 Imported by: 0

README

Global, Atomic Counter in Redis

The 2 most mature and commonly used Redis drivers in Go are:

A full list of Redis drivers for Go is available here.

In this recipe, we will implement a global, atomic counter using Redis using the Radix driver. Why not just implement a counter locally in the Go program instead. Sure, we can do that if it's just 1 client running a single thread. But in a concurrent context where we have multiple goroutines or in a distributed system, we need to have a system to support an atomic increment operation across the network. Redis provides support for a counter via the INCR command.

Setup

  1. Start redis.

    $ docker-compose up
    
  2. In the another shell, you can connect to Redis via the CLI tool:

    $ docker-compose exec redis redis-cli
    
  3. Run Go program.

    $ go run ./main.go
    
  4. Shut down and remove the container when you are done.

    $ docker-compose down
    
  5. Alternatively, you can run everything with just 2 commands.

    $ make run
    $ make teardown    # Run this to remove the container
    

Reference and Credits

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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