Monday, April 25, 2022

Leetcode discuss: 36. Valid Sudoku

 April 25, 2022

Here is the link. 

C# | Encode 1-9 digits to each row and each column and 9 3 x 3 matrix

April 19, 2022
Introduction
It takes me 10 minutes at least to write a working solution. First a hashset is created to include all digits from 1 to 9 to map for each row and each column and each of 9 3 x 3 matrix; next step is to go over each number in given matrix, and then remove it from the hashset.

The following C# code passes online judge.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _36_valid_sudoku___encode_as_strings
{
    class Program
    {
        static void Main(string[] args)
        {
            //[[".","8","7","6","5","4","3","2","1"],
            //["2",".",".",".",".",".",".",".","."],
            //["3",".",".",".",".",".",".",".","."],
            //["4",".",".",".",".",".",".",".","."],
            //["5",".",".",".",".",".",".",".","."],
            //["6",".",".",".",".",".",".",".","."],
            //["7",".",".",".",".",".",".",".","."],
            //["8",".",".",".",".",".",".",".","."],
            //["9",".",".",".",".",".",".",".","."]]
            var board = new char[9][];
            board[0] = new char[] { '.', '8', '7', '6', '5', '4', '3', '2', '1' };
            board[1] = new char[] { '2', '.', '.', '.', '.', '.', '.', '.', '.' };
            board[2] = new char[] { '3', '.', '.', '.', '.', '.', '.', '.', '.' };
            board[3] = new char[] { '4', '.', '.', '.', '.', '.', '.', '.', '.' };
            board[4] = new char[] { '5', '.', '.', '.', '.', '.', '.', '.', '.' };
            board[5] = new char[] { '6', '.', '.', '.', '.', '.', '.', '.', '.' };
            board[6] = new char[] { '7', '.', '.', '.', '.', '.', '.', '.', '.' };
            board[7] = new char[] { '8', '.', '.', '.', '.', '.', '.', '.', '.' };
            board[8] = new char[] { '9', '.', '.', '.', '.', '.', '.', '.', '.' };

            var result = IsValidSudoku(board);
        }

        /// <summary>
        /// study code:
        /// https://leetcode.com/problems/valid-sudoku/discuss/15472/Short%2BSimple-Java-using-Strings
        /// Collect the set of things we see, encoded as strings. For example:
        /// '4' in row 7 is encoded as "(4)7".
        /// '4' in column 7 is encoded as "7(4)".
        /// '4' in the top-right block is encoded as "0(4)2".
        /// </summary>
        /// <param name="board"></param>
        /// <returns></returns>
        public static bool IsValidSudoku(char[][] board)
        {            
            var set = new HashSet<string>();
            var digits = "123456789".ToCharArray();            

            /*
             * Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
                Each row must contain the digits 1-9 without repetition.
                Each column must contain the digits 1-9 without repetition.
                Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
             */
            foreach(var digit in digits)
            {
                var digitStr = "(" + digit.ToString() + ")";
                // add all rows - ()0-9
                for (int index = 0; index < 9; ++index)
                {
                    set.Add(digitStr + index.ToString());  // every row at most 9 distinct digits
                    set.Add(index.ToString() + digitStr);  // every column 

                    set.Add(index % 3 + digitStr + index / 3);  // 9 3 x 3 matrixes 
                }
            }                          
            
            for (int row = 0; row < 9; ++row)
            {
                for (int col = 0; col < 9; ++col)
                {
                    if (board[row][col] == '.')
                    {
                        continue; 
                    }
                    
                    var b = "(" + board[row][col] + ")";

                    if (set.Contains(b + row) && set.Contains(col + b) && set.Contains(row / 3 + b + col / 3))
                    {
                        set.Remove(b + row);
                        set.Remove(col + b);
                        set.Remove(row / 3 + b + col / 3);
                    }
                    else 
                    {
                        return false; 
                    }
                    
                }
            }

            return true;
        }
    }
}

Sunday, April 24, 2022

Leetcode discuss: 295. Find Median from Data Stream

 April 24, 2022

Here is the link. 

C# | Minimum heap and maximum heap | SortedSet<Tuple<int, int>>

April 22, 2022
Maximum heap and minimum heap design
It is important to come out best time complexity using two heaps, one is maximum heap, one is minimum heap. So it is O(1) time complexity to find median value. That is most efficient way to find median value in a stream.

