feat(core): optimize performance and reliability through batching and redis-backed unread counts
This commit introduces several significant architectural improvements to enhance system performance, scalability, and reliability:
- **Performance Optimization (N+1 Problem Resolution)**: Implemented batching mechanisms across `MessageHandler`, `ChatService`, `GroupService`, `MessageService`, and `UserService`. This replaces multiple individual database/cache queries with single batch operations for fetching unread counts, last messages, participants, and user information.
- **Enhanced Unread Count Management**: Migrated unread count tracking to a Redis Hash-based approach (`unread#️⃣{userID}`). This allows for atomic increments/decrements and efficient retrieval of both individual conversation unread counts and total unread counts for a user.
- **Improved Message Sequencing**: Integrated Redis-based sequence (`seq`) pre-allocation for messages to ensure strict ordering and reduce database contention during high-concurrency message creation.
- **Push Service Reliability**: Refactored the push notification system to include persistent `PushRecord` tracking. Added a recovery mechanism (`recoverPendingPushes`) to reload pending notifications from the database upon service startup, ensuring better delivery guarantees.
- **WebSocket Reliability**: Updated the WebSocket hub to move away from in-memory history replay in favor of a client-driven synchronization model (`sync_required` event and `ack` handling), reducing memory overhead and improving connection stability.
- **Cache Layer Enhancements**: Added `HIncrBy` and `IncrBySeq` to the `Cache` interface and its implementations (`RedisCache`, `LayeredCache`) to support the new unread and sequence management logic.
This commit is contained in:
6
internal/cache/cache.go
vendored
6
internal/cache/cache.go
vendored
@@ -48,6 +48,8 @@ type Cache interface {
|
||||
HGetAll(ctx context.Context, key string) (map[string]string, error)
|
||||
// HDel 删除 Hash 字段
|
||||
HDel(ctx context.Context, key string, fields ...string) error
|
||||
// HIncrBy 原子递增 Hash 字段的值
|
||||
HIncrBy(ctx context.Context, key string, field string, incr int64) (int64, error)
|
||||
|
||||
// ==================== Sorted Set 操作 ====================
|
||||
// ZAdd 添加 Sorted Set 成员
|
||||
@@ -68,6 +70,8 @@ type Cache interface {
|
||||
// ==================== 计数器操作 ====================
|
||||
// Incr 原子递增(返回新值)
|
||||
Incr(ctx context.Context, key string) (int64, error)
|
||||
// IncrBySeq 原子递增指定值(用于 seq 对齐)
|
||||
IncrBySeq(ctx context.Context, key string, delta int64) (int64, error)
|
||||
// Expire 设置过期时间
|
||||
Expire(ctx context.Context, key string, ttl time.Duration) error
|
||||
}
|
||||
@@ -95,7 +99,7 @@ var settings = Settings{
|
||||
NullTTL: 5 * time.Second,
|
||||
JitterRatio: 0.1,
|
||||
PostListTTL: 30 * time.Second,
|
||||
ConversationTTL: 60 * time.Second,
|
||||
ConversationTTL: 120 * time.Second,
|
||||
UnreadCountTTL: 30 * time.Second,
|
||||
GroupMembersTTL: 120 * time.Second,
|
||||
DisableFlushDB: true,
|
||||
|
||||
Reference in New Issue
Block a user