Sunday, 1 March 2015

Why should you expect failures in your datacenter?

Not only should you expect failures but you should expect FREQUENT failures.

If you can estimate that every server in your data centre will fail once every ten years then that sounds pretty good right?

Failure rate = Once / 10 years = once / 120 months

But... if you have 120 servers then that will mean you should expect a failure every month!

According to this article, in 2010 Facebook was running at least 60,000 servers across its data centres. 

If these each of these 60,000 servers is expected to fail once every 10 years, then at that time Facebook would have expected a server failure about every hour and a half (120 months / 60000 ~= 1.46  hr)

Eeeek. 





Scala Puzzler 1: Overrides in Constructors

Question:


What is printed when the following code is executed?

object Example extends App {
val alex = new Person {
override val stringName = "Alexandra"
}
alex.whoAreYou
}
class Person {
val stringName = "I dont have a name"
val name = new Name(stringName)
def whoAreYou = { println(name.value) }
}
case class Name(val value: String)

Answer:


This code will print out 'null'.

This is because  the Person constructor is called before the override, meaning that val name is instantiated before the override is called. The default name value is never set because the compiler is clever enough to know we don't wan stringName to be set to "I dont have a name" so it skips that assignment.

object Example extends App {
val alex = new Person {
override val stringName = "Alexandra" // val name = new Name(stringName) has already been called
}
alex.whoAreYou
}
class Person {
val stringName = "I dont have a name" // never executed
val name = new Name(stringName) // executed but stringName is currently null!
def whoAreYou = { println(name.value)}
}
case class Name(val value: String)

Interestingly... 


If the stringName was declared with the 'lazy keyword' the code would work properly and print 'Alexandra'. This is because a lazy variable is declared as soon as it is needed, which in this case would be when the name class is instantiated.

object Example extends App {
val alex = new Person {
lazy override val stringName = "Alexandra"
}
alex.whoAreYou
}
class Person {
lazy val stringName = "I dont have a name"
val name = new Name(stringName)
def whoAreYou = { println(name.value)}
}
case class Name(val value: String)


Scala with Cats: Answers to revision questions

I'm studying the 'Scala with Cats' book. I want the information to stick so I am applying a technique from 'Ultralearning...