K E Y-VA LU E D ATA B A S E S
Shreya Sridhar
Deepika c
2
CONTENTS
o What Is a Key-Value Store
o Key-Value Store Features
o Suitable Use Cases
o When Not to Use
o Summary
Crypto: investing & trading
W H A T I S A K E Y- V A L U E
STORE
• Key-value stores are the simplest NoSQL data stores to use
from an API perspective.
• The client can either get the value for the key, put a value for
a key, or delete a key from the data store.
• Since key-value stores always use primary-key access, they
generally have great performance and can be easily scaled.
• Some of the popular key-value databases are Riak , Redis
(often referred to as Data Structure server) , Memcached DB
and its flavors , Berkeley DB, HamsterDB (especially suited for
embedded use) , Amazon DynamoDB (not open-source), and
Project Voldemort (an open-source implementation of Amazon
DynamoDB).
W H A T I S A K E Y- V A L U E
STORE
• In some key-value stores, such as Redis, the aggregate being
stored does not have to be a domain
• object—it could be any data structure. Redis supports storing
lists, sets, hashes and can do range,
• diff, union, and intersection operations. These features allow
Redis to be used in more different ways
• than a standard key-value store.
• There are many more key-value databases and many new
ones are being worked on at this time.
• For the sake of keeping discussions in this book easier we will
focus mostly on Riak. Riak lets us
W H A T I S A K E Y- V A L U E
STORE
• In some key-value stores, such as Redis, the aggregate being
stored does not have to be a domain
• object—it could be any data structure. Redis supports storing
lists, sets, hashes and can do range,
• diff, union, and intersection operations. These features allow
Redis to be used in more different ways
• than a standard key-value store.
• There are many more key-value databases and many new
ones are being worked on at this time.
• For the sake of keeping discussions in this book easier we will
focus mostly on Riak.
W H AT I S A K E Y-VA L U E S T O R E
K E Y- V A L U E S T O R E F E A T U R E S
While using any NoSQL data stores, there is
an inevitable need to understand how the
features
compare to the standard RDBMS data stores
that we are so used to. The primary reason is
to
understand what features are missing and
how does the application architecture need to
change to
better use the features of a key-value data
store. Some of the features we will discuss for
all the
8
K E Y-VA L U E S T O R E F E AT U R E S
Consistency
o Consistency is applicable only for operations on a single key,
since these operations are either a get,
o put, or delete on a single key. Optimistic writes can be
performed, but are very expensive to
o implement, because a change in value cannot be determined by
the data store.
Transactions
o Different products of the key-value store kind have different
specifications of transactions.
o Generally speaking, there are no guarantees on the writes.
Many data stores do implement transactions in different ways.
o Riak uses the concept of quorum (“Quorums,” p. 57)
implemented by using the W value
9
K E Y-VA L U E S T O R E F E AT U R E S
Query Features
o All key-value stores can query by the key—and that’s about it.
o If you have requirements to query by using some attribute of
the value column, it’s not possible to use the database: Your
application needs to read the value to figure out if the
attribute meets the conditions.
Structure of Data
o Key-value databases don’t care what is stored in the value part
of the key-value pair. The value can be a blob, text, JSON, XML,
and so on.
o In Riak, we can use the Content-Type in the POST request to
specify the data type.
10 K E Y-VA L U E S T O R E F E AT U R E S
Scaling
o Many key-value stores scale by using sharding (“Sharding,” p.
38).
o With sharding, the value of the key determines on which node
the key is stored.
o Let’s assume we are sharding by the first character of the
key; if the key is f4b19d79587d, which starts with an f, it will
be sent to different node than the key ad9c7a396542.
o This kind of sharding setup can increase performance as more
nodes are added to the cluster.
o Sharding also introduces some problems. If the node used to
store f goes down, the data stored on
o that node becomes unavailable, nor can new data be written
with keys that start with f.
S U I TA B L E U S E C A S E S
S U I TA B L E U S E C A S E S
Storing Session Information
o Generally, every web session is unique and is assigned a unique
sessionid value.
o Applications that store the sessionid on disk or in an RDBMS will
greatly benefit from moving to a key-value store,since everything
about the session can be stored by a single PUT request or retrieved
using GET.
o This single-request operation makes it very fast, as everything about
the session is stored in a single object.
o Solutions such as Memcached are used by many web applications,
S U I TA B L E U S E C A S E S
User Profiles, Preferences
o Almost every user has a unique userId, username, or some other
attribute, as well as preferences such as language, color, timezone,
which products the user has access to, and so on.
o This can all be put into an object, so getting preferences of a user
takes a single GET operation. Similarly, productprofiles can be stored.
Shopping Cart Data
o E-commerce websites have shopping carts tied to the user. As we want
the shopping carts to be available all the time, across browsers,
machines, and sessions, all the shopping information can be put into
WHEN NOT TO USE
There are problem spaces where key-value
stores are not the best solution.
15 WHEN NOT TO USE
Relationships among Data
o If you need to have relationships between different sets of data, or
correlate the data between different sets of keys, key-value stores are
not the best solution to use, even though some key-value stores
provide link-walking features.
Multioperation Transactions
o If you’re saving multiple keys and there is a failure to save any one of
them, and you want to revert or roll back the rest of the operations,
key-value stores are not the best solution to be used.
16 WHEN NOT TO USE
Query by Data
o If you need to search the keys based on something found in
the value part of the key-value pairs, then key-value stores are not
going to perform well for you. Multioperation Transactions If you’re
saving multiple keys and there is a failure to save any one of them,
and you want to revert or roll back the rest of the operations, key-
value stores are not the best solution to be used.
Operations by Sets
Since operations are limited to one key at a time, there is no way to
operate upon multiple keys at the same time. If you need to operate
THANK
YO U
Deepika Chandrashekar
Shreya sridhar D