Compression: Making the Big Smaller and Faster (Part 1)
Making your information perform as quickly and efficiently as possible is key these days. Learn how to make it happen with data compression.
Join the DZone community and get the full member experience.Join For Free
how important is data compression? the sharing of information in a fast and efficient manner has been an area of constant study and research. companies like google and facebook have spent a lot of time and effort trying to develop faster and better compression algorithms. compression algorithms have existed since the ’70s and the ongoing research to have better algorithms proves just how important compression is for the internet and for all of us.
the need for data compression
the world wide web has undergone a lot of changes since it was made available to the public in 1991. believe it or not, the copy of the world’s first website can still be browsed here . back then, web pages were very simple. today, they are increasingly more complex and there is an evident need to have compression algorithms that are lossless, fast, and efficient.
there are several best practices that help optimize page load time. read this blog post to learn about web page optimization techniques. in this article, we will spend some time understanding the basics of compression and how it works. we will also cover a new type of compression method called “brotli” in the second part of this blog.
encoding and data compression
let’s start by understanding what data encoding and compression are:
the word “compression” comes from the latin word compressare , which means to press together. “encoding” is the process of placing a sequence of characters in a specialized format that allows efficient data storage as well as transmission. per wikipedia: “data compression involves encoding information using fewer bits than the original representation.”
compression plays a key role when it comes to saving bandwidth and speeding up your site. modern day websites involve a lot of http requests and responses between the client (the browser) and the server to serve a web page. with an overall increase in the number of http requests and responses, it becomes important to ensure that these transfers are taking place at a fast and efficient rate.
http works on a request-response model, as demonstrated below:
in this case, we are not using any compression method to compress the response being sent by the server.
the browser sends an http request asking for the
- the server looks for the requested file and responds with the requested resource and a 200 ok http status message.
- the browser receives the server’s response and renders the page.
as we can see, in this case, there is no compression involved. the server responded with a 300 kb file (index.html page). if the file size was bigger, it would have taken more time for the response to be sent on the wire and this would have increased the overall page load time. please note that we are currently looking only at a single http response. modern websites receive hundreds of such http responses from the server to render a web page.
the image below shows the same http request — response between the browser and the server, but in this case, we use compression to reduce the size of the response being sent by the server to the browser.
today, complex and dynamic websites generate hundreds of http requests/responses. this made it important to have a system which would ensure fast and efficient data transfer between the server and the browser. this is when compression algorithms like deflate and gzip came into existence.
introduction to gzip
gzip is a compression method that is used to make files smaller for storage and faster transmission over the network. gzip is one of the most popular, powerful, and effective ways of compressing data and it can reduce the file size by up to 70%.
gzip is based on the deflate algorithm, which in turn is a combination of lz77 and huffman coding. understanding how lz77 works is essential to understand how compression methods like deflate and gzip work.
developed in the late ’70s by abraham lempel and jacob ziv, the lz77 method of compression looks for sequences of characters that recur in a text. it performs compression by replacing the recurring occurrences of strings using pointers that backreference identical strings, previously encountered in the text, that needs to be compressed.
the pointer or backreference is of the form <relative jump, length>, where relative jump signifies how many bytes are there between the current occurrence of the string and its last occurrence and length is the total number of identical bytes found.
now let us understand this better with the help of an example. assume, there is a text file with the following text:
as idle as a painted ship, upon a painted ocean.
in this file, we see the following strings: “as” and “painted” occurring multiple times. what lz77 method does is, it replaces multiple occurrences of strings with the notation: <relative jump, length>.
so using lz77, the text will get encoded in the following way:
as idle <8,2> a painted ship, upon a <21,7> ocean.
to encode the text, we took the following steps:
- looked at the string and tried to find occurrences of the same “string” or “substrings”.
- replaced multiple occurrences of a string with the notation: <relative jump, length>; the two strings: “as” and “painted” were replaced the multiple occurrences of the strings with <relative jump, length>.
- the string “painted” which would have earlier occupied 7 bytes (i.e. the number of characters in the word: “painted”) x 1 byte = 7 bytes was compressed to occupy only 2 bytes. 2 bytes or 16 bits is the size of the pointer or backreference.
huffman coding is another lossless data compression algorithm. the frequency of occurrence of a string in a text file or pixels in images form the basis of huffman coding. to get a deeper understanding of this algorithm, read this detailed tutorial that clearly explains how huffman coding works.
for the first test run, we did not specify any encoding to be used by passing the custom header: accept-encoding: identity along with the request. the first image shows no content-encoding being passed for the request.
in the second image, the browser is sending accept-encoding:gzip, for which the server is sending zipped file as the response.
we can clearly see how gzip can drastically compress the files to improve data transmission rate over the wire.
catchpoint’s scheduled tests also highlight the difference between compressed and not-compressed content loading on web pages.
a new compression method called brotli was introduced not too long ago. the brotli compression algorithm is optimized for the web and specifically for small text documents. we will discuss more this compression method and what it has to offer to the world wide web community in the second part of the article.
Published at DZone with permission of Nilabh Mishra. See the original article here.
Opinions expressed by DZone contributors are their own.