Database Reference
In-Depth Information
Storing large attribute values
We know that currently DynamoDB puts size constraints on each item that we put or re-
trieve. Also, each and every call to DynamoDB is money for us. So, in order to reduce the
data size and ultimately the cost, we have various ways to deal with large attributes. Here
are a few such techniques.
Using compressions
In Chapter 2 , Data Models , we saw the data model of DynamoDB, where we covered vari-
ous DynamoDB data types. We also saw what the binary data type is and how it is used.
When we declare the data type of a certain attribute as binary, we would expect a huge at-
tribute to get stored in it. So, in such cases, it is recommended that we compress such a bin-
ary attribute using well-known compression algorithms, such as gzip, LZO, and so on.
So, when we are going to store an item, we encode it using a certain algorithm that would
reduce its length and size, and while retrieving that value, we decode it using a similar al-
gorithm to get the original value back. This technique helps us to reduce the size of the data
stored and retrieved, ultimately reducing the cost.
But one thing we should keep in mind is that even though using these algorithms would
save storage space and money, it would also increase CPU time as we might need to invest
some time in encoding and decoding the data. So, if you are comfortable with a slight delay
in your data retrievals, then you must go and implement this technique and save some
bucks for you and your organization.
The following is a sample Java code that you can use to compress a long string into gzip:
public String compress(String inputString) throws
IOException {
if (inputString == null || inputString.length() ==
0) {
return inputString;
}
ByteArrayOutputStream out = new
ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(inputString.getBytes());
gzip.close();
Search WWH ::




Custom Search