Data compression
Sending large volumes of log data over the network increases latency and cost. Simple Log Service supports lz4 compression for select API operations, reducing network traffic and improving throughput without data loss.
Compress request data
The following API operations accept lz4-compressed data in the HTTP request body.
PutLogs (PutLogStoreLogs)
PutWebtracking
To send compressed data:
Add
x-log-compresstype: lz4to the HTTP request header.Compress the HTTP request body using lz4.
Set
x-log-bodyrawsizein the request header to the uncompressed body size.Set
Content-Lengthin the request header to the compressed body size.
Receive compressed data
The PullLogs API operation can return lz4-compressed data.
To receive compressed data:
Set
Accept-Encoding: lz4in the request header. The server returns lz4-compressed data.Read
x-log-bodyrawsizefrom the response header to get the uncompressed body size. Pass this value to your decompression function.
Examples
-
Raw logs
The examples use log-sample.json as sample input. Replace this with your actual log data when calling the API.
{ "__tags__": {}, "__topic__": "", "__source__": "47.100.XX.XX", "__logs__": [ { "__time__": "03/22 08:51:01", "content": "*************** RSVP Agent started ***************", "method": "main", "level": "INFO" }, { "__time__": "03/22 08:51:01", "content": "Specified configuration file: /u/user10/rsvpd1.conf", "method": "locate_configFile", "level": "INFO" }, { "__time__": "03/22 08:51:01", "content": "Using log level 511", "method": "main", "level": "INFO" }, { "__time__": "03/22 08:51:01", "content": "Get TCP images rc - EDC8112I Operation not supported on socket", "method": "settcpimage", "level": "INFO" }, { "__time__": "03/22 08:51:01", "content": "Associate with TCP/IP image name = TCPCS", "method": "settcpimage", "level": "INFO" }, { "__time__": "03/22 08:51:02", "content": "registering process with the system", "method": "reg_process", "level": "INFO" }, { "__time__": "03/22 08:51:02", "content": "attempt OS/390 registration", "method": "reg_process", "level": "INFO" }, { "__time__": "03/22 08:51:02", "content": "return from registration rc=0", "method": "reg_process", "level": "INFO" } ] } -
Test program
The following Python code compresses the sample file and prints the compression ratio:
from lz4 import block with open('log-sample.json', 'rb') as f: data = f.read() compressed = block.compress(data, store_size=False) # Compress print(f'out/in: {len(compressed)}/{len(data)} Bytes') print(f'Compression ratio: {len(compressed)/len(data):.2%}') -
Compression result
For log-sample.json, the compression ratio is 39.30%. Actual results vary by content — data with more repetition compresses more efficiently.
out/in: 542/1379 Bytes Compression ratio: 39.30%