Prepare a check list

  1. Using C# Tuple<int, int> to build a minimum and maximum heap. The first one in Tuple is value, and the second one is the counter variable name value, which is helpful to keep duplicate value and make it unique since counter variable has identity value to increment one always.
  2. Design two heaps, one is maximum heap, one is minimum heap; The small half numbers are stored in maximum heap, using C# SortedSet<Tuple<int, int>>, called smallHalf;
  3. If the total count of numbers are even, then smallHalf and bigHalf has same number of integers; otherwise I choose to keep extra one into smallHalf always. It is better to change smallHalf to smallHalfPlusOne to remind myself, so it is easy for me to code, for example, add extra number to smallHalfPlusOne, get medium number from smallHalfPlusOne if total count is odd. In other words, easy to figure out, easy to avoid mistakes in the code.
  4. Variable naming: smallHalfPlusOne, bigHalf variables names are more meaningful compared to setLow, setHigh. Design minimum heap and maximum heap using C# SortedSet<Tuple<int, int>>.
  5. First step is to check which heap to add the incoming integer, next step is to move one number to another heap to maintain two heap's count's difference is at most one, and also if it is not equal, smallHalfPlusOne should have an extra integer.

Warmup for Meta onsite in May 2022
I chose to work on this algorithm to prepare Meta onsite in May 2022. I try to figure out what to learn from this practice. I wrote down my detail - 30 days to meta onsite here Day 18 - I chose to go over 15 algorithms from Stefan Pochmann.

