Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
com.codeablereason.restcompress.provider:rest-compress-lib
Advanced tools
Library providing additional compression options for RestEasy
Fast HTTP Compression For RestEasy
#Why? Have you noticed that you're spending a significant amount of bandwidth and transfer time sending fluffy JSON or XML data from your Java RESTful Web services? Wishing there were some way to reduce that, but without GZIP's high CPU overhead slowing things down? Look no further, here's your solution. :-)
Rest-compress extends the popular RestEasy JAX-RS framework, by adding support for fast compression. All you have to do is add an annotation to your methods: it's as easy as GZIP, but fast enough to use within the data center or to the cloud.
The initial version uses the LZF algorithm, based on Tatu Saloranta's excellent Compress-LZF library. For compatibility with other libraries, it generates an output format compatible with the C LZF implementation. There are LZF libraries for all the popular languages. So, you can run a Java server, but easily add support in your Python or Ruby front-end!
In the future I hope to add support for additional compression methods (including XML/JSON specific compression formats).
#LZF? LZF is a speed-optimized compression algorithm in the Lempel-Ziv (LZ) family, optimized for speed over compression ratio. The more popular DEFLATE/GZIP algorithm combines LZ with Huffman coding for better compression, but is significantly slower. With GZIP, you spend a lot of CPU time to get your compression, and on a LAN connection it will often reduce application performance.
LZF vs. GZIP for JSON/XML (TPS is MB/s, size % is ratio of compressed to uncompressed data)
###There are other fast compression algorithms out there, so why LZF?
More info on LZF: C LZF documentation
#How To Use: To integrate in your projects:
<dependency>
<groupId>com.codeablereason.restcompress.provider</groupId>
<artifactId>rest-compress-lib</artifactId>
<version>0.6</version>
</dependency>
import org.restcompress.provider.LZF;
@GET
@Path("/object")
@LZF
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
/** Returns a sample serialized object */
public KeyValue getObject();
#Benchmarks: Here are some benchmarks to whet the appetite. Methodology and machine specs are at the bottom.
##Network Benchmark 1: Serving Static Content
Demo App REST URL: {hostname}:8080/rest/static (all processing time should be for serialization & compression) For GZIP, using: {hostname}:8080/rest/static/gzip
Compression | Request Time (s) | Avg Data Size (kB) |
---|---|---|
none | 0.03310 | 1842761.0 |
LZF | 0.02660 | 174164.0 |
GZIP | 0.05441 | 90949.0 |
As you can see, LZF reduces file size by 90.5% (not as good as GZIP's 95.1%) but is considerably faster. Where GZIP's CPU-expensive compression reduces performance by 39.2%, LZF improves overall REST response time by 24.4% (!) despite being on a very fast network.
##Network Benchmark 2: Serving Generated "dummy" data
Calling demo app method: {hostname}:8080/rest/complex/10000* (JSON return)*
Compression | Request Time (s) | Avg Data Size (kB) |
---|---|---|
none | 0.05938 | 1766648.424 |
LZF | 0.05239 | 173025.076 |
Request time is measured from sending request until response fully received.
Even with nontrivial processing to generate data, this yields an 89.8% reduction in bandwidth, giving a 10% performance boost. This is despite being on fast LAN connection (10 GBit shared between VMs).
#Dedicated LZF vs. GZIP Compression Benchmarks This gives some idea of the compression speed, as a comparison with GZIP, and for estimating when LZF is advantageous. It will vary with the hardware used, of course. Your mileage will vary. Full benchmark results are here.
Compression | Avg Round-Trip Speed (MB/s) | Compression Ratio |
---|---|---|
LZF | 174.596 | 29.803% |
GZIP | 30.851 | 18.172% |
Round-trip speed is the total rate at which data can be compressed and decompressed. It is reported as the harmonic mean of the speeds for all files in the corpus. Harmonic mean is used here instead of arithmetic mean, because it better represents throughput, as it does not disproportionately weight the maximum values.
As you can see, LZF is about 5x faster than GZIP, at the cost of a somewhat reduced compression. For our hardware, we see that:
#How it works:
Client-side: Client adds HTTP header "Accept-Encoding: lzf" to all REST requests and uses LZF encoding for PUT/POST request bodies, setting the 'Content-Encoding: lzf' header.
Server-side: If client request has 'Accept-Encoding: lzf' header set, the response will be LZF compressed and the 'Content-Encoding: lzf' header set. If the client request body has header 'Content-Encoding: lzf' then the request body will be decompressed as LZF before further processing (deserialization, etc).
What this means: You can use a client that is not LZF-aware and LZF simply won't be used. This means you can easily test & debug REST APIs by the usual tools (curl, browser, etc). Thanks to the magic of HTTP Headers, you can call the RESTful services from non-Java clients without LZF compression, and then later build this into them as you choose.
#Gotchas: You may not be able to use both @GZIP and @LZF annotations on the same REST method. They simply don't play well together, because of how the interceptors for GZip are implemented. For now, pick one method or the other and stick to it.
#Demo: I've included a trivial REST demo app in the rest-demo-app module, which can be used as an example. It is what I used in manual testing, as well. To deploy, build it and drop the WAR in a JBoss Application Server 7 instance (in the deployments folder).
#Testing: You may notice that there aren't unit tests. This is because the Interceptors only fire when actively running in a container and this project does not provide functionality separate from this.
To work around this, manual testing was done (using the demo app) to verify that:
#Future Plans: The following additions are planned at some point, and are listed below in priority order so consumers are aware that they are already planned. No ETA when they will be completed, however.
##Benchmark Methodology
###Network Tests:
###LZF Tests:
##Benchmark Machine:
Host Hardware:
FAQs
Library providing additional compression options for RestEasy
We found that com.codeablereason.restcompress.provider:rest-compress-lib demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.