• Stars
    star
    252
  • Rank 155,495 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created 12 months ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Drop-in enhancement for Rails cache, offering better performance and compression with Brotli algorithm

Rails Brotli Cache Gem Version CircleCI

This gem enables support for compressing Ruby on Rails cache entries using the Brotli compression algorithm. RailsBrotliCache::Store offers better compression and performance compared to the default Rails.cache Gzip, regardless of the underlying data store. The gem also allows specifying any custom compression algorithm instead of Brotli.

I'm currently working on a post describing the gem in more detail. You can subscribe to my blog's mailing list or follow me on Twitter to get notified when it's out.

Benchmarks

Brotli cache works as a proxy layer wrapping the standard cache data store. It applies Brotli compression instead of the default Gzip before storing cache entries.

redis_cache = ActiveSupport::Cache::RedisCacheStore.new(
  url: "redis://localhost:6379"
)
brotli_redis_cache = RailsBrotliCache::Store.new(redis_cache)

~25% better compression of a sample JSON object:

json = File.read("sample.json") # sample 435kb JSON text
json.size # => 435662
redis_cache.write("json", json)
brotli_redis_cache.write("json", json)

## Check the size of cache entries stored in Redis
redis = Redis.new(url: "redis://localhost:6379")
redis.get("json").size # => 31698 ~31kb
redis.get("br-json").size # => 24058 ~24kb

~20% better compression of a sample ActiveRecord objects array:

users = User.limit(100).to_a # 100 ActiveRecord objects
redis_cache.write("users", users)
brotli_redis_cache.write("users", users)

redis.get("users").size # => 12331 ~12kb
redis.get("br-users").size # => 10299 ~10kb

~25% faster performance for reading/writing a larger JSON file:

json = File.read("sample.json") # sample ~1mb JSON text

Benchmark.bm do |x|
  x.report("redis_cache") do
    100.times do
      redis_cache.write("test", json)
      redis_cache.read("test")
    end
  end

  x.report("brotli_redis_cache") do
    100.times do
      brotli_redis_cache.write("test", json)
      brotli_redis_cache.read("test")
    end
  end

  # ...
end

# redis_cache  1.782225   0.049936   1.832161 (  2.523317)
# brotli_redis_cache  1.218365   0.051084   1.269449 (  1.850894)
# memcached_cache  1.766268   0.045351   1.811619 (  2.504233)
# brotli_memcached_cache  1.194646   0.051750   1.246396 (  1.752982)
# file_cache  1.727967   0.071138   1.799105 (  1.799229)
# brotli_file_cache  1.128514   0.044308   1.172822 (  1.172983)

Regardless of the underlying data store, Brotli cache offers 20%-40% performance improvement.

You can run the benchmarks by executing:

cp docker-compose.yml.sample docker-compose.yml
docker compose up -d
cd benchmarks
bundle install
bundle exec ruby main.rb

Configuration

Gem works as a drop-in replacement for a standard Rails cache store. You can configure it with different store types:

config.cache_store = RailsBrotliCache::Store.new(
  ActiveSupport::Cache::RedisCacheStore.new(url: "redis://localhost:6379")
)
config.cache_store = RailsBrotliCache::Store.new(
  ActiveSupport::Cache::MemCacheStore.new("localhost:11211")
)
config.cache_store = RailsBrotliCache::Store.new(
  ActiveSupport::Cache::FileStore.new('/tmp')
)

You should avoid using it with ActiveSupport::Cache::MemoryStore. This type of cache store does not serialize or compress objects but keeps them directly in the RAM. In this case, adding this gem would reduce RAM usage but add huge performance overhead.

Gem appends br- to the cache key names to prevent conflicts with previously saved entries. You can disable this behavior by passing { prefix: nil } during initialization:

config.cache_store = RailsBrotliCache::Store.new(
  ActiveSupport::Cache::RedisCacheStore.new,
  { prefix: nil }
)

Addition of the prefix means that you can safely add the Brotli the cache config and avoid compression algorithm conflicts between old and new entries. After configuring the Brotli cache you should run Rails.cache.clear to remove the outdated (gzipped) entries.

Use a custom compressor class

By default gem uses a Brotli compression, but you can customize the algorithm. You can pass a compressor_class object as a store configuration argument or directly to read/write/fetch methods:

config.cache_store = RailsBrotliCache::Store.new(
  ActiveSupport::Cache::RedisCacheStore.new,
  { compressor_class: Snappy }
)
Rails.cache.write('test-key', json, compressor_class: Snappy)

This config expects a class which defines two class methods inflate and deflate. It allows you to instead use for example a Google Snappy algorithm offering even better performance for the cost of worse compresion ratios. Optionally, you can define a custom class wrapping any compression library.

Testing

cp docker-compose.yml.sample docker-compose.yml
docker compose up -d
rake test_all

More Repositories

1

rails-pg-extras

Rails PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Ruby
1,072
star
2

termit

Translations with speech synthesis in your terminal as a ruby gem
Ruby
507
star
3

ecto_psql_extras

Ecto PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Elixir
351
star
4

normit

Translations with speech synthesis in your terminal as a node package
JavaScript
239
star
5

activerecord-analyze

Add EXPLAIN ANALYZE to Rails Active Record query objects
Ruby
209
star
6

smart_init

A simple gem for eliminating Ruby initializers boilerplate code, and providing unified service objects API
Ruby
177
star
7

ruby-pg-extras

Ruby PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Ruby
123
star
8

node-postgres-extras

NodeJS PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
JavaScript
70
star
9

ecto_extras

Ecto helper functions.
Elixir
36
star
10

devloop

An automated test runner for Rails that instantly executes specs based on a recent git diff output.
Ruby
36
star
11

lazyme

A simple gem to help you optimize your shell workflow
Ruby
35
star
12

python-pg-extras

Python PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Python
35
star
13

Locker

Ethereum Smart Contracts for locking Ether, ERC20 and ERC721 tokens based on time and price conditions
TypeScript
28
star
14

WaitForIt

Events and time based iOS app scenarios made easy.
Swift
26
star
15

.dotfiles

My development environment settings.
Shell
13
star
16

haskell-pg-extras

Haskell PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Haskell
9
star
17

ruby-jemalloc-node-yarn

Docker image of Ruby with Jemalloc Node 16 LTS and Yarn
Dockerfile
9
star
18

railsSearchKit

This Chrome extension provides easy access to the search bars every Rails developer needs.
JavaScript
8
star
19

dont_you_count

Disable count queries for selected Active Admin tables.
Ruby
7
star
20

pi-hole-docker-compose

pi-hole-docker-compose
6
star
21

activerecord-implicit-order

Ruby
5
star
22

delegate_it

A drop in replacement for ActiveSupport delegate method in non Rails projects.
Ruby
4
star
23

active-admin-tips

Active Admin tips and performance optimizations in action
Ruby
4
star
24

rust-pg-extras

Rust PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Rust
3
star
25

Siorbackend

Ruby
1
star
26

abstract_base

Abstract Class pattern Ruby gem
Ruby
1
star
27

FRP_introduction

Comparison between observer and reactive approach to login form validations.
JavaScript
1
star
28

focus.apki.io

Landing page for Focus app
HTML
1
star