you know what a spinlock is, right?
here's a spinlock:
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;
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 | |
} |
-- | |
-- 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); |
#!/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 | |
} |
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.
Keep it simple, stupid. You ain't gonna need it.
-- 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() | |
); |
#!/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 { |