33
loading...
This website collects cookies to deliver better user experience
Setup Redis in Docker and Prepare the .NET Core Project
Implement the Top Categories
Top Users, the Inbox Pattern, and Lua Scripting for Atomicity
Final Thoughts and Outlook
C:\dev>docker run --name redis -d redis
Microsoft.EntityFrameworkCore.Tools
StackExchange.Redis
Line 1: Connect a Redis multiplexer
Lines 7–11: Insert the post and update the category count in a transaction
Lines 13–15: Read the latest count for the category and add/update the entry in the Redis sorted set “CategoriesByPostCount”
The ZADD command has an optional GT parameter: The value is only updated if it is greater than the current value. This prevents race conditions where another thread also increased and updated the count.
Line 3–8: Prepare the Lua script
Line 13: Read the last sent post ID from Redis
Line 15: Load the unsent posts from the DB
Line 17–21: Execute the Lua script to add the post to the user
Line 22: Set the post count for the user in UsersByPostCount
Line 25: Update the last sent ID after all operations are finished
An extra counter per user is needed so that all key parameters map to the same Redis hash tag. And therefore would be placed on the same Redis shard. The curly braces like in the key “{User:5}:PostsByTimestamp” are signifiers for a Redis hash tag.