Practice & Assessment
Test your understanding of Transactions, Scripting, and Pipelines
Multiple Choice Questions
5After EXEC returns nil in a WATCH transaction, what should your code do?
What is the key difference between a pipeline and a MULTI/EXEC transaction?
Which Redis feature replaces WATCH+MULTI/EXEC for complex conditional operations under high contention?
In a fixed-window rate limiter using INCR, why must you set the TTL on the first request only?
What does SCRIPT LOAD return, and how is it used?
Coding Challenges
1Atomic Inventory Decrement
Implement a purchaseItem(itemId, quantity) function using a Lua script that: reads the current inventory count for inventory:<itemId>, checks that inventory >= quantity, decrements by quantity if sufficient, and returns {success: true, remaining: N} or {success: false, reason: 'out-of-stock'}. The entire check-and-decrement must be atomic. Seed inventory:<sword> with 10. Call purchaseItem 12 times and log each result. Input: string itemId, integer quantity. Output: result object. Estimated time: 20 minutes.
Mini Project
Redis-Powered Rate Limiter Middleware
Build an Express.js middleware that rate-limits API requests per IP address. Implement three strategies and expose each on a different route: /api/fixed — fixed window, 10 requests per 10 seconds using INCR+EXPIRE; /api/sliding — sliding window, 10 requests per 10 seconds using Sorted Set with ZREMRANGEBYSCORE and ZADD; /api/token — token bucket, replenishes 1 token per second, max 10 tokens using Lua script for atomic check-and-decrement. Return 429 with a Retry-After header when the limit is exceeded. Return X-RateLimit-Remaining and X-RateLimit-Reset headers on every response. Include a test script that fires 25 concurrent requests to each route and logs the responses.
