Rate limiting using Redis in a Rails app

The web is a weird place. You go to sleep thinking that you have a perfectly functional web application and the next day when you wake up, you might find yourself staring at a sudden huge spike in the number of requests. Either your app got popular overnight or you were just a victim of a DOS attack trying to bring your app server down. Usually, it's the latter. There are some popular gems like rack-attack and rack-throttle which work quite well and provides a lot of flexibility. But if you're looking to write your custom logic with minimum dependencies,

Setting up Sidekiq with Redis for background job processing

Before starting with setup process lets first briefly go through what Sidekiq is? and how it works? Sidekiq provides very handy and efficient way to perform most of our work asynchronously in a job. Provided Redis cache connection to it, it serializes all job parameters in Redis and deserializes it while picking it up for execution based on priority of job as it uses queue data structure to store each job in particular queue specified in job and each queue has its priority defined as high, medium, low etc. To setup Sidekiq with Redis in your application you can follow

Javascript Browser Window Operations in Rails

Recently we developed one feature where we wanted to present a new tab view for an existing smaller form interface whereby users can use the full screen of the new window to move around and use the feature with ease. While we were developing the feature, we came across few challenges and thereby few learnings which we thought would be good to share. 1. Caching existing values. We have around 8-9 forms from where we can use this feature but each of them have different implementation based on the requirement. So before we open the new tab - we wanted

Clear Redis cache and Solr indexes

As I am working in an app which needs keys to be stored in Redis and index data in Solr, due to redis eviction policy, sometimes keys gets evicted, but index in solar remains same, which result in confusing local behavior for me. To clear local redis copy, I do it from redis-cli: $ redis-cli 127.0.0.1:6379> FLUSHDB OK 127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> exit To remove Solr indexes, I use curl: $ curl http://localhost:8982/solr/update?commit=true -d '**:**'

Fragment caching and sweeping strategy

Rails provides three types of caching techniques: page caching action caching fragment caching The rails_guides_on_caching is the best place to know how it works. This article is to demonstrate fragment caching the Rails Way with various data-stores and clearing those cached fragments on ActiveRecord callbacks on CRUD operations. Rails ActiveSupport::Cache::Store is responsible to do all the read and write operations. 1. The setup By default caching is disabled in rails. You will find the following config commented out in production.rb # Use a different cache store in production # config.cache_store = :mem_cache_store You

Caching with Redis

What is Redis? Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. Why should we use Redis? By default, Rails uses the database to cache internal application data which can be expensive to generate (menu trees, views, filter results, etc), and to keep cached page contents. Since the database also handles many queries for normal page requests, it can create a bottleneck and increase load-times. Redis provides an alternative caching backend for Rails, taking that work off the database, which is vital for scaling to a larger number of logged-in

Example usage of Redis - LUA script in Rails

This topic is about using Redis and LUA script in Rails. Before proceeding, you may like to read [ Introduction to LUA script with Redis][1] Redis doesn't have any command to calculate total number of keys We'll use [zrangebyscore][2] to find number of members between index range. Instal [redis-rb][3] Start rails console Prepare a dataset based on timestamp > t10 = 10.minutes.ago => 1386935151 > t20 = 20.minutes.ago => 1386934568 > t30 = 30.minutes.ago => 1386933971 > t40 = 40.minutes.ago => 1386933374 This is non-realistic data, may vary for you. Populate redis with Sorted Set > redis = Redis.new # initialize redis => # zadd -

Introduction to LUA script with Redis

What is Redis ? We all might be familiar with [Redis][1] . For those who are not - It's a "NoSQL" key-value data store. More precisely, it is a data structure server. To read more - [Redis-Wiki][2] Do [try redis][3] it has a nice interactive tutorial. To install and setup redis on your machine see [how-to-install-and-use-redis][4] What is LUA ? LUA is an embeddable scripting language. About LUA - http://www.lua.org/about.html Redis >= 2.6 supports embedded scripting language. Lets give a try to it. To run a script in redis we need

Suggestions On Redis, Backing-Up Redis And Setting up production environment for redis

Redis installation at local is one of the easiest things to do. However, There are some gotchas involved when we are trying to use redis. Running redis-server without config(Default Config File will be picked). When we run redis-server without config, then it creates a dump.rb(A physical file to store all data related to redis) at the folder from which we are starting the redis. We normally, don't realize the impact of this issue as we start the redis server at the same project folder every-time. However, If we start the redis from the some other folder, then