1 NOSQL Yan Cui @theburningmonk
2 Server-side Developer @
3 iwi by numbers 400k+ DAU ~100m requests/day 25k+ concurrent users1500+ requests/s 7000+ cache opts/s 100+ commodity servers (EC2 small instance) 75ms average latency
4 Sign Posts Why NOSQL? Types of NOSQL DBs NOSQL In Practice Q&A
5 A look at the… Current Trends
6 5 exabytes of data from the dawn of civilization to 20035 exabytes of data from the dawn of civilization to Now we generate that much data every 2 days.
7 Big Data “…data sets whose size is beyond the ability of commonly used software tools to capture, manage and process within a tolerable elapsed time…” The challenge facing many developers operating within the web/social space is how to cope with ever increasing volumes of data, and that challenge is commonly referred to as ‘Big Data’. Given that the size of the digital universe is predicated to continue to grow exponentially for the foreseeable future, life is not gonna get easier for us developers anytime soon!
8 Big Data Unit Symbol Bytes Kilobyte KB 1024 Megabyte MB 1048576Gigabyte GB Terabyte TB Petabyte PB Exabyte EB Zettabyte ZB Yottabyte YB PAIN-O-Meter Just how big does your data have to be for it to be considered a ‘Big Data’? Understandably, it is a moving target, but generally speaking, when you cross over the terabyte threshold you’re starting to step into the ‘Big Data’ zone of pain.
9 So how exactly do we tame the beast that is ‘Big Data’?
10 Vertical Scaling Server Cost PowerEdge T110 II (basic)8 GB, 3.1 Ghz Quad 4T $1,350 32 GB, 3.4 Ghz Quad 8T $12,103 PowerEdge C2100 192 GB, 2 x 3 Ghz $19,960 IBM System x3850 X5 2048 GB, 8 x 2.4 Ghz $646,605 Blue Gene/P 14 teraflops, 4096 CPUs $1,300,000 K Computer (fastest super computer) 10 petaflops, 705,024 cores, 1,377 TB $10,000,000 annual operating cost The traditional wisdom says that we should get bigger servers! And sure, it’ll work, to some extent, but it’ll cost you! In fact, the further up the food chain you go, the less value you get for your money as the cost of the hardware goes up exponentially.
11 Horizontal Scaling Incremental scaling Cost grows incrementallyEasy to scale down Linear gains
12 If you consider scaling purely as a function of cost, then if you can keep your cost under control and make sure that it increases proportionally to the increases in scale then it’s happy days all around! You’re happy, your boss is happy, marketing’s happy, and the shareholders are happy. On the other hand, if you choose to fight big data with big hardware, then your cost to scale ratio is likely to clime significantly, leaving you out of pocket. And when everyone decides to play that game, it’ll undoubtedly make some people very happy...
13 Hardware Vendor ...but unless you’re in the business of selling expensive hardware to developers you’re probably not the one laughing... And since most of that hardware investment is made up-front, as a company, possibly a start up, you’ll be taking on a significant risk and god forbid if things don’t pan out for you...
14
15 Here’s an alternative…Introducing NoSql
16 NOSQL is … No SQL Not Only SQL A movement away from relational modelConsisted of 4 main types of DBs
17 NOSQL is … Hard A new dimension of trade-offs CAP theoremIn 2000, Eric Brewer gave a keynote speech at the ACM Symposium on the Principles of Distributed Computing, in which he said that as applications become more web-based we should stop worrying about data consistency, because if we want high availability in these new distributed applications, then guaranteed consistency of data is something we cannot have. There are three core systemic requirements that exists in a special relationship when it comes to designing and deploying applications in a distributed environment – Consistency, Availability and Partition Tolerance.
18 CAP Theorem A C P Availability: Consistency: Partition Tolerant:Each client can always read and write data Consistency: All clients have the same view of data Partition Tolerant: System works despite network partitions A service that is Consistent operates fully or not at all. (Consistent here differs from the C in ACID which describes a property of database transactions that ensure data will never be persisted that breaks certain pre-set constraints) This usually translates to the idea that multiple values for the same piece of data are not allowed. Availability means just that – a service is available. Funny thing about availability is that it most often deserts you when you need it the most – during busy periods. A service that’s available but not accessible is no benefit to anyone. A service that is Partition Tolerant can survive network partitions. The CAP theorem says that you can only have two of the three. C P
19 NOSQL DBs are … Specialized for particular use cases Non-relationalSemi-structured Horizontally scalable (usually)
20 Motivations Horizontal Scalability Low Latency Cost Minimize Downtime
21 Motivations Use the right tool for the right job!
22 Vertical Scaling The Good The Bad Simple to set upFamiliar to developers Cost grows exponentially Up-front hardware cost* Difficult to scale down* * : mitigated using Cloud services to some extent
23 Horizontal Scaling The Good The Bad Incremental scalingCost grows incrementally Easy to scale down Linear gains More complex programming model More complex to manage
24 RDBMS CAN scale horizontally (via sharding) Manual client side hashingCross-server queries are difficult Loses ACIDcity Schema update = PAIN Before we move onto NoSQL databases, I just want to make it clear that IT IS POSSIBLE to scale horizontally with traditional RDBMS. However, there’s a number of drawbacks: you have to implement client-side hashing yourself, which is not that hard and even some of the NoSQL DBs don’t provide clustering out of the box and requires manual implementation for client side hashing once you’ve sharded your db, it means queries against a particular table now needs to be made across all the sharded nodes, making the orchestration and collection of results more complex also, cross-node transactions is almost a no-go, and it’s difficult to enforce consistency and isolation in a distributed environment too, some specialized NoSQL DBs are designed to solve that problem but to force a similar solution onto a general purposed RDBMS is a recipe for disaster schema updates on a large db is painful, schema update on a massive multi-node db cluster is a pain worse than death...
25 Types of nosql dbs
26 Types Of NOSQL DBs Key-Value Store Document Store Column DatabaseGraph Database
27 Key-Value Store “key” “value” morpheus
28 Key-Value Store It’s a Hash Basic get/put/delete ops Crazy fast!Easy to scale horizontally Membase, Redis, ORACLE…
29 Document Store “key” “document” { name : “Morpheus”, rank : “Captain”,occupation: “Total badass” } morpheus
30 Document Store Document = self-contained piece of dataSemi-structured data Querying MongoDB, RavenDB…
31 Column Database Name Last Name Age Rank Occupation Version LanguageThomas Anderson 29 Morpheus Captain Total badass Cypher Reagan Agent Smith 1.0b The Architect C++
32 Column Database Data stored by column Semi-structured dataCassandra, HBase, …
33 Graph Database 7 3 9 1 2 5 KNOWS CODED_BY name = “Morpheus”name = “Thomas Anderson” age = 29 name = “Trinity” age = 3 days KNOWS name = “Morpheus” rank = “Captain” occupation = “Total badass” disclosure = public name = “Cypher” last name = “Reagan” disclosure = secret age = 6 months name = “Agent Smith” version = 1.0b language = C++ name = “The Architect” CODED_BY
34 Graph Database Nodes, properties, edges Based on graph theoryNode adjacency instead of indices Neo4j, VertexDB, …
35 Real-world use cases for NoSQL DBs...NoSql In Practice
36 Redis Remote dictionary server Key-Value store In-memory, persistentData structures
37 Redis Sorted Sets Lists Sets Hashes
38 Redis
39 Redis in Practice #1 Counters
40 Counters Potentially massive numbers of opsValuable data, but not mission critical
41 Counters Lots of row contention in SQL Requires lots of transactions
42 Counters Redis has atomic incr/decr INCR Increments value by 1 INCRBYIncrements value by given amount DECR Decrements value by 1 DECRBY Decrements value by given amount
43 Counters
44 Redis in Practice #2 Random items
45 Random Items Give user a random article SQL implementationselect count(*) from TABLE var n = random.Next(0, (count – 1)) select * from TABLE where primary_key = n inefficient, complex
46 Random Items Redis has built-in randomize operation SRANDMEMBERGets a random member from a set
47 Random Items About sets: 0 to N unique elements Unordered Atomic add
48 Random Items
49 Redis in Practice #3 Presence
50 Presence Who’s online? Needs to be scalable Pseudo-real time
51 Presence Each user ‘checks-in’ once every 3 minsB 00:22am C D 00:23am E 00:24am A 00:25am ? 00:26am A, C, D & E are online at 00:26am
52 Presence Redis natively supports set operations SADDAdd item(s) to a set SREM Remove item(s) from a set SINTER Intersect multiple sets SUNION Union multiple sets SRANDMEMBER Gets a random member from a set ...
53 Presence
54 Redis in Practice #4 leaderboards
55 Leaderboards Gamification Users ranked by some score
56 Leaderboards About sorted sets: Similar to a setEvery member is associated with a score Elements are taken in order
57 Leaderboards Redis has ‘Sorted Sets’ ZADDAdd/update item(s) to a sorted set ZRANK Get item’s rank in a sorted set (low -> high) ZREVRANK Get item’s rank in a sorted set (high -> low) ZRANGE Get range of items, by rank (low -> high) ZREVRANGE Get range of items, by rank (high -> low) ...
58 Leaderboards
59 Redis in Practice #5 Queues
60 Queues Redis has push/pop support for listsAllows you to use list as queue/stack LPOP Remove and get the 1st item in a list LPUSH Prepend item(s) to a list RPOP Remove and get the last item in a list RPUSH Append item(s) to a list
61 Queues Redis supports ‘blocking’ pop Message queues without polling!BLPOP Remove and get the 1st item in a list, or block until one is available BRPOP Remove and get the last item in a list, or block until one is available
62 Queues
63 Redis Supports data structures No built-in clusteringMaster-slave replication Redis Cluster is on the way... Redis is very good at quirky stuff you’d never thought of using a database for before!
64 Membase Written in Erlang & C Membase = Memcached + … Disk persistenceReplication Dynamic cluster configuration
65 Membase Super fast (200k+ ops/sec) Very nice web GUI
66 Membase Cluster Membase Cluster Clients Clients8k ops/sec per server x 6 = 48k ops/sec Membase Cluster 8k ops/sec per server x 3 = 24k ops/sec Membase Cluster Scale Up Scale Down Clients Clients
67 Membase Horizontal scaling means… Semi-automatic scaling up & downLinear increase in throughput Linear increase in cost Semi-automatic scaling up & down Scaling requires NO downtime
68 Membase No queriability No transactions Simple Check-And-Set (cas)
69 Membase Best used for Low-latency data access High concurencyOnline gaming (Zynga, iwi, …)
70 Friends or Foes? Sql vs Nosql
71 A.C.I.D Atomicity Consistency Isolation DurabilityAtomicity – a transaction is all or nothing. Consistency – only valid data is written to the database. Isolation – pretend all transactions are happening serially and the data is correct. Durability – what you write is what you get. Problem with ACID is that trying to guarantee atomic transactions across multiple nodes and making sure that all data is consistent and update is HARD. To guarantee ACID under load is down right impossible, which was the premises of Eric Brewer’s CAP theorem as we saw earlier. However, to minimise downtime, we need multiple nodes to handle node failures, and to make a scalable system we also need many nodes to handle lots and lots of reads and writes.
72 B.A.S.E Basically Available Soft state Eventually consistentIf you can’t have all of the ACID guarantees you can still have two of CAP, which again, stands for: Consistency – data is correct all the time Availability – you can read and write your data all the time Partition Tolerance – if one or more node fails the system still works and becomes consistent when the system comes online If you drop the consistency guarantee and accept that things will become ‘eventually consistent’ then you can start building highly scalable systems using an architectural approach known as BASE: Basically Available – system seems to work all the time Soft State – the state doesn’t have to be consistent all the time Eventually Consistent – becomes consistent at some later time
73 Before we go... Summaries
74 Considerations In memory? Disk-backed persistence?Managed? Database As A Service? Cluster support?
75 SQL or NoSQL? Wrong question What’s your problem? TransactionsAmount of data Data structure
76 Key-Value Store Fast Good for constant stream of small reads and writes Good fit for Social gaming
77 Document Store Natural data modelling Programmer friendly Web friendlyCRUD
78
79 Dynamo DB Fully managed Provisioned through-putPredictable cost & performance SSD-backed Auto-replicated
80 Google BigQuery Game changer for Analytics industryAnalyze billions of rows in seconds SQL-like query syntax Prediction API NOT a database system And lastly, I’d like to make a honorary mention of a new product from Google that’s likely going to be a complete and utter game changer for the analytics industry. With BigQuery, you can easily load billions of rows of data from Google Cloud Storage in CSV format and start running ad-hoc analysis over them in seconds. To make queries against data table in BigQuery, you can use a SQL-like syntax and output the summary data to a Google spreadsheet directly. In fact, you can write your queries in ‘app script’ and trigger them directly from the Google spreadsheet as you would a macro in Excel! There is also a Predication API which makes analysing your data to give predication a snip! However, it’s still early days and there are a lot of limitations on table joins. And you need to remember that BigQuery is NOT a database system, it doesn’t support table indexes or other database management features. But it’s a great tool for running analysis on vast amounts of data at a great speed.
81 Scalability Success can come unexpectedly and quicklyNot just about the DB
82 Thank You! @theburningmonk