This project demonstrates the Publisher/Subscriber pattern using Golang and DragonFlyDB. It consists of two services:
- Publisher Service (pub.go) - Publishes messages to a channel
- Subscriber Service (subscriber.go) - Subscribes to the channel and processes messages
- Go 1.21 or higher
- Docker and Docker Compose (for containerized deployment)
- DragonFlyDB (automatically set up with Docker Compose)
- The Publisher service connects to DragonFlyDB and periodically publishes JSON messages to a specified channel.
- The Subscriber service connects to the same DragonFlyDB instance and subscribes to the channel.
- When the Publisher sends a message, DragonFlyDB distributes it to all Subscribers.
- The Subscriber receives the message, processes it, and measures the latency.
- Pub/Sub Pattern: Decouples message senders (publishers) from receivers (subscribers)
- Message Distribution: One message can be received by multiple subscribers
- Asynchronous Communication: Publishers and subscribers operate independently
- Scalability: Easy to add more publishers or subscribers without changing the code
# Start all services
docker compose up
# To run with multiple subscriber instances
docker compose up --scale subscriber=3
- Start DragonFlyDB:
docker run -p 6379:6379 docker.dragonflydb.io/dragonflydb/dragonfly
- Run the subscriber:
go run subscriber.go
- Run the publisher:
go run pub.go
Both the publisher and subscriber retrieve configuration via environment variables. The following environment variables control the configuration:
REDIS_ADDR
- DragonFlyDB address (default: "localhost:6379")CHANNEL
- Channel name (default: "messages")
Example environment variable usage:
export REDIS_ADDR=localhost:6379
export CHANNEL=custom-channel
go run pub.go
go run subscriber.go
Messages are JSON-encoded with the following structure:
{
"id": "msg-123",
"content": "This is message #123",
"timestamp": "2023-04-18T12:34:56.789Z"
}
Both services handle SIGINT and SIGTERM signals for graceful shutdown:
- Stop publishing/receiving messages.
- Unsubscribe from channels (for subscribers).
- Close DragonFlyDB connections.
- Exit cleanly.
- Add message acknowledgment
- Implement message persistence
- Add message filtering capabilities
- Implement message replay functionality
- Create a web interface to monitor message flow