Skip to content

Instantly share code, notes, and snippets.

@matthiasr
matthiasr / event_reporting.md
Created July 18, 2022 16:35
The website says I shouldn't use Prometheus for billing, what should I do?

That's a whole system design interview question right there 😉

Fundamentally, if you need that level of detail and fidelity, you compromise on timeliness. You (somehow) record an event for every message sent, then tally those up on a daily or monthly basis.

It depends on the system and the details of your requirements. I would start by looking at how messages are being sent to begin with. For example, if there's a job in a database, you might be able to count the jobs marked as completed right there.

If the message system is fed by a Kafka topic, and you can get away with billing for messages attempted to send, you can count those from the source topic.

You can also explicitly record events, say into another Kafka topic or a database table. You will need to make some choices about CAP un that case: if recording the event fails, do you want to send anyway? What if you're not sure recording it worked?

Kubernetes defaults to scaling by CPU usage, because that is what is always available. However, this is not a great metric to scale on. Most backend services are not CPU-bound – they mostly wait for responses from other services, caches, or databases. If one of those gets slow, or worse, talking to one of those gets slow, CPU-based scaling will tear down resources rather than scaling out, because all it sees is "idle" instances. This is especially bad if the contended resource is concurrency on those network requests. If many requests are waiting to check out a connection from the database connection pool, scaling down is what you want the least.

My favorit metric to scale on is how many requests you have currently ongoing (per instance). There's a relationship between latency, request rate, and this number of ongoing requests, that means that say a 20% increase in latency at the same request rate, or a 20% increase in requests at the same latency, result i

@matthiasr
matthiasr / 01.md
Created May 19, 2022 07:50
What makes operating databases more complex than stateless services?

To me, the fundamental difficulty in managing databases is the amount of state they have, the time it takes to move that state around, and the difficulty in keeping it in sync.

Whether it's Cassandra, MySQL, or PostgreSQL, bringing up a new instance takes time, orders of magnitude more than replacing some stateless service. Network mountable volumes help, because that state mostly lives on whatever provides that, but you still need to account for "moving" it into the cache.

Additionally, you necessarily have shared state that you cannot ever reset. A lot of the usefulness from "cattle" servers is that you have a clear way to reset them to a known good state. In most cases you cannot do that with your data.

Some of the mechanics of a cattle management system like Kubernetes still work but they're on timelines that give the cluster operators headaches.

@matthiasr
matthiasr / questions.md
Created December 3, 2021 07:38
Someone asked: How can I be less annoying to collaborate with?

A great technique that a manager set me on the path towards, and that I learned over the last years is to ask many good questions. this takes practice. a good question pushes the other person towards an insight, but lets them supply their own experiences and gives them the satisfaction of figuring something out vs. being told what to do.

For example, instead of stating "this container needs a readiness probe", you could ask "what would a good readiness probe be for this container?", or even more open "how can we make sure this gets taken out of traffic when it breaks? how can we detect that it's broken?"

I found the questioning approaches from Resilience Engineering/LFI helpful, especially to never ask why – it's always an accusation. Better questions are How and What. Sometimes it takes me a few iterations in my head to go from "Why" to "What b

@matthiasr
matthiasr / 1_intro.md
Last active May 9, 2022 14:45
Notes on operating cluster queues

Someone asked in chat (paraphrased) "I am coming from synchronous online systems, and now am going to work on a queue-based system. What should I be aware of?" This is a (somewhat freely-associated) collection of thoughts, based on my experience at SoundCloud.

@matthiasr
matthiasr / delete-s3.sh
Created December 14, 2020 09:18
Delete everything in an S3 bucket (in a hurry)
# I needed to delete ~300k objects from an S3 bucket in a hurry. The better and cheaper solution is to use a lifecycle rule, but those can take a day or two to take effect.
bucket="i-want-to-lose-all-my-data"
mkdir -p deletes
# 1. List all the objects in the bucket, transform them 1000 at a time into request objects for DeleteObjects, write each to a separate file
aws s3api list-objects-v2 --bucket "${bucket}" \
| jq -c '.Contents | _nwise(1000) | map({ Key: .Key }) | { Objects: ., Quiet: true }' \
| awk '{ f = "deletes/" NR ".json"; print $0 > f; close(f) }'
$ kubectl --namespace mr-test exec -ti hostname-test bash
root@hostname-test:/# cat /etc/hosts
# Kubernetes-managed hosts file.
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
10.128.1.142 hostname-test
@matthiasr
matthiasr / LICENSE.md
Last active February 15, 2023 00:16
Exporting chef-client metrics to Prometheus

The MIT License (MIT)

Copyright © 2017 SoundCloud Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE

@matthiasr
matthiasr / out.txt
Created October 13, 2016 17:32
Kubernetes file permissions in tarball have changed
% for v in 1.3.5 1.3.6 1.3.7 1.3.8 1.4.0 1.4.1; do echo ========= $v ==========; curl -sSfL "https://github.com/kubernetes/kubernetes/releases/download/v${v}/kubernetes.tar.gz" | tar -O -zxf - kubernetes/server/kubernetes-server-linux-amd64.tar.gz | tar -tvzf -; done
========= 1.3.5 ==========
drwxr-xr-x 0 root root 0 Aug 11 20:35 kubernetes/
-rw-r--r-- 0 root root 3533536 Aug 11 20:35 kubernetes/LICENSES
-rw-r--r-- 0 root root 41933134 Aug 11 20:35 kubernetes/kubernetes-src.tar.gz
drwxr-xr-x 0 root root 0 Aug 11 20:35 kubernetes/server/
drwxr-xr-x 0 root root 0 Aug 11 20:35 kubernetes/server/bin/
-rw-r--r-- 0 root root 33 Aug 11 20:35 kubernetes/server/bin/federation-apiserver.docker_tag
-rw-r--r-- 0 root root 33 Aug 11 20:35 kubernetes/server/bin/federation-controller-manager.docker_tag
-rw-r--r-- 0 root root 104576512 Aug 11 20:35 kubernetes/server/bin/federation-apiserver.tar
#!/bin/sh
set -eufx
tmpdir=$(mktemp -d ./prometheus-1610.XXXX)
cd "${tmpdir}"
cleanup() {
set +e