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
@rafaelpontezup
rafaelpontezup / races-conditions.md
Created October 31, 2022 17:01
Chapter Stackspot: Race conditions, locking e bancos de dados relacionais

Race conditions, locking e bancos de dados relacionais

Pretendo apresentar um código com uma lógica de negócio simples que funciona bem na perspectiva do negócio mas que quebra miseravelmente em ambientes minimamente concorrentes. Para isso, vou demonstrar através de testes de integração como identificar o Race Condition no código e principalmente como resolvê-lo através de mecanismos de locking e Isolation Level do seu banco de dados;

O que vamos discutir

  1. Por que estudamos esse assunto mais a fundo?
  • 1.3. A importância do treino: o que eu levei alguns bons anos para aprender o Jordi levou 6 meses
@rafaelpontezup
rafaelpontezup / LockManager.kt
Created October 4, 2022 20:25 — forked from soudmaijer/LockManager.kt
Postgres transaction-level advisory lock implementation that uses Spring JDBC
import org.slf4j.LoggerFactory
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Transactional
import java.time.Duration
interface LockManager {
fun <T> tryWithLock(key: Long, timeout: Duration, function: () -> T): T
}
@rponte
rponte / buying_new_tickets.sql
Last active September 2, 2024 15:08
PostgreSQL: Playing a little bit with race conditions, SQL and isolation levels
--
-- Trying to buy a new ticket for an event
--
BEGIN;
UPDATE events e
SET updated_at = now()
WHERE e.id = :event_id
AND e.max_tickets > (SELECT count(*) -- (does this logic work with READ_COMMITTED??)
FROM tickets t
WHERE t.event_id = e.id);
@clemensv
clemensv / messaging-and-eventing-platforms.md
Created January 5, 2022 14:43
Elements of Messaging and Eventing Platforms
title
Elements of Messaging and Eventing Platforms

This document provides a brief overview of the essential elements of a messaging and eventing platform and how they relate to each other.

Message and Event Broker Categories

Thread Pools

Thread pools on the JVM should usually be divided into the following three categories:

  1. CPU-bound
  2. Blocking IO
  3. Non-blocking IO polling

Each of these categories has a different optimal configuration and usage pattern.

@fernandrone
fernandrone / topw
Last active February 1, 2021 15:12
Top N most-used words in a text
#!/usr/bin/env sh
#
# Simple script that prints out the top N most-used words in a text from standard input.
#
# Inspired by https://buttondown.email/hillelwayne/archive/donald-knuth-was-framed/. The short
# linux script is first shown at https://www.cs.tufts.edu/~nr/cs257/archive/don-knuth/pearls-2.pdf
topw() {
tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | sed "$N"q
}
@stettix
stettix / things-i-believe.md
Last active August 28, 2024 14:54
Things I believe

Things I believe

This is a collection of the things I believe about software development. I have worked for years building backend and data processing systems, so read the below within that context.

Agree? Disagree? Feel free to let me know at @JanStette. See also my blog at www.janvsmachine.net.

Fundamentals

Keep it simple, stupid. You ain't gonna need it.

@purcell
purcell / taskqueues.sql
Last active March 19, 2021 08:35
Easy task queues using PostgreSQL
-- Let's say you have a table full of work:
CREATE TABLE tasks (
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
status TEXT NOT NULL DEFAULT 'pending',
payload JSON NOT NULL, -- or just have meaningful columns!
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
@TimothyJones
TimothyJones / robust-bash.sh
Last active January 29, 2021 14:30
These are (currently) the only functions I recommend porting around when writing bash scripts
#!/bin/bash -eu
if [ -z "${LIB_ROBUST_BASH_SH:-}" ]; then
LIB_ROBUST_BASH_SH=included
function error {
echo "Error: ${1:-}"
}
# Check to see that we have a required binary on the path
function require_binary {
@rponte
rponte / avoid-distributed-transactions.md
Last active September 19, 2024 21:00
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.

[...]