The following C# code pass online judge.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _295_Find_median_of_stream
{
    class Program
    {
        static void Main(string[] args)
        {
        }

        public class MedianFinder {

   private int counter = 0;

        private SortedSet<Tuple<int, int>> smallHalfPlusOne = new SortedSet<Tuple<int, int>>();
        private SortedSet<Tuple<int, int>> bigHalf = new SortedSet<Tuple<int, int>>();        

        public void AddNum(int num)
        {
             var newNum = new Tuple<int, int>( num, counter++ );            
            
            // Deal with empty minimum heap or maximum heap case 
            if ( smallHalfPlusOne.Count == 0 || newNum.Item1 < smallHalfPlusOne.Max.Item1)
            {
                smallHalfPlusOne.Add(newNum);
            }
            else
            {
                bigHalf.Add(newNum);
            }

            // There is more than one numbers in smallHalfPlusOne
            while (smallHalfPlusOne.Count > bigHalf.Count + 1)
            {
                bigHalf.Add(smallHalfPlusOne.Max);
                smallHalfPlusOne.Remove(smallHalfPlusOne.Max);
            }
            
            // bigHalf has more numbers than smallHalfPlus 
            while(bigHalf.Count > smallHalfPlusOne.Count)
            {
                // move the minimum number from setHigh to setLow. 
                smallHalfPlusOne.Add(bigHalf.Min);
                bigHalf.Remove(bigHalf.Min);                
            }                     
        }

        /// <summary>
        /// if minimum heap and maximum heap have same size, then medium is to get the average of those two values 
        /// </summary>
        /// <returns></returns>
        public double FindMedian()
        {
            if (smallHalfPlusOne.Count == 0)
            {
                return 0;
            }

            if (smallHalfPlusOne.Count == bigHalf.Count)
            {
                return (smallHalfPlusOne.Max.Item1 + bigHalf.Min.Item1) / 2d;
            }
            else
            {
                return smallHalfPlusOne.Max.Item1;
            }
        }
}


Udemy -> Pragmatic System design | Section 15: Design a news feed (aka Twitter)

 April 24, 2022

Introduction

I have to take down some notes, and then I will start to read more carefully and also follow the presentation better. 

How to scale 


  • Read replicas?
  • Sharding?
  • Cache?
Read replicas
300K reads per second
20K reads per replica? = 15 replicas
50K reads per replica? = 6 replicas
Pros: Simplicity
Cons: Cost and space requirements

Sharding

300K reads per second
10K reads per shard? - 30 shards
25K read per shard? = 12 shards
Pros: less costly than replicas, since each shard is smaller 
Cons: architectural complexity

Sharding - space requirements
30TB of data
30 shards => 1TB for each
12 shards -> 3TB for each

Caching


Cache aside?
Read through?

Caching and storage
86GB per day
3 instances for read purposes
Every instance is 32GB?





Friday, April 22, 2022

April 22, 2022 | Market plunge

 The S&P 500 plunged 2.8%, marking its second-worst day of the year, while the Dow Jones Industrial Average wiped out 980 points in its worst day since October 2020. The tech-heavy Nasdaq Composite tumbled 2.6%. Meanwhile, the 10-year U.S. Treasury yield remained at 2.9%, the highest level since December 2018.

"Markets are very uneasy about the growing likelihood of a policy error by the Federal Reserve," Harris Financial Group Managing Partner Jamie Cox said in a note. "When a Fed official suggests a 50 basis points hike, markets immediately start trying to price in 75 basis point hikes — it's madness really."

The losses follow remarks from Fed Chair Jerome Powell at a panel hosted by the International Monetary Fund Thursday signaling a 50-basis point rate increase was “on the table” for May, when the U.S. central bank holds its next policy-setting meeting. The Fed chair also reiterated that policymakers were committed to “front-end loading” inflation-fighting efforts.

"Today's market action reflects the power of Jerome Powell's comments yesterday, that the Fed is determined to slay climbing inflation and virtually acknowledging that the market can expect a 50 basis point hike in May," LPL Financial chief equity strategist Quincy Krosby said in comments Friday.

Yahoo -> Finance -> Portfolio

April 22, 2022




I should think about carefully, try to cut loss on PSFE early, FB stock, and sell intc for $1600 dollars gain. Do not wait for last minute, and then take actions. 

It is hard to get rid of market risk. I have to be patient and then wait for the dip instead. I had $4400 US dollars gain a few days ago, and I thought that I should catch IBM 7% gain on earnings date. 




Udemy course: Pragmatic system design | Alexey Soshin | Section 11: Design a web crawler (aka Google Crawler)

 Udemy course: Pragmatic system design | Alexey Soshin | Section 11: Design a web crawler (aka Google Crawler)

April 22, 2022

I think that I made a good choice to purchase the course. I love learning from a mock interview section:

Section 11: Design a web crawler (aka Google Crawler). 

I was asked to work on a system design to design a web crawler a few years ago. I always search good ideas to solve this system design question. 

Current design | Politeness | Detect change or not change 

Udemy course: Pragmatic system design | Alexey Soshin | Section 11: Design a web crawler (aka Google Crawler)

April 22, 2022

I think that I made a good choice to purchase the course. I love learning from a mock interview section:

Section 11: Design a web crawler (aka Google Crawler). 

I was asked to work on a system design to design a web crawler a few years ago. I always search good ideas to solve this system design question. 

Current design | Uniqueness checker 



Bloom filter | uniqueness design talk 

Cons:
  • Memory requirements
    • 1.58 sites, if each site has 10 pages on average
      • 50GB of RAM
  • False positives



Key-value store? | uniqueness design talk 

  • Average URL = 50 bytes
  • 15B URLs
  • = 750,000,000,000 bytes
  • = 732,000,000 KB
  • = 715,000 MB
  • = 700 GB

Plain old DB? 




Summary | Three choices: Bloom filters, Redis, RDBMS 

High throughput, weak consistency - Bloom filters

Medium throughput, medium consistency - Redis

Low throughput, high consistency - RDBMS 

How Twitter uses Redis to scale - 10TB RAM, 39MM QPS, 10,000+ instances | My 20 minutes study


Here is the article.

How Twitter uses Redis to scale - 10TB RAM, 39MM QPS, 10,000+ instances

MONDAY, SEPTEMBER 8, 2014 AT 9:05AM

Yao Yue has worked on Twitter’s Cache team since 2010. She recently gave a really great talk: Scaling Redis at Twitter. It’s about Redis of course, but it's not just about Redis.

Yao has worked at Twitter for a few years. She's seen some things. She’s watched the growth of the cache service at Twitter explode from it being used by just one project to nearly a hundred projects using it. That's many thousands of machines, many clusters, and many terabytes of RAM.

It's clear from her talk that's she's coming from a place of real personal experience and that shines through in the practical way she explores issues. It's a talk well worth watching.

As you might expect, Twitter has a lot of cache.

Timeline Service for one datacenter using Hybrid List:
  • ~40TB allocated heap
  • ~30MM qps
  • > 6,000 instances
Use of BTree in one datacenter:
  • ~65TB allocated heap
  • ~9MM qps
  • >4,000 instances

You'll learn more about BTree and Hybrid List later in the post.

A couple of points stood out:

  • Redis is a brilliant idea because it takes underutilized resources on servers and turns them into valuable service.
  • Twitter specialized in Redis with two new data types that fit their use cases perfectly. So, they got the performance they needed, but it locked them into an older code based and made it hard to merge with new features. I have to wonder, why use Redis for this sort of thing? Just create a timeline service using your own data structures. Does Redis really add anything to the party?
  • Summarize large chunks of log data on the node, using your local CPU power, before saturating the network.
  • If you want something that’s high performance separate the fast path, which is the data path, away from the slow path, which is the command-and-control path. 
  • Twitter is moving towards a container environment with Mesos as the job scheduler. This is still a new approach so it's interesting to hear about how it works. One issue is the Mesos wastage problem that stems from requirement to specify hard resource usage limits in a complicated runtime world.
  • A central cluster manager is really important to keep a cluster in a state that’s easy to understand.
  • The JVM is slow and C is fast. Their cache proxy layer is moving back to C/C++.

With that in mind, let's learn more about how Redis is used on Twitter:

Why Redis?

  • Redis drives Timeline, Twitter’s most important service. Timeline is an index of tweets indexed by an id. Chaining tweets together in a list produces the Home Timeline. The User Timeline, which consists of tweets the user has tweeted, is just another list.

  • Why consider Redis instead of Memcache? The Network Bandwidth Problem and The Long Common Prefix Problem.

  • The Network Bandwidth Problem.

    • Memcache didn’t work as well as Redis for the timeline. The problem was dealing with fanout.

    • Twitter read and write happen incrementally and they are fairly small, but the timelines themselves are fairly large.

    • When a tweet is generated, it needs to be written to all relevant timelines. The tweet is a small piece of data that is attached to some data structure. On reading it’s desirable to load a small batch of tweets. On a scroll down another batch is loaded.

    • The home timeline can be largish, what is reasonable for a viewer to read in one set. Maybe 3000 entries, for example. Which means for performance reasons accessing the databases should be avoided.

    • A read-modify-write cycle for incremental writes, and small reads, on large objects (the timeline), is too expensive and creates a network bottleneck.

    • On a gigalink at 100K+ reads and writes per second, if the average object size is more than 1K, the network becomes the bottleneck.

  • The Long Common Prefix Problem (really two problems)

    • A flexible schema approach is used for data formats. An object has certain attributes that may or may not exist. A separate key can be created for each individual attribute. This requires sending out a separate request for each individual attribute and not all attributes may be in the cache.

    • Metrics that are observed over time have the same name with each sample having a different time stamp. If storing each metric individually the long common prefix is being stored many times. 

    • To be more space efficient in both scenarios, for metrics and a flexible schema, it is desirable to have a hierarchical key space.

  • A dedicated caching cluster underutilizes CPUs. For simple cases, in-memory key-value stores are CPU light. 1% of CPU time on a box can handle more than 1K requests per second for small key values. Though for different data structures the result can be different.

  • Redis is a brilliant idea. It sees what the server can do, but is not doing. For simple key-value stores, there’s a lot of CPU headroom on the server side for a service like Redis.

  • Redis was first used within Twitter in 2010 for the Timeline service. It is also used in the Ads service.

  • The on-disk features of Redis are not used. Partly this is because inside Twitter the Cache and Storage services are in different teams so they use whatever mechanisms they think best. Partly this may be because the Storage team thinks another service fits their goals better than Redis.

  • Twitter forked Redis 2.4 and added some features to it, so they are stuck at 2.4 (2.8.14 is the latest stable version). Changes were: two data structure features within Redis; in-house cluster management features; in-house logging and data insight.

  • Hotkeys are a problem so they are a building a tiered caching solution with clients-ide caching that will automatically cache hotkeys.

Hybrid List

  • Added Hybrid List to Redis for more predictable memory performance.

  • Timeline is a list of Tweet IDs, so it’s a list of integers. Each ID is small.

  • Redis supports two list types: ziplist and linklist. Ziplist is space efficient. Linked list is flexible, but as a doubly linked list has the overhead of two pointers per key, which given the size of the ID is very high overhead.

  • To use memory efficiently ziplists are used exclusively.

  • A Redis ziplist threshold is set to the max size of a Timeline. Never store a bigger Timeline than can be stored in a ziplist. This means a product decision, how many tweets can be in a Timeline, are linked to a low level component (Redis). Generally not desirable.

  • Adding to and deleting from a ziplist is inefficient, especially with a very large list. Deleting from a ziplist uses memmove to move data around, to make sure the list is still contiguous. Adding to a ziplist requires a memory realloc call to make enough space for the new entry.

  • Potential high latency for write operations due to Timeline size. Timelines vary a lot in size. Most users don’t tweet very much, so their User Timeline is small. Home Timelines, especially those involving celebrities can be huge. When updating a large timeline and the cache runs out of heap, which is often the case when using a cache, a very large number of very small timelines will be evicted before there’s enough contiguous RAM to handle one big ziplist. As all this cache management takes time, a write operation can have a high latency.

  • Since writes are fanned out to a lot of timelines there’s a higher chance to be caught in a write latency trap as memory is used for expanding the timelines.

  • It’s hard to create a SLA for write operations given the high variability of write latencies.

  • Hybrid List is a linked list of ziplists. A threshold is set of how big each ziplist can be in bytes. In bytes because to memory efficient it helps to allocate and deallocate blocks of the same size. When a list goes over it is spilled into the next ziplist. A ziplist is not recycled until the list is empty, which means it is possible, through deletion, to have each ziplist have only one entry. In practice, tweets aren’t deleted all that often.

  • Before Hybrid List a workaround was to expire larger timelines more quickly, which freed up memory for other timelines, but was expensive when a user went to view their timeline.

BTree

  • Added BTree to Redis to support range queries on hierarchical keys to return a list of results.

  • In Redis the way to deal with secondary keys or fields is a hash map. To have sorted data in order to perform a range query a sorted set is used. Sorted set orders by a score which is a double, so an arbitrary secondary key or an arbitrary name can’t be used for the sorting. Since hash map uses a linear search it’s not great if there are a lot of secondary keys or fields.

  • BTree is the attempt fix the shortcomings of hash map and sorted set. It’s better to just have one data structure that does what you want. It’s easier to understand and reason about.

  • Borrowed the BSD implementation of BTree and added it to Redis to create a BTree. Supports key lookup as well as range query. Has good lookup performance. The code is relatively simple. The downside is BTree is not memory efficient. It has a lot of meta data overhead due to the pointers.

Cluster Management

  • A cluster is using more than one instance of Redis for a single purpose. If a data set is larger than a single Redis instance can handle or throughput is higher than what a single instance can handle, the key space will need to be partitioned so the data can be stored in more than one shard, across a set of instances. Routing is taking a key and figuring out which shard the data for the key is on.

  • Thinks cluster management is the number one reason Redis adoption hasn’t exploded. When a cluster is available there’s no reason not to migrate all cache use cased to Redis.

  • Tricky to get Redis cluster right. People use Redis because as a data structure server the idea is to perform frequent updates. But a lot of Redis operations are not idempotent. If there’s a network glitch a retry is required and the data can be corrupted.

  • Redis cluster favors having a centralized manager dictating the global view. With memcache a lot clusters use a client side approach based on consistent hashing. If there’s inconsistent data, so be it. To provide really good services, a cluster needs features like detecting which shard is down and then replaying operations to get back in sync. After a long enough period spent down cache state should be cleaned up. Corrupted data in Redis is hard to detect. When there’s a list and it’s missing a chunk, it’s hard to tell.

  • Twitter has multiple attempts at building a Redis cluster. Twemproxy which is not used by Twitter internally, it was built for Twemcache and Redis support was added. Two more solutions were based on proxy style routing. One was associated with the Timeline service and not meant to be general. The second was a generalization of the Timeline solution that provided cluster management, replication, and shard repairing.

  • Three options in a cluster: servers talk to each other to reach agreement of what a cluster looks like; use a proxy; or do client side cluster management where the clients form a quorum.

  • Didn’t go with a server approach because the philosophy is to keep servers simple, dumb and fast.

  • Didn’t go with the client because changes are hard to propagate. Approximately 100 projects in Twitter use a cache cluster. Changing anything in the client would have to be pushed to 100 clients it could take years for changes to propagate. Quick iteration means it’s almost impossible to put code in the client.

  • Went with a proxy style routing approach and partitioning for two reasons. A cache service is a high performance service. If you want something that’s high performance separate the fast path, which is the data path, away from the slow path, which is the command and control path. If cluster management is merged into the server it complicates the code for Redis, which is a stateful service, any time you want to fix a bug or provide an upgrade to the cluster management code, the stateful Redis service must be restarted too, which will potentially throw away a bunch of data. A rolling restart of a cluster is painful.

  • There was a concern using the proxy approach that another network hop is inserted between the client and the server. Profiling showed the extra hop is a myth. At least in their ecosystem. Latency to through the Redis server was less than .5 milliseconds. At Twitter most of the backend services are Java based and use Finagle to talk to each other. When going through the Finagle path the latency was close to 10 milliseconds. So the extra hop isn’t the problem. Inside the JVM is the problem. Outside the JVM you can do pretty much whatever you want, unless of course you go through another JVM.

  • Failure of a proxy doesn’t matter much. On the data path introducing a proxy layer isn’t so bad. The client doesn’t care which proxy they talk to. If a proxy fails after a timeout the client goes to another proxy. No sharding is happening at the proxy level, they are all stateless. To scale throughput simply add more proxies. The tradeoff is additional cost. The proxy layer is allocated resources just to do the forwarding. Cluster management, sharding, and doing the view of the cluster happens outside the proxies. The proxies don’t have to agree with each other.

  • Twitter has instances that have 100K open connections and it works fine. There’s just overhead to pay. There’s no reason to close connections. Just keep them open, it improves latency.

  • Cache clusters are used as a look-aside cache. The caches themselves are not responsible for data replenishment. The client is responsible for fetching a missing key from storage then caching it. If a node goes down the shard is moved to another node. The failed machine is flushed when it comes back so no data is left around. All this is done by the cluster leader. A central viewpoint is really important to keep a cluster in a state that’s easy to understand.

  • Did an experiment with a proxy written in C++. The C++ proxy saw a significant performance increase (no number given). The proxy tier is being moved back to C and C++.

Data Insight

  • When there’s a call saying the cache system is misbehaving most of the time the cache is fine. Usually the clients are configured wrong. Or they are abusing the cache system by requesting way too many keys. Or requesting the same key over and over again and saturating the server or the link.

  • When you tell someone they are abusing your system they want proof. Which key? Which shard is bad? What kind of traffic leads to this behaviour? Proof requires metrics and analysis that can be shown to customers.

  • An SOA architecture doesn’t give you problem isolation or make debugging easier automatically. You have to have good visibility into every component that makes up the system.

  • Decided to build Insight into caching. The cache is written in C and is fast, so it can provide data those other components can’t. Other components can't handle the load of providing data for every request.

  • Logging every single command is possible. The cache can log everything at 100K qps. Only meta data is logged, values are not logged (Good joke about the NSA).

  • Avoid locking and blocking. Especially don’t block on disk writes.

  • At 100 qps and a 100 bytes per log message, each box will log 10MB of data per second. That’s a lot of data to move off the box. 10% of network bandwidth would be used just in case something went bad. Economically not feasible.

  • Precompute logs on the box to reduce costs. Assumption is that it is already knows what will be computed. A process reads the logs and generates a summary and periodically sends this view of the box. The view is tiny compared to the original data.

  • View data is aggregated by Storm, stored, and there’s a visualization system sitting on top. You can get data like here are your top 20 keys; here’s your traffic by second and there’s a peak which means the traffic pattern is spiky; here’s are the number of unique keys, which helps with capacity planning. A lot can be done when every single log is captured.

  • Insight is very valuable for operations. If there are packet drops often that can be linked to either a hot key or spiky traffic behaviour.

Wish List For Redis

  • Explicit memory management.

  • Deployable (Lua) Scripts. Talked about near the start.

  • Multi-threading. Would make cluster management easier. Twitter has a lot of “tall boxes,” where a host has 100+ GB of memory and a lot of CPUs. To use the full capabilities of a server a lot of Redis instances need to be started on a physical machine. With multi-threading fewer instances would need to be started which is much easier to manage.

Lessons Learned

  • Scale demands predictability. The larger the cluster, the more customers, the more predictable and deterministic you want your service to be. When there’s one customer and there’s a problem you can dig into a problem and it’s intriguing. When you have 70 customers you can’t keep up.

  • Tail latencies matter. When you do fanouts to a lot of shards, when one is slow your entire query will be slow.

  • Deterministic configuration is operationally important. Twitter is moving towards a container environment. Mesos is used as the job scheduler. The scheduler fulfills the request for the amount of CPU, memory etc. A monitor kills any job that goes over its resource requirement. Redis causes a problem in a container environment. Redis introduces external fragmentation, meaning you use more memory to store the same amount of data. If you don’t want to be killed you have to compensate for that with oversupply. You have to think my memory fragmentation ratio won’t go over 5%, but I’ll allocate 10% more as a buffer space. Maybe even 20%. Or I think I’ll get 5000 connections per host, but just in case let me allocate memory for 10,000 connections. The result is a huge potential for waste. Super low latency services don’t play well with Mesos today, so these jobs are isolated from other jobs.

  • Knowing your resource usage at runtime is really helpful. In a large cluster bad stuff happens. You think you are safe but things happen and behaviour is unexpected. Most services today can’t degrade gracefully. For example, when a limit of 10GB of RAM is reached then requests are rejected until there’s free RAM. This only fails a small percentage of traffic that’s proportional to the resource that they require. That's graceful. Garbage collection problems are not graceful, traffic just gets dropped on the floor, this problem affects a lot of teams in a lot of companies every day.

  • Push computation to the data. If you look at relative network speeds, CPU speeds, and disk speeds, it makes sense to do computation before going to disk and do computation before going to the network. An example is summarizing logs on a node before they are pushed to a centralized monitoring service. LUA in Redis is another way to apply computation close to the data.

  • LUA is not production ready in Redis today. On demand scripting means service providers can’t guarantee their SLA. A loaded script can do anything. What service provider would want to take the risk of blowing their SLA because of someone else's code? A deployment model would be better. It would allow for code review and benchmarking, so resource usage and performance could be properly calculated.

  • Redis as the next high performance stream processing platform. It has pub-sub and scripting. Why not?

Related Articles


Bloom filters

Here is the article.  

Bloom filters are space-efficient probablistic data structures used to test whether an element is a member of a set.

They're surprisingly simple: take an array of m bits, and for up to n different elements, either test or set k bits using positions chosen using hash functions. If all bits are set, the element probably already exists, with a false positive rate of p; if any of the bits are not set, the element certainly does not exist.

Bloom filters find a wide range of uses, including tracking which articles you've read, speeding up Bitcoin clients, detecting malicious web sites, and improving the performance of caches.

This page will help you choose an optimal size for your filter, or explore how the different parameters interact.

Udemy course: Pragmatic system design | Alexey Soshin

April 22, 2022

I think that I made a good choice to purchase the course. I love learning from a mock interview section:

Section 11: Design a web crawler (aka Google Crawler). 

 

This course aims to prepare you for system design interviews, as well as discusses how you could apply this knowledge in your day to day job.

In real world, most of the engineers don't get to design new systems often. Some don't get to design them at all. In many companies architecture is something only a few individuals do regularly. But when it comes to interviewing, we suddenly expect everyone to be master in system design. This course tries to cover some of the basic topics, as well as provide you with my approach to some of the most common system design interview questions.

Second purpose of this course is to provide senior engineers with an alternative view to system design. What I see in the industry is that we don't discuss design among ourselves much. It becomes a sensitive topic, because no real world design is perfect. And that's something I hope to change.

There are two ways I suggest to consume the course. If you have plenty of time, just watch it start to finish. I tried to construct it in a logical order, so you will accumulate more and more confidence as you go.

Alternatively, if you are short on time, or if you aren't preparing actively for interviews at the moment, you can start with the design videos, and if you aren't familiar with one of the topics I discuss, there should be either a video for that or a link to a relevant article.

The goal of system design interview is usually to cover multiple topics. It evaluates the breath of knowledge first, depth of knowledge second. For that reason, I tried to keep the theoretical part on each topic rather brief. That is - it's as deep as I expect as an interviewer from my candidates.

Finally, I will repeat myself and say that no design is perfect. There are always tradeoffs, there are always compromises that you must make. And each design is personal. It depends on what are your areas of expertise. When you watch my videos, please don't treat them as the ultimate way I would design a system, but more as a collection of ideas of how to approach the topic.


If there are more system design interview questions you'd like me to solve or additional topics you'd like me to cover, let me know!

What you’ll learn

  • How to solve most popular FANG interview questions
  • Most important scalability concepts
  • Common communication protocols
  • Caching and Redis
  • Concurrency
  • Database design and PostgreSQL
  • Sharding strategies

Are there any course requirements or prerequisites?

  • Computer hardware basics
  • Basic SQL knowledge for some of the examples

Who this course is for:

  • Software engineers of all levels preparing for System Design interviews
  • Senior engineers that are looking to make the next step in their career
  • Software architects that are looking to broaden their knowledge

Alexey Soshin

Solutions Architect @Depop



Thursday, April 21, 2022

Kindle unlimited: Amazon.ca $9.99/ month | First month | Mastering the System design interview: Insider tips for your system design interview from a former Amazon hiring manager

 April 21, 2022

Introduction

It is the first time I paid annual subscription for Amazon kindle reader. It is a better life style to start to learn to be an Amazon kindle reader. 

Mastering the System design interview: Insider tips for your system design interview from a former Amazon hiring manager


I like to go over the book quickly today, and then I can watch the video course on udemy.com again. I need to learn how to cut budget on clothing and food, and spend more time to read and play sports.


SABR stock: My purchase and gain around $4500 US dollars

 April 21, 2022

Introduction

It takes time for me to learn how to invest on travel software company SABR. I like to document my learning and continue to work on investment during pandemic. 

SABR | Purchase history 




Udemy: Mastering the System Design Interview | Frank Kane

 April 21, 2022

Introduction

I just finished this Udemy course and I was amazed about the quality of content, and I like the mock interview most. 

Mastering the System design interview: Insider tips for your system design interview from a former Amazon hiring manager

I am planning to purchase the book so that I can learn better by reading the book. 


Tuesday, April 19, 2022

WBD, T, DIS, NFLX stock: My 10 minutes study

Here is the article on seekingalpha.com. 

There are more than 3 billion Internet users globally, and 74 or 22 million is not a very significant percentage. Also, if we compare Netflix's (NFLX) subscriber statistics, the company had around 222 million subscribers at the end of 2021. This dynamic illustrates a great deal of market share to capture for WBD, and the market will not get saturated for a long time.

Analysts' estimates are for around $50 billion in revenues for WBD this year. Furthermore, WBD's revenues will likely rise by about 7%-12% YoY in 2023. If the company builds on its growth momentum, it can continue delivering robust growth in future years. The stock's valuation is only about $63 billion today, as the company is trading at only around ten times forward EPS estimates and roughly 1.2 times expected sales. These are remarkably low valuation ratios for a company in WBD's position, implying that shares are grossly undervalued now.

In comparison, Disney (DIS) trades at a forward P/E ratio of around 30 and has a forward sales ratio of roughly 2.7. Netflix also trades at a P/E ratio of about 30 but has a P/S ratio of about 5. Therefore, we see that WBD's nearest competitors trade at ratios 2.5-5 times higher. It's difficult to explain this vast disconnect, but it appears like WBD's stock price can double, and it will still be substantially less expensive than Netflix or Disney. Therefore, I suspect we can see much more upside from WBD as we advance.

Monday, April 18, 2022

April 18, 2022 | Stock futures rise ahead of busy earnings day

 MARKETS

Stock futures rise ahead of busy earnings day

Stock futures rose on Monday evening as traders navigate one of the busiest weeks of corporate earnings season.

Futures tied to the Dow Jones Industrial Average added 98 points, or 0.3%, while those for the S&P 500 climbed 0.4%. Nasdaq 100 futures gained 0.5%

The move in futures comes after a slightly down day for stocks. The Dow and Nasdaq Composite each dipped 0.1%, while the S&P 500 inched lower by 0.02%.

The major indexes have been grinding lower as the first-quarter earnings season heats up. Before the bell on Tuesday, Johnson & Johnson and insurance giant Travelers will report their latest results. Other notable reports include Hasbro, Lockheed Martin, and multiple mid-sized banks such as Citizens Financial.

With inflation and the Federal Reserve’s next steps a key debate in markets, investors are watching for insight into how supply chains and consumer demand are performing for major companies. Expectations for Fed hikes have risen sharply in recent months, though the central bank has said it will be data dependent in deciding how it will hike rates throughout the year.

“Can the Fed raising rates actually solve some of the shortages we have with labor, with semiconductors, with wheat? Probably not. So maybe they’re going to act a little bit less aggressively in the end than some people think,” said Adam Parker of Trivariate Research on “Closing Bell: Overtime.”

The concern about the Fed’s next steps have caused high volatility in the bond market as well, which appears to have weighed on stocks in recent weeks. On Monday, the 10-year Treasury yield hit its highest level in three years. St. Louis Fed president James Bullard told CNBC’s Steve Liesman on Monday that “quite a bit has been priced in” in terms of Fed actions.

Elsewhere on Tuesday, investors will get an updated look at the housing market with housing starts and building permits for March.