You can become a serverless blackbelt. Enrol in my course Learn you some Lambda best practice for great good! and learn best practices for performance, cost, security, resilience, observability and scalability. By the end of this course, you should be able to make informed decisions on which AWS service to use with Lambda and how to build highly scalable, resilient and cost-efficient serverless applications.
If you enjoy reading these exercises then please buy Crista’s book to support her work.
Style 4 – Cookbook
Although Crista has called this the Cookbook style, you’d probably know it as Procedural Programming.
- No long jumps
- Complexity of control flow tamed by dividing the large problem into smaller units using procedural abstraction
- Procedures may share state in the form of global variables
- The large problem is solved by applying the procedures, one after the other, that change, or add to, the shared state
As stated in the constraints, we need to solve the term frequencies problem by building up a sequence of steps (i.e . procedures) that modifies some shared states along the way.
So first, we’ll define the shared states we’ll use to:
- hold the raw data that are read from the input file
- hold the words that will be considered for term frequencies
- the associated frequency for each word
I followed the basic structure that Crista laid out in her solution:
- read_file : reads entire content of the file into the global variable data;
- filter_chars_and_normalize : replaces all non-alphanumeric characters in data with white space’’;
- scan : scans the data for words, and adds them to the global variable words;
- remove_stop_words : load the list of stop words; appends the list with single-letter words; removes all stop words from the global variable words;
- frequencies : creates a list of pairs associating words with frequencies;
- sort : sorts the contents of the global variable wordFreqs by decreasing order of frequency
As you can see, readFile is really straight forward. I chosen to store the content of the file as a char rather than a string because it simplifies filterCharsAndNormalize:
- it allows me to use Char.IsLetterOrDigit
- it gives me mutability (I can replace the individual characters in place)
The downside of storing data as a char is that I will then need to construct a string from it when it comes to splitting it up by white space.
To remove the stop words, I loaded the stop words into a Set because it provides a more efficient lookup.
For the frequencies procedure, I would have liked to simply return the term frequencies as output, but as the constraint says that
The large problem is solved by applying the procedures, one after the other, that change, or add to, the shared state
so unfortunately we’ll have to update the wordFreqs global variable instead…
And finally, sort is straight forward thanks to Array.sortInPlaceBy:
To tie everything together and get the output, we simply execute the procedures one after another and then print out the first 25 elements of wordFreqs at the end.
Since each procedure is modifying shared states, this introduces temporal dependency between the procedures. As a result:
- they’re not idempotent – running a procedure twice will cause different/invalid results
- they’re not isolated in mindscape – you can’t think about one procedure without also thinking about what the previous procedure has done to shared state and what the next procedure expects from the shared state
Also, since the expectation and constraints of the procedures are only captured implicitly in its execution logic, you cannot leverage the type system to help communicate and enforce them.
That said, many systems we build nowadays can be described as procedural in essence – most web services for instance – and rely on sharing and changing global states that are proxied through databases or cache.
You can find all the source code for this exercise here.
I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.
Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.
Check out my new podcast Real-World Serverless where I talk with engineers who are building amazing things with serverless technologies and discuss the real-world use cases and challenges they face. If you’re interested in what people are actually doing with serverless and what it’s really like to be working with serverless day-to-day, then this is the podcast for you.
Check out my new course, Learn you some Lambda best practice for great good! In this course, you will learn best practices for working with AWS Lambda in terms of performance, cost, security, scalability, resilience and observability. We will also cover latest features from re:Invent 2019 such as Provisioned Concurrency and Lambda Destinations. Enrol now and start learning!
Check out my video course, Complete Guide to AWS Step Functions. In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. There is something for everyone from beginners to more advanced users looking for design patterns and best practices. Enrol now and start learning!
Are you working with Serverless and looking for expert training to level-up your skills? Or are you looking for a solid foundation to start from? Look no further, register for my Production-Ready Serverless workshop to learn how to build production-grade Serverless applications!
Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.
- Lambda optimization tip – enable HTTP keep-alive
- You are wrong about serverless and vendor lock-in
- You are thinking about serverless costs all wrong
- Just how expensive is the full AWS SDK?
- Many faced threats to Serverless security
- We can do better than percentile latencies
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong
- Top 10 Serverless framework best practices