NoSQL solving Unique Problems

By Ian Worley

Presented at University of Centeral Missouri

Date Created:

What to expect to learn in this presentation?

  • What is a NoSQL Database
  • Why is it important to learn NoSQL databases compare to SQL databases
  • Diffrent approachs that NoSQL databases have to data modeling
  • Downsides of using NoSQL databases
  • Common NoSQL databases that one may come across
  • Extra time a technical demo with Redis

What is a NoSql Database?

NoSQL means the database management system does not model data with a dialect of the Structured Query Language.

Why is learning about NoSql important when Relation Database Systems are widely used?

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.

What are some of the downsides of adding NoSql Database Managment System in your techstack?

  1. Less mature and developed compared to Relational Databases using SQL
  2. Less tools and resources available
  3. No standardization on data modeling
  4. May not support ACID Transactions
How does NoSql Database Managment System model data?
  • Document
  • Key-Value pairs
  • Column
  • Graph

What is document data modeling in a NoSql Database Managment System?

Document based data modeling is using format like JSON to store data in the database management system.

What is a Key Value pair store?

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.

What is a column based data modeling in a NoSql Database Managment System?

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.

What is a Graph data modeling in a NoSql Database Managment 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.

Why use NoSQL Database Managment Systeam?

  • Need better modeling of data that traditional relational database can't provide
  • Easier to scale horizontally and vertically
  • Need to store large amounts of data

What are some of the unique problems NoSQL solves?

  • With a key-value stored ratelimit an REST API users to prevent denial of service attack
  • Representing followers on social media with a graph database
  • Representing a orders with a document database
  • Representing a user profile with a column database

What NoSQL Database Management Systeam are out their?

Redis

Redis Logo
  • Redis describes their project as "...in-memory data structure store, used as a database, cache, and message broker." ( Redis, Introduction to Redis )
  • Redis first release was in 2009
  • While data stored in memory with Redis, the data can be persisted on disk
  • Redis is a key-value store

MongoDB

MongoDB Logo
  • MongoDB is a project that uses a JavaScript Object Notation (JSON) like document model to structure data.
  • Supports ACID
  • Can horizontally scale as well as vertically
  • Many developers like using MongoDB because of how JSON like MongoDB

Cassandra

Cassandra Logo
  • Cassandra is a project that uses a column based data model to structure data.
  • Can run on mutiple nodes but as in redundancies in data
  • Supports ACID

Neo4j

Neo4j Logo
  • Neo4j is a project that uses a graph data model to structure data.
  • Neo4j was written in Java which is how 4J was added to the name
  • Neo4J uses a speical lanaguge named Cypher for modeling data
  • Known to horizontally scale

Using NoSQL Database Managment to solve problems faster deep dive

  • Recall key-value pair store recording how many requests has user made in REST API
  • There will be a closer look at Redis

Understanding how redis manages key value pairs with INCR and EXPIRE commands

        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

What is a ratelimiter?

  • A ratelimiter is limits the amount of requests a user can make to a REST API
  • A ratelimiter prevents a denial of service attack by limiting the rate of requests made by a user

Now the fun part, how to create a naive approach to rate limit a REST API in Redis with TypeScript

      
        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
    
    

Continuing the naive approach to rate limit a REST API in Redis with TypeScript

        
          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
          }
        
      

Conclusion of the naive approach to rate limit a REST API in Redis with TypeScript

        
          return NextResponse.json(
         { ratelimit: current, ip },
          { status: 200 }
        ); 
        
      

Actual Demo of this ratelimter

Demo Link

Sources

  • “Cassandra - Introduction.” Tutorialspoint, https://www.tutorialspoint.com/cassandra/cassandra_introduction.htm. Accessed 5 April 2024.
  • “Introduction to Redis.” Redis, https://redis.io/docs/about/. Accessed 5 April 2024.
  • Kumar, Ajitesh. “NoSQL Data Models Types: Concepts & Examples.” Analytics Yogi, 24 December 2022, https://vitalflux.com/nosql-data-models-concepts-examples/. Accessed 5 April 2024.
  • Mahler, Charles. “What Is a Column Database and When Should You Use One?” The New Stack, 12 December 2022, https://thenewstack.io/what-is-a-column-database-and-when-should-you-use-one/. Accessed 5 April 2024.
  • “Native Graph Database.” Neo4j, https://neo4j.com/product/neo4j-graph-database/. Accessed 5 April 2024.
  • “Neo4j - Overview.” Tutorialspoint, https://www.tutorialspoint.com/neo4j/neo4j_overview.htm. Accessed 5 April 2024.
  • Nicholson, Brad. “SQL vs. NoSQL Databases: What's the Difference?” IBM, 12 June 2022, https://www.ibm.com/blog/sql-vs-nosql/. Accessed 5 April 2024.
  • Pykes, Kurtis. “What is A Graph Database? A Beginner's Guide.” DataCamp, https://www.datacamp.com/blog/what-is-a-graph-database. Accessed 5 April 2024.
  • Raina, Ajeet. “How to build a Rate Limiter using Redis.” The Home of Redis Developers, 10 November 2022, https://developer.redis.com/howtos/ratelimiting/. Accessed 5 April 2024
  • Schaefer, Lauren. “What Is NoSQL? NoSQL Databases Explained.” MongoDB, https://www.mongodb.com/nosql-explained. Accessed 5 April 2024.
  • “Structured vs. Unstructured Data: What’s the Difference?” IBM, IBM, 29 June 2021, https://www.ibm.com/blog/structured-vs-unstructured-data/. Accessed 5 April 2024.
  • Walker, Jim. “Document store vs. relational Database: Which is right for your workload?” Cockroach Labs, 7 July 2022, https://www.cockroachlabs.com/blog/document-store-vs-relational-database/. Accessed 5 April 2024.
  • “What Is MongoDB?” MongoDB, https://www.mongodb.com/what-is-mongodb. Accessed 5 April 2024.
  • “When To Use NoSQL Databases.” MongoDB, https://www.mongodb.com/nosql-explained/when-to-use-nosql. Accessed 5 April 2024.