Skip to content

amrishshah/db-cache-sync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DB Cache Sync

DB Cache Sync reads MySQL binlogs asynchronously using go-mysql/canal and updates Redis cache based on database changes (Insert/Update/Delete).


Features

  • Read MySQL binlog events (insert, update, delete)
  • Update Redis cache asynchronously
  • Auto-reconnect on MySQL/Redis failures
  • Lightweight

Requirements

  • Go 1.20+
  • MySQL (Binlog enabled, Row format)
  • Redis server

Installation

git clone https://your-repository-link/db-cache-sync.git
cd db-cache-sync
go mod tidy
go run main.go

Configuration

Edit main.go to update your database and Redis connection settings:

cfg.Addr = "host:port"
cfg.User = "your_mysql_user"
cfg.Password = "your_mysql_password"
cfg.Flavor = "mysql"
cfg.ServerID = 1001 // random unique number

// Redis config
redisClient = redis.NewClient(&redis.Options{
    Addr:     "localhost:6379",
    Password: "",
    DB:       0,
})

MySQL Binlog Settings (important)

Make sure MySQL server is configured properly:

SHOW VARIABLES LIKE 'binlog_format'; -- Should be ROW
SHOW VARIABLES LIKE 'binlog_row_image'; -- Should be FULL

If not, set in your my.cnf:

[mysqld]
log-bin=mysql-bin
server-id=1
binlog-format=ROW
binlog-row-image=FULL

Restart MySQL after changes.


Usage

Simply run:

go run main.go

The service will listen to binlogs and update Redis automatically.

Example Redis Key format:

  • Insert: cache:{table_name}:{primary_key} → JSON value
  • Update: cache:{table_name}:{primary_key} → updated JSON
  • Delete: Redis key deleted

Error Handling

If MySQL dump fails with:

Couldn't execute 'SET SQL_QUOTE_SHOW_CREATE=1...': Variable 'sql_mode' can't be set...

Solution:
Set DumpExecPath to empty and skip initial snapshot in code:

canalCfg.Dump.ExecutionPath = ""
canalCfg.Dump.TableDB = ""

You can also disable Dump mode entirely if binlog position is already known.


Libraries Used


License

This project is open-sourced under the MIT license.

About

Update Redis / Dice DB using Bin log of MySQL / MariaDB

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages