Stock API Update Guide
Learn how to migrate to the new stock API.
Overview
We've upgraded our stock management system to use Redis Sorted Sets for improved performance, reliability, and atomic operations. This document outlines the changes and how to adapt your integration to the new system.
Key Changes
- Storage Model: Stocks are now stored in Redis Sorted Sets using the pattern
stock:${ownerId}:${game}:${groupId}
- Atomic Operations: All stock checkout operations are now atomic using Redis Lua scripts
- Return Values: Response formats have been updated to better represent stock availability
- Error Handling: More detailed error responses for stock availability issues
API Endpoint Changes
GET /stock
- Get Available Stocks
What Changed:
- Response structure now reflects Redis Sorted Set counts by game and amount
- No longer returns individual stock IDs in the response
Before:
After:
POST /stock/check
- Check Stock Availability
What Changed:
- Now uses atomic operations with Redis Sorted Sets to check availability
- Response format updated to include more precise missing stock information
Before:
After:
Error Example:
POST /stock/buy
- Buy Stocks
What Changed:
- Now uses atomic Lua scripts to ensure all-or-nothing stock checkout
- Improved rollback mechanism if PostgreSQL update fails
- More detailed response about purchased stocks
Before & After: The response structure remains similar but the operation is more reliable:
Integration Guidelines
Checking Stock Availability
When checking stock availability, focus on the available
boolean and missing
object:
Buying Stocks
The buying process is now fully atomic - either all stocks are acquired or none:
Benefits of the New System
- Improved Performance: Redis Sorted Sets provide O(log(N)) complexity for range operations
- Atomic Operations: Prevents race conditions during high-concurrency checkout
- Better Reliability: Automatic rollback if any part of the transaction fails
- Increased Scalability: Designed for high throughput and concurrent access
Potential Migration Issues
- Different Response Format: Update your code to handle the new JSON response structures
- More Specific Errors: Error responses now contain more detailed information about missing stock
- All-or-Nothing Transactions: The system will not partially fulfill orders if insufficient stock exists
Best Practices
- Always Check Before Buying: While the buy operation is atomic, it's still good practice to check availability first
- Handle Missing Stock Gracefully: Use the detailed
missing
object to inform users which stock items are unavailable - Implement Proper Error Handling: The API now provides more specific error details to help diagnose issues
Need Help?
If you encounter any issues during migration or have questions about the new system, please contact our support team at [email protected] or open a ticket in our developer portal.