Skip to content

Instantly share code, notes, and snippets.

View rponte's full-sized avatar
🏠
Working from home

Rafael Ponte rponte

🏠
Working from home
View GitHub Profile
@rponte
rponte / avoid-distributed-transactions.md
Last active May 5, 2025 05:19
THEORY: Distributed Transactions and why you should avoid them (2 Phase Commit , Saga Pattern, TCC, Idempotency etc)

Distributed Transactions and why you should avoid them

  1. Modern technologies won't support it (RabbitMQ, Kafka, etc.);
  2. This is a form of using Inter-Process Communication in a synchronized way and this reduces availability;
  3. All participants of the distributed transaction need to be avaiable for a distributed commit, again: reduces availability.

Implementing business transactions that span multiple services is not straightforward. Distributed transactions are best avoided because of the CAP theorem. Moreover, many modern (NoSQL) databases don’t support them. The best solution is to use the Saga Pattern.

[...]

@vy
vy / DemoSpringAppApplication.java
Created March 26, 2019 08:28
How to pass a Spring property to Log4j in a Spring Boot application
package com.vlkan.demo.spring;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.annotation.PostConstruct;
@rponte
rponte / littles-law-and-back-pressure.md
Last active June 20, 2025 20:05
THEORY: Little's Law and Applying Back Pressure When Overloaded

Applying Back Pressure When Overloaded

[...]

Let’s assume we have asynchronous transaction services fronted by an input and output queues, or similar FIFO structures. If we want the system to meet a response time quality-of-service (QOS) guarantee, then we need to consider the three following variables:

  1. The time taken for individual transactions on a thread
  2. The number of threads in a pool that can execute transactions in parallel
@rponte
rponte / HowToUseIt.java
Last active December 30, 2023 14:37
THEORY: Example of a simple Single Thread Pool implementation in Java
public class HowToUseIt {
/**
* Usually we'll have a single instance per client
*/
private static final SingleThreadPool THREAD_POOL = new SingleThreadPool();
public void executeAsync() {
try {
@mattmc3
mattmc3 / modern_sql_style_guide.md
Last active July 18, 2025 12:26
Modern SQL Style Guide
layout author title revision version description
default
mattmc3
Modern SQL Style Guide
2019-01-17
1.0.1
A guide to writing clean, clear, and consistent SQL.

Modern SQL Style Guide

@alexandreaquiles
alexandreaquiles / EscapeXmlELResolverListener.java
Last active May 7, 2020 07:19
EscapeXmlELResolverListener for Spring Boot based on pukkaone.github.io/2011/01/03/jsp-cross-site-scripting-elresolver.html
package br.com.caelum.fj91.seguranca;
import java.beans.FeatureDescriptor;
import java.util.Iterator;
import javax.el.ELContext;
import javax.el.ELResolver;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.jsp.JspContext;
@jpukg
jpukg / TaskRunnerTest.java
Created October 10, 2017 02:07 — forked from onraz/TaskRunnerTest.java
Using CountDownLatch to Test Asynchronous Code
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
@alexandreaquiles
alexandreaquiles / github.gql
Created August 28, 2017 16:32
Consulta GraphQL que busca estatísticas do projeto Express: o número de stars, de pull requests abertos, de issues abertas, a data da última release e informações do último commit.
query {
repository(owner:"expressjs", name: "express") {
stargazers {
totalCount
}
pullRequests(states: OPEN) {
totalCount
}
issues(states:OPEN) {
totalCount
package monoids;
public abstract class Monoid<T> {
public abstract T combine(T one, T other);
public abstract T identity();
public T fold(Iterable<T> elements) {
T result = identity();
for (T i : elements) {

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.