Skip to content

Instantly share code, notes, and snippets.

I already have a ring of servers (message brokers, to be precise) that uses Cassandra to persist data needed to recover from a crash. We played with some configurations and realized that we get much better performance if we run Cassandra on the same box as the app. The row key is well distributed, so 1/n chance that the IO operation can be performed locally. The traffic is also well distributed (it's a safe assumption) so the question is, why rely on the row key to distribute the rows around the ring? Why can't I just assume the local shard is the master that I want to write to? This would give me a 100% chance that the IO operation can be performed locally.

I assume this is a bit of a corner case, so there's probably nothing pre-boxed for my situation. But is there something that would function as a good starting point?

@tkellogg
tkellogg / intersecting-unions.scala
Created November 6, 2013 16:09
Is this a bad pattern? I've begun using it a lot and I haven't seen any negative effects, but I've never heard anyone advocate for it either. Wondering if my intuition is mislead. As far as I know, these patterns don't exist in F#, ML or Haskell, so I'm wondering if they're a brilliant addition of Scala or an antipattern.
sealed trait ParseTree
sealed trait ValueNode extends ParseTree
sealed trait PrimitiveValueNode extends ValueNode
case class OrNode(left: ParseTree, right: ParseTree) extends ParseTree
case class AndNode(left: ParseTree, right: ParseTree) extends ParseTree
case class BoolNode(value: Boolean) extends PrimitiveValueNode
case class IntNode(value: Int) extends PrimitiveValueNode
case class StringNode(value: String) extends PrimitiveValueNode
// Use refined trait ValueNode to better describe what value can *actually* hold
case class Fail(text: Option[String])
object Main extends App {
import org.json4s._
import org.json4s.native.Serialization
import org.json4s.native.Serialization.{read, write}
implicit val formats = org.json4s.DefaultFormats
read[Fail]("""{"notext":"foo"}""").text match {
case Some(txt) => // ClassCastException here when using json4s v3.1.0
@tkellogg
tkellogg / implicit.scala
Created July 3, 2013 16:54
Demonstrating use of implicit parameters for dependency injection
object People {
implicit val columnFamily: ColumnFamily = getColumnFamily
def getPersonByName(name: String)(implicit cf: ColumnFamily) = ...
}
class ProdUsage {
// uses ColumnFamily from object People
def foo = People.getPersonByName("Jordan")
}
@tkellogg
tkellogg / git-recommit.sh
Created June 24, 2013 15:43
A git alias that's like commit --amend but adds changes to the last commit without changing the commit message
#!/bin/sh
msg=$(git log -n 1 --format="%B")
git reset HEAD^
git add -A
git commit -m "$msg"
@tkellogg
tkellogg / notTailRecursive.scala
Created June 23, 2013 19:43
How extracting a method can make a function go from tail recursive to just regular recursive. There's a few more ways too.
def bar(value: Int) = foo(Some(value / 3))
def foo(value: Option[Int]): Option[Int] = value match {
case Some(v) if (v % 2 == 0) => bar(v)
case Some(v) => Some(v)
case None => None
}
@tkellogg
tkellogg / index.html
Last active December 18, 2015 07:19
Why masterless infrastructures are necessary
<!DOCTYPE html>
<meta charset="utf-8">
<link rel="stylesheet" href="http://cmx.io/v/0.1/cmx.css">
<script src="http://cmx.io/v/0.1/cmx.js" charset="utf-8"></script>
<style>.cmx-user-scene4 .cmx-text-border .cmx-path {stroke: orange}</style>
<body>
<div style="max-width:900px; -webkit-transform:rotate(0deg)">
<scene id="scene1">
<label t="translate(0,346)">
@tkellogg
tkellogg / gist:5619461
Created May 21, 2013 12:33
How I installed mono 3 and monodevelop 4 on Ubuntu 13.04 with NuGet and F# support
# Add this line to your software sources
deb http://debian.meebey.net/experimental/mono /
sudo apt-get update
# of course, apt-get remove mono-complete first...
sudo apt-get install mono-complete
# I installed monodevelop from apt just to get all the prereqs
sudo apt-get install monodevelop
@tkellogg
tkellogg / Closure.cs
Created June 25, 2012 04:19
"The anatomy of closures" blog post
class Anon1
{
private int second;
// constructor
public Anon1(int second)
{
this.second = second;
}
@tkellogg
tkellogg / Word-Definition.cs
Created March 23, 2012 03:38
Why Object IDs & Primary Keys Are Implementation Details
public class Word {
public int Id { get; set; }
public string Name { get; set; }
public IList<Definition> Definitions { get; private set; }
}
public class Definition {
public int Id { get; set; }
public int WordId { get; set; }
public string Text { get; set; }