A service that handles real-time notifications for a social media platform when users create new posts.
- gRPC endpoint for receiving new post events
- Concurrent notification processing using Go routines and worker pools
- GraphQL API for retrieving user notifications
- Automatic retry with exponential backoff for failed notifications
- Metrics endpoint for monitoring system performance
The service consists of the following components:
- gRPC Service: Receives new post events and queues notifications for followers.
- Notification Queue: Processes notifications concurrently using a worker pool.
- GraphQL API: Provides an endpoint to retrieve user notifications.
- In-Memory Store: Stores user, post, and notification data.
- Language: Go
- API Protocols: gRPC, GraphQL
- Concurrency: Goroutines, Channels, Worker Pool
- Error Handling: Retry logic with exponential backoff
- Go 1.22+
- Protocol Buffer Compiler (protoc)
- Git
-
Clone the repository
git clone https://github.com/suyashXD/DNDS.git cd DNDS -
Install dependencies
go mod download
-
Generate protocol buffer code
protoc --go_out=. --go_opt=paths=source_relative \ --go-grpc_out=. --go-grpc_opt=paths=source_relative \ internal/grpc/proto/notification.proto -
Build the service
go build -o notification-service.exe ./cmd/server/
-
Run the service
./notification-service.exe
Alternatively, you can use Docker:
# Build the image
docker build -t notification-service .
# Run the container
docker run -p 50051:50051 -p 8080:8080 notification-serviceThe service exposes the following endpoints:
The gRPC service implements the following RPC:
rpc PublishPost(Post) returns (NotificationResponse)Example using a gRPC client:
post := &proto.Post{
AuthorId: "user1",
Content: "Hello world!",
CreatedAt: time.Now().Unix(),
}
response, err := client.PublishPost(context.Background(), post)The GraphQL API is available at http://localhost:8080/graphql and provides the following query:
query {
getNotifications(userId: "user1") {
id
content
createdAt
status
}
}Metrics are available at http://localhost:8080/metrics and return JSON with the following information:
total_sent: Total number of notifications deliveredfailed_attempts: Number of failed delivery attemptstotal_retries: Number of retried deliveriesavg_delivery_time: Average time to deliver a notificationqueue_size: Current number of notifications in the queueworker_count: Number of active workers
- All data is stored in memory for simplicity
- Sample user and follower data is pre-populated
- A 10% random failure rate is simulated for demonstration purposes
- Notifications are kept simple with minimal content
- Replace in-memory storage with a persistent database
- Add authentication/authorization
- Implement rate limiting
- Scale horizontally with multiple instances