By Ian Worley
Presented at University of Centeral Missouri
Date Created:NoSQL means the database management system does not model data with a dialect of the Structured Query Language.
Because NoSql Database Managment Systems are created to solve problems that do not scale well with Relational Database Managment Systems that use Structured Query Language.
Document based data modeling is using format like JSON to store data in the database management system.
A key value pair store is where to values the key which is what is identifiy the value and the value itself is stored. With using the key one can locate the value. Think like Java Hashmap but in a database.
Column based data modeling is where storing data instead instead of records or rows like sql that data is stored in columns. This allows for compression of storage for data allowing for more space unlike a Relational Database Management System.
Graph data modeling uses nodes these nodes have a collection of attributes with them and edges which connects nodes to each other. Overall this allows for determing realtionships between mutiple things together like a web for example a freinds list on social media.
Redis describes their project as "...in-memory data structure store, used as a database, cache, and message broker." ( Redis, Introduction to Redis )
redis> setex 127.0.0.1 30 1 # Create and set a key that exires in 30 seconds with a value of 1
"OK"
redis> get 127.0.0.1 # Get the value of the key "1"
"1"
redis> ttl 127.0.0.1 # Get the remaining time to live of the key "127.0.0.1"
(integer) 20
redis> get 127.0.0.1 # Get the key of 127.0.0.1 again after 20 seconds
(nil)
With the knowledge of setex and ttl one can create a ratelimiter for a REST
API
const ip = req.headers.get("x-forwarded-for") // grab the client ip
if (!ip) // check if the ip is valid or not
return new Response("Invalid IP", { status: 400 });
const key = `rate-limit:${ip}`; // create a key with the ip address
const limit = 5; // limit
const ttl = 60; // expire or also known as the time to live
const current = await redis.get(rateLimitKey); // get the current value of the key
if (current) {
if (current >= limit) {
// check if the limit is exceeded
return new Response("Rate limit exceeded", { status: 429 }); // return a 429 status code (rate limit exceeded)
} else {
// if not increment the key
await redis.setex(key, ttl, current + 1);
}
}
else {
await redis.setex(key, ttl, 1); // Set the key with a ttl of 60 seconds and value of 1
}
return NextResponse.json(
{ ratelimit: current, ip },
{ status: 200 }
);