Skip to content

Instantly share code, notes, and snippets.

View valarpirai's full-sized avatar

Valar valarpirai

View GitHub Profile
@valarpirai
valarpirai / Database race condition.md
Last active May 1, 2024 23:06
Database Race conditions

How to Solve Race Conditions in a Booking System

Database isolation refers to the level of isolation between concurrent transactions in a database. Isolation levels control the visibility and accessibility of data to concurrent transactions and can affect the occurrence of race conditions in a database. If your isolation level is not “serializable” - there is a possibility of race conditions.

The Serializable isolation level provides the strictest transaction isolation. This level emulates serial transaction execution for all committed transactions; as if transactions had been executed one after another, serially, rather than concurrently

Race conditions

Example of Race condition, Hotel room booking and movie ticket booking etc

#!/usr/bin/env python
#
# Converts any integer into a base [BASE] number. I have chosen 62
# as it is meant to represent the integers using all the alphanumeric
# characters, [no special characters] = {0..9}, {A..Z}, {a..z}
#
# I plan on using this to shorten the representation of possibly long ids,
# a la url shortenters
#
@valarpirai
valarpirai / Example.java
Created April 27, 2024 10:03
Java Thread Race Condition example
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Example {
public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newCachedThreadPool();
int max_thread = 1000;
Increment eg = new Increment();
@valarpirai
valarpirai / RateLimitter.ipynb
Last active April 16, 2024 04:22
Practice DSA
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@valarpirai
valarpirai / Cheat Sheet.md
Last active April 12, 2024 16:41 — forked from vasanthk/System Design.md
System Design Cheatsheet

Golden rules for System Design

👉 For a Read-Heavy System - Consider using a Cache.

👉 For a Write-Heavy System - Use Message Queues for async processing

👉 For a Low Latency Requirement - Consider using a Cache and CDN.

👉 If we need a system to be ACID complaint, we should go for RDBMS or SQL Database

const credentials = require('./credentials.json');
const googleAuth = require('google-oauth-jwt')
const config = {
"private_key": credentials.private_key,
"client_email": credentials.client_email
}
const getToken = () => {
return new Promise((resolve) => {
googleAuth.authenticate(
class TrieNode
attr_accessor :childrens, :leaf
def initialize
self.childrens = {}
self.leaf = false
end
end
class WordDictionary
@valarpirai
valarpirai / trie.rb
Created January 31, 2023 13:14
Design Add and Search Words Data Structure
class TrieNode
attr_accessor :childrens, :leaf
def initialize
self.childrens = {}
self.leaf = false
end
end
class WordDictionary
@valarpirai
valarpirai / message_break.rb
Created January 31, 2023 11:13
SMS break messages | Twilio
MAX_LINE_LENGTH = 80
# Gets long string as input
# Retruns array of splitted strings with (1/3)
def split_msg(message)
length = message.length
return [message] if length < MAX_LINE_LENGTH