No-Nonsense NoSQL

by Jenn Strater @jennstrater

Current

http://jlstrater.github.io/No-Nonsense-NoSQL

Slides(source)

Previous Versions

GreachSource |Slides

Iowa Code CampSource |Slides

Gr8Conf EUSource |Slides

About Me

Professional

About Me

Experience

source: http://tjvantoll.com/2014/12/29/so-you-want-to-write-a-tech-book/

About Me

Minneapolis, MN

    Connect on Social Media
  • Twitter @jennstrater
  • LinkedIn https://linkedin.com/in/jennstrater
  • Github https://github.com/jlstrater

Agenda

  • Review of Grails & Relational Databases
  • Gr8Ladies Example Application
  • NoSQL Definition
  • Types of NoSQL data stores
  • Example
    • MongoDB
    • Redis

What will NOT be covered

  • Every aspect of every NoSQL solution
  • Implementation, maintenance, or scaling of NoSql databases
  • Non-operational solutions
    • Search(i.e. Elasticsearch)
    • File systems And Processing(i.e Hadoop)

Review

Groovy

  • Dynamic, compiled language for the Java Virtual Machine(JVM).
  • Interoperable with existing Java libraries
  • Concise, easy to read code
  • Used by many major companies including:

Grails

  • Groovy Based
  • Model View Controller(MVC) Web framework for the JVM
  • Convention over Configuration
  • Features including:
    • Object Relational Mapper(ORM)
    • Domain-Specific Languages(DSL)
    • Runtime and compile-time Metaprogramming
  • Extensive Plugin Support
  • Open-source community driven Project

New in Grails 3

  • Based on Spring Boot
  • Switched to Gradle for build system
  • Major structural changes
    • Configuration
    • Scripts

Relational Databases

  • Tabular
  • Defined Schema
  • Relationships between data
  • Terms
    • Database
    • Table
    • Row
    • Column

Example Task

Gr8Ladies Products

  • Catalog Products
  • Shopping Cart

Code Samples

class Product {

  String name
  Vendor vendor
  String chapter
  String imageUrl
  String vendorUrlPath
  SortedSet priceQuantityRelations
  Integer minQuantity
  
  static hasMany = [priceQuantityRelations: PriceQuantityRelation]
  
  static constraints = {
      chapter nullable: true
      imageUrl nullable: true
      minQuantity min: 1
  }
  
  static mapping = {
      priceQuantityRelations sort: 'quantity'
  }
}
package org.gr8ladies

class Vendor {

    String name
    String url
    
    static constraints = {
        url(url: true)
    }
}
class PriceQuantityRelation implements Comparable {

    BigDecimal price
    Integer quantity
    BigDecimal unitPrice
    
    static belongsTo = [product: Product]
    
    static constraints = {
        quantity min: 1
        price min: 0.00, scale: 2
        unitPrice min: 0.00, scale: 4
    }
    
    String getDisplayName() {
        quantity + ' for $' + price + '($' + unitPrice + ' each)'
    }
    
    int compareTo(obj) {
        quantity.compareTo(obj.quantity)
    }
}
class Cart {
    String userSession
    
    static hasMany = [cartItems: CartItem]
    
    static constraints = {
    }
    
    Integer getSize() {
        this.cartItems?.size() ?: 0
    }
    
    BigDecimal getTotalPrice() {
        BigDecimal sum = cartItems?.sum { item ->
            item.priceQuantityRelation.unitPrice *
            item.priceQuantityRelation.quantity
        }
        sum ? sum.setScale(2) : 0.00
    }
}
package org.gr8ladies

class CartItem {

    PriceQuantityRelation priceQuantityRelation
    
    static belongsTo = [cart: Cart]
    
    static constraints = {
    }
}

What is NoSQL?

  • Not Only SQL
  • Group of data storage solutions that are not Relational Database Management System(RDBMS)
    • Schema-less
    • Non-relational

Goals of NoSQL

  • Volume
  • Velocity
  • Dependability

CAP Theorem

modified from source: http://www.momahler.com/ProArtistManifesto

Types of NoSQL solutions

  • Aggregate Stores
    • Key-value
    • Document
    • Column
  • Graph

Key-value Stores

Uses

  • Content Caching
  • Transient Data
    • Session Data
  • Image Stores

Examples

  • MemCached
  • DynamoDB*
  • Redis*
  • Amazon S3

Considerations

  • Relationships
  • Transactions
  • Querying

Libraries & Plugins

Status
Jedis
Grails Redis
Redis GORM Last Updated June 2014
Memcached 2nd-Level Cache Plugin Last Updated December 2011
DynamoDB GORM Last Updated April 2012

Document Stores

Uses

  • Content Management Systems(CMS) / Blogging Platforms
  • Forms/data with many optional fields
  • Frequently Changing Schemas

Examples

  • CouchDB
  • RavenDB
  • DynamoDB*
  • Azure DocumentDB
  • MongoDB

Considerations

  • Complex transactions across documents
  • Complex searching

Libraries & Plugins

Status
GMongo Last Updated March 2015
MongoDB GORM
CouchDB GORM Last Updated April 2011
CouchDB REST API

Column Family

aka Wide Column Stores

Use Cases

  • CMS Systems/ Blogging Platforms
  • Log Aggregators
  • Incremental Counters

Examples

  • Cassandra
  • HBase
  • Hypertable

Considerations

  • Schema Changes
  • Arbitrary unstructured data

To Learn More

Graph Databases

Types

  • Labeled Property Graph
  • Hypergraphs
  • Triples

Labeled Property Graph

Hypergraphs

Triples

SPARQL

Uses

  • Social Networks
  • E-commerce Recommendations
  • Geo
    • Nearby Locations and Routing

Examples

  • Neo4J
  • OrientDB
  • Allegro Graph
  • Hypergraph DB

Considerations

  • Deep tree traversals*
  • Very different way of thinking about data

Libraries & Plugins

Status
Neo4j Native REST API
Neo4j GORM Last Updated June 2014

Suggested Reading

  • Sadalage, Pramod J., and Martin Fowler. NoSQL Distilled: A Brief Guide to the Emerging World of Polyglot Persistence. Addison-Wesley Professional, 2012. Print.
  • Vardanyan, Mikayel. "Picking the Right NoSQL Database Tool." Monitis Blog. 22 May 2011. Web.
  • Copeland, Rick. MongoDB Applied Design Patterns. O'Reilly Media, 2013. Print.
  • Chinnachamy, Arun. Redis Applied Design Patterns. Packt Publishing, 2014. Print.
  • Robinson, Ian., Jim Webber, and Emil Eifrem. Graph Databases. O'Reilly Media, 2015. Print.
  • Kingsbury, Kyle. Jepson. https://aphyr.com/tags/jepsen

How do I pick just one?

You don't have to!

Approach # 1

All In - MongoDB GORM

MongoDB

Terminology

  • Databases
  • Collections
  • Document

MongoDB Continued

    Highlights
  • Created When Needed
    • Databases
    • Collections
  • Optional Fields
  • BSON Object Ids over Integers

Approach # 2

Partial - Grails-Redis plugin

Redis

Terminology

  • Keys
  • Types
    • String
    • Hash
    • Sets

Highlights

  • Organized/Indexed Keys
  • CRUD operations
  • Key Expiration
  • Service Level Injection rather than all of GORM

Conclusion

  • Picking a NoSQL solution depends on the problem
  • Not every tool is the right one for the job
  • NoSQL stores are valid alternatives to relational databases
  • NoSQL solutions are highly supported in Groovy and Grails

Questions?

@jennstrater

jenn@gr8ladies.org