Documentation: - Add detailed README files for all services (auth, character, database, launcher, packet, utils, world) - Create API documentation for the database service with detailed endpoint specifications - Document database schema and relationships - Add service architecture overviews and configuration instructions Unit Tests: - Implement comprehensive test suite for database repositories (user, character, session) - Add gRPC service tests for database interactions - Create tests for packet service components (bufferpool, connection, packets) - Add utility service tests (health check, logging, load balancer, redis cache, service discovery) - Implement auth service user tests - Add character service tests Code Structure: - Reorganize test files into a more consistent structure - Create a dedicated tests crate for integration testing - Add test helpers and mock implementations for easier testing
3.5 KiB
Database Service
The Database Service is a core component of the MMORPG server architecture, providing centralized database access for user accounts, character data, and session management.
Overview
The Database Service exposes gRPC endpoints that allow other services to interact with the PostgreSQL database and Redis cache. It implements the repository pattern to abstract database operations and provides efficient caching mechanisms to improve performance.
Architecture
The service is built using the following components:
- gRPC Server: Exposes endpoints for user, character, and session operations
- Repository Layer: Implements data access logic with caching
- PostgreSQL Database: Stores persistent data
- Redis Cache: Provides high-speed caching for frequently accessed data
Services
The Database Service exposes the following gRPC services:
UserService
Handles user account operations:
GetUser: Retrieves a user by IDGetUserByUsername: Retrieves a user by usernameGetUserByEmail: Retrieves a user by email
CharacterDbService
Handles character operations:
GetCharacter: Retrieves a character by IDGetCharacterList: Retrieves all characters for a userCreateCharacter: Creates a new characterDeleteCharacter: Marks a character for deletion
SessionService
Handles session management:
GetSession: Retrieves session informationRefreshSession: Updates session expiration
Repositories
The service implements the repository pattern with the following repositories:
UserRepository
Manages user data with methods for retrieving users by ID, username, email, or session.
CharacterRepository
Manages character data with methods for creating, retrieving, and deleting characters.
SessionRepository
Manages session data with methods for retrieving and refreshing sessions.
Caching Strategy
The service uses Redis for caching with the following strategy:
- Cache keys follow a consistent pattern (e.g.,
user:{id},character:{id}) - Default TTL of 300 seconds (5 minutes)
- Cache invalidation on updates
- Fallback to database when cache misses
Configuration
The service is configured using environment variables:
LISTEN_ADDR: The address to listen on (default: 0.0.0.0)SERVICE_PORT: The port to listen on (default: 50052)DATABASE_URL: PostgreSQL connection stringREDIS_URL: Redis connection string (default: redis://127.0.0.1:6379)
Running the Service
# Set environment variables
export DATABASE_URL=postgres://username:password@localhost:5432/dbname
export REDIS_URL=redis://localhost:6379
# Run the service
cargo run
Docker
The service can be run using Docker:
docker build -t database-service .
docker run -p 50052:50052 \
-e DATABASE_URL=postgres://username:password@host.docker.internal:5432/dbname \
-e REDIS_URL=redis://host.docker.internal:6379 \
database-service
Integration with External Systems
The Database Service is designed to work with an external database schema management system:
- Schema Management: The database schema is managed by an external web application using better-auth
- User Management: User creation and updates are handled by the external system
- Session Management: Initial session creation is handled by the external system
The Database Service primarily provides read access to this data for other microservices, with limited write capabilities for game-specific data like characters.