Redis Introduction
Redis is an in-memory data structure store that supports multiple data types, making it a top choice for many developers.
Major usage
- Caching
- Session management
- Rate Limiting
- Message Broker
Supported Data Types
- String
- Hash
- List
- Set
- Sorted set
- Stream
- Bitmap
- Bitfield
- Geospatial
Refer redis documentation which is clean and well structured.
Why Redis?
Reading and writing data in RAM is several orders of magnitude faster than on disk, enabling Redis to handle hundreds of thousands to millions of operations per second.
Redis has a rich set of data structures. These structures enable complex data manipulation and aggregation tasks with minimal code.
Atomicity
Redis processes all the commands in a single-threaded, event-driven loop which ensures that the commands are executed in the order they are issued.
Redis offers atomic operations at the data structure level.
For example: If two clients concurrently execute INCR
command on the same key, Redis ensures that the value is correctly incremented twice without any lost increments.
Transactional
Redis supports transactions using MULTI
and EXEC
commands. It allows a list of commands to be queued and executed atomically.
-
MULTI
starts a transaction - Any commands issued after
MULTI
will be queued and not executed immediately -
EXEC
executes all commands atomically as a single batch - Note that if any command fails, the remaining commands in the transaction still execute. Redis does not support rollback.
Rate Limiting
Think about when your boss dumps a mountain of work on you in no time. You would be exhausted, maybe even seeing stars, right? While we can complain about it, networks don’t have that option—they just slow down and struggle! So, if we get tired from overload, it’s fair to say networks need a breather too!
Rate limiting is a technique which controls the follow of traffic by limiting the number of requests made to a network, server, or resource in a given time frame. It prevents users from exhausting network resources.
A rate-limited application signals users to slow down rather than overwhelming it with too many requests. It’s like traffic officers stopping speeding drivers—this isn't ideal for the fast driver (here, the user has to wait after hitting a certain limit) or for other drivers on the same road (other users trying to access the same resource). Rate limiting ensures smoother access for everyone.
Types of rate limits
Rate limit can be implemented based on the business objective and level of restriction required. There are 3 common types.
User rate limits: It monitors the number of requests made by a user by tracking IP address or API key. Users who have hit the threshold value are notified and denied for further requests until the timeframe resets.
Geographic rate limits: It relies on detecting user's location through IP address or geolocation APIs. By applying different rate limits to various geographic regions, developers can prioritize certain areas or reduce traffic from regions known for abusive behavior. For instance, some applications restrict access or enforce stricter rate limits from countries associated with a high frequency of DDoS or brute-force attacks.
Server rate limits: Developers can set rate limits directly on the server level if they assign specific servers to handle different parts of an application. This approach gives them more flexibility, letting them increase rate limits on frequently used servers while lowering traffic limits on servers that see less action.