Redis Caching and Storage
Relevant source files
The following files were used as context for generating this wiki page:
Redis provides transient storage and caching for GoGovSG. The system uses multiple Redis database instances to separate concerns such as session management, one-time password (OTP) throttling, URL redirection caching, and statistics tracking. This architecture improves performance by reducing load on the primary PostgreSQL database and provides fast access to frequently used data.
Sources: README.md:94-96, src/server/config.ts:182-192
Infrastructure Architecture
The application initializes five distinct Redis clients, each connected to a specific URI defined in the environment configuration. These clients handle different functional domains within the server.
Redis Client Instances
| Client Name | URI Environment Variable | Purpose |
|---|---|---|
otpClient | REDIS_OTP_URI | Stores OTP hashes and manages rate limits for logins. |
sessionClient | REDIS_SESSION_URI | Manages browser sessions and user authentication states. |
redirectClient | REDIS_REDIRECT_URI | Caches short-to-long URL mappings for fast redirects. |
statClient | REDIS_STAT_URI | Stores computed statistics like user, link, and click counts. |
safeBrowsingClient | REDIS_SAFE_BROWSING_URI | Caches threat match results from Google Safe Browsing. |
Sources: src/server/redis.ts:16-77, src/server/config.ts:182-192, README.md:104-108
Connection Flow
The following diagram illustrates how the Express server establishes connections to the various Redis instances during initialization.
The server creates these clients using the redis library. In a local development environment, these often point to different database indices (0-4) on a single Redis container.
Sources: src/server/redis.ts:1-77, docker-compose.yml:25-29
URL Redirection Caching
The redirectClient optimizes the redirection process by storing URL metadata. When a user accesses a short link, the system first checks Redis before querying the PostgreSQL database.
Cache Logic and Expiry
- Cache Miss: If the short URL is not in Redis, the system fetches it from the database and populates the cache.
- Cache Hit: If found, the system returns the cached
longUrlandisFilestatus immediately. - Expiry: Cached redirects expire after a duration defined by
REDIRECT_EXPIRY, which defaults to 300 seconds (5 minutes).
Sources: test/server/repositories/UrlRepository.test.ts:468-500, src/server/config.ts:15-16
Sequence: URL Lookup
Session Management
GoGovSG uses connect-redis to store session data in the sessionClient. This allows for scalable, stateless application servers where user sessions persist across restarts or multiple server instances.
- Initialization: The session middleware is configured in
src/server/index.ts. - Security: Cookies are configured with
httpOnly: trueandsameSite: 'strict'. - Persistence: The
ttl(Time To Live) for session data in Redis defaults to thecookie.maxAgesetting, which is typically 1 day (86,400,000 ms).
Sources: src/server/index.ts:162-177, src/server/config.ts:109-123
Configuration and Environment
Redis connections require specific environment variables. In production, these should include credentials and TLS options for services like AWS ElastiCache.
Environment Variables
| Variable | Type | Default (Dev) | Description |
|---|---|---|---|
REDIS_OTP_URI | String | redis://redis:6379/0 | URI for OTP database |
REDIS_SESSION_URI | String | redis://redis:6379/1 | URI for Session database |
REDIS_REDIRECT_URI | String | redis://redis:6379/2 | URI for Redirect cache |
REDIS_STAT_URI | String | redis://redis:6379/3 | URI for Stats database |
REDIS_SAFE_BROWSING_URI | String | redis://redis:6379/4 | URI for Safe Browsing cache |
REDIRECT_EXPIRY | Number | 300 | Cache TTL in seconds |
Sources: src/server/config.ts:15-16, docker-compose.yml:25-29
Local Setup Snippet
The following snippet from docker-compose.yml shows the standard local development configuration for Redis services:
redis:
image: redis:5.0.3-alpine
ports:
- '6379:6379'
Sources: docker-compose.yml:95-98
Summary
Redis serves as a critical performance and security layer for GoGovSG. By separating data into five distinct functional clients (OTP, Session, Redirect, Stats, and Safe Browsing), the system ensures that high-volume operations like URL redirection and login attempts do not interfere with session stability or statistical accuracy.
Sources: src/server/redis.ts:1-77, README.md:94-108