Video summary
Golang Microservices Full Course #13 - Redis Caching
Main summary
Key takeaways
Summary
This episode of the Go microservices course introduces Redis caching—using the rate/read-through cache plus the cache-aside pattern—to speed up wallet balance reads. It also explains cache invalidation when underlying MySQL data changes (for example, after transfers).
Goals / Problem Solving
- Replace direct DB reads with an in-memory Redis cache backed by MySQL.
- Implement cache-aside behavior:
- Try Redis first.
- On cache miss: query MySQL, then store the result in Redis.
- Handle cache invalidation:
- When wallet balances change (e.g., after a transfer), delete the relevant Redis cache keys so future reads fetch fresh data from MySQL.
Caching Flow Implemented
In the wallet service handler (example mentioned: /wallets/me):
- Build a Redis cache key, such as:
wallet:{userId}- Constructed via
fmt.Sprintfusing the user ID.
- Check Redis:
- If found: deserialize JSON into the wallet model and return immediately.
- If not found: fetch wallet from MySQL, serialize it, and store it in Redis.
- TTL (time-to-live):
- Set Redis key expiration to ~5 minutes (e.g.,
EXPIRE/Set(..., 5 minutes)).
- Set Redis key expiration to ~5 minutes (e.g.,
Redis + Docker Compose Setup
-
Add Redis to
docker-compose.ymlalongside MySQL:- Image:
redis:7-alpine - Container name:
go-wallet-redis - Port mapping:
6379:6379 - Restart policy:
always
- Image:
-
Update Go dependencies and configuration:
- Uses
github.com/ra/go-redis v9 - Set Redis host/port via internal config:
radius/redis addressbuilt from environment host + port.
- Uses
Redis Client Wrapper / Connection Logic
- Create a Redis “database wrapper” under
internal/database/...:- Connect to Redis using the configured address.
- Use a context with timeout (subtitles mention ~3 seconds).
- Log a successful connection via an internal logger.
Cache Invalidation After Transfers
Redis cache keys must be deleted whenever balances change.
- Implemented in the transaction commit / after-transfer flow:
- After a transfer commits, invalidate:
- Sender’s wallet cache key
- Receiver’s wallet cache key
- Delete keys using Redis
DEL(e.g., viaDel).
- After a transfer commits, invalidate:
Performance Detail
- Cache deletion should be asynchronous / non-blocking so it does not delay the HTTP response.
Testing / Mocks (Unit Tests)
- Mocks are updated to include Redis expectations:
- Transaction service tests require mocking Redis client behavior for cache invalidation.
- Test cases are added/updated so invalidation behavior is validated.
- Notes include potential test panics when dependencies (such as logger and Redis client) are not correctly included/mocked.
Runtime Verification / Expected Results
Manual testing is done using Swagger endpoints (course mentions running Swagger via go run ... swagger, including routes like register/profile/get-my-profile).
Expected behavior
- First request to
wallets/me:- cache miss → fetch from MySQL → store to Redis
- Second request:
- cache hit → return from Redis
Log messages
- “cache miss … falling back to my SQL”
- “cache hit … returning from radius/Redis”
Main Speakers / Sources
- Course/author name is not explicitly stated in the subtitles.
- Referenced external libraries:
- Redis client library:
github.com/ra/go-redis/v9
- Redis client library:
- Project components mentioned:
- Swagger/OpenAPI tooling
- “Go wallet” microservices codebase (wallet + transaction services)
Rate this summary
Your feedback will help improve summaries.
Improve this summary
Reprocess with a stronger model when the summary feels incomplete or inaccurate.
Translate summary in another language
Ask questions to this video
Chat for follow-up questions, clarifications, and source-backed answers.