Skip to content

Instantly share code, notes, and snippets.

@VeerpalBrar
VeerpalBrar / quorum.rb
Last active May 11, 2022
Simple Quorum example in ruby
View quorum.rb
# Node is an abstraction of the db
class Node
attr_reader :id
def initialize(id)
@id = id
@data = {}
end
def read(key)
@data[key]
end
@VeerpalBrar
VeerpalBrar / validate_associated_n_queries.rb
Last active Dec 21, 2021
Fixing N+1 queries when using `validates_associated` with `has_many`
View validate_associated_n_queries.rb
# Blog post: https://veerpalbrar.github.io/blog/2021/11/26/Fix-N+1-queries-when-using-validates_associated-with-has_many
### Setup
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rails"
gem "sqlite3"
end
@VeerpalBrar
VeerpalBrar / consistent_hashing.rb
Last active Oct 26, 2021
A consistent hashing implementation in ruby
View consistent_hashing.rb
require 'digest'
class ConsistentHashing
def initialize(nodes)
nodes.map { |node| add_node(node) }
end
def find_cache(key)
puts
hash = hash_value(key)