西维蜀黍

【Golang】Composition in Golang

Composition vs. Inheritance

Composition and inheritance are two ways to achieve code reuse and build complex systems. Inheritance is a mechanism that allows a class to inherit properties and behavior from a parent class. Composition is a way of building complex objects by combining smaller, simpler objects.

In Go, composition is favored over inheritance. This is because Go does not have classes like traditional object-oriented programming languages. Instead, Go uses structs to define objects and interfaces to define behavior. Composition is achieved by embedding one struct into another.

For example, a solution to compose a simple report in Go can be model as follows:

type celsius float64type temperature struct {
    maximum, minimum celsius
}type location struct {
    latitude, longitude float64
}type report struct {
    sol int
    temperature temperature
    location location
} 
  ...


【Python】Collection - tuple

Tuple

Tuples are used to store multiple items in a single variable.

Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.

A tuple is a collection which is ordered and unchangeable.

Tuples are written with round brackets.

  ...


【Hardware】破解 IPMI 密码

Via metasploit

# brew install metasploit

$ msfconsole

msf > use auxiliary/scanner/ipmi/ipmi_dumphashes
msf auxiliary(ipmi_dumphashes) > show options
    ... show and set options ...
msf auxiliary(ipmi_dumphashes) > set RHOSTS ip-range
msf auxiliary(ipmi_dumphashes) > set RHOSTS 192.168.18.30
msf auxiliary(ipmi_dumphashes) > exploit
  ...


【Architecture】System Design - URL Shortener

Scope

Functional Requirements

Here are the basic use cases:

  1. URL shortening: given a long URL => return a much shorter URL
  2. URL redirecting: given a shorter URL => redirect to the original URL
- Candidate: Can you give an example of how a URL shortener work?
- Interviewer: Assume URL https://www.systeminterview.com/q=chatsystem&c=loggedin&v=v3&l=long is the original URL. Your service creates an alias with shorter length: https://tinyurl.com/ y7keocwj. If you click the alias, it redirects you to the original URL.
- Candidate: How long is the shortened URL? 
- Interviewer: As short as possible.
- Candidate: What characters are allowed in the shortened URL?
- Interviewer: Shortened URL can be a combination of numbers (0-9) and characters (a-z, A-Z).
- Candidate: Can shortened URLs be deleted or updated?  
  • Interviewer: For simplicity, let us assume shortened URLs cannot be deleted or updated.

  • The link may be expired?

  • Can a customer create a tiny url of his/her choice or will it always be service generated ? If user is allowed to create customer shortened links, what would be the maximum size of custom url ?

  • Yes user can create a tiny url of his/her choice. Assume maximum character limit to be 16.

  • Do we expect service to provide metrics like most visited links ?

  • Yes. Service should also aggregate metrics like number of URL redirections per day and other analytics for targeted advertisements.

  • what if a long URL already had a short URL? what if a long URL already had a short URL, but the expire is different?

Non-Functional Requirements

  • Service should be up and running all the time
  • URL redirection should be fast and should not degrade at any point of time (Even during peak loads)

Volumn

    • Candidate: What is the traffic volume?
    • Interviewer: 100 million URLs are generated per day.
  • assume reading occurs much more than write? and ratio is 10:1

  • Any SLA required? E.g., the SLA of URL redirecting

  • Serve globally? need to deploy services to be close to users?

  • High availability, scalability, and fault tolerance considerations

Estimation

  • Write operation: 100 million URLs are generated per day.
  • Write operation per second: 100 million / 24 /3600 = 1160
  • Read operation: Assuming ratio of read operation to write operation is 10:1, read operation per second: 1160 * 10 = 11,600
  • Assuming the URL shortener service will run for 10 years, this means we must support 100 million * 365 * 10 = 365 billion records.
  • Assume average URL length is 100.
  • Storage requirement over 10 years: 365 billion * 100 bytes * 10 years = 365 TB
  ...


【Architecture】System Design - ID Generator

Scope

  • QPS

  • SLA

  • Required Performance

    • The system should be able to generate 10,000 IDs per second.
  • Do IDs only contain numerical values?

    • Interviewer: Yes, that is correct.
  • What is the ID length requirement?

    • Interviewer: IDs should fit into 64-bit.
  • For each new record, does ID increment by 1?

    • Interviewer: The ID increments by time but not necessarily only increments by 1. IDs created in the evening are larger than those created in the morning on the same day.
  ...


【Architecture】System Design - Chat

Background

Functional Requirements

Candidate: What kind of chat app shall we design? 1 on 1 or group based? Interviewer: It should support both 1 on 1 and group chat.

Candidate: Is this a mobile app? Or a web app? Or both? Interviewer: Both.

Candidate: For group chat, what is the group member limit? Interviewer: A maximum of 100 people

Candidate: What features are important for the chat app? Can it support attachment? Interviewer: 1 on 1 chat, group chat, online indicator. The system only supports text messages.

Non-functional Requirements

Candidate: What is the scale of this app? A startup app or massive scale? Interviewer: It should support 50 million daily active users (DAU).

    • Candidate: Is there a message size limit?
    • Interviewer: Yes, text length should be less than 100,000 characters long.
    • Candidate: Is end-to-end encryption required?
    • Interviewer: Not required for now but we will discuss that if time allows.

Candidate: How long shall we store the chat history? Interviewer: Forever.

  ...


【Architecture】System Design - Consistent Hashing

Background

The rehashing problem

If you have n cache servers, a common way to balance the load is to use the following hash method: serverIndex = hash(key) % N, where N is the size of the server pool.

Let us use an example to illustrate how it works. As shown below, we have 4 servers and 8 string keys with their hashes.

  ...


【Architecture】System Design - News Feed

Background

What is news feed? According to the Facebook help page, “News feed is the constantly updating list of stories in the middle of your home page. News Feed includes status updates, photos, videos, links, app activity, and likes from people, pages, and groups that you follow on Facebook”.

Similar questions commonly asked are: design Facebook news feed, Instagram feed, Twitter timeline, etc.

  ...


【Architecture】System Design

Overall

  1. 理清需求(功能性需求和非功能性需求)
    1. 约束条件,如QPS、用户量、存储量,Page View,Daily Active User
  2. High-level Design
    1. 描述系统中的一些组件和组件之间的交互关系

High-level Design

Template - System Design Details

  • Acknowledgement

    • By other teams
  • Changelog

  • Deployment Sequence

    • Same deployment sequence means they can be deployed together (no strong requirement on the sequence)
  • Introductions

    • Background

      • A little background on the changes
        • why the feature is necessary
        • why do we take this approaches
        • Terminologies
    • Objectives

      • Provide the objective of the services to support the feature/changes
    • Requirements

      • Functional Requirements

      • Non-Functional Requirements

        • 可靠性(Reliability)

        • 安全性(Security)

        • 可扩展性(Scalability)

          • throughput
          • DAU
          • User size
        • 可用性(Availability)

        • 性能(Performance)

          • latency
  • Flows and Logics

  • APIs

    • logic changes
    • highlight the new fields that are added
    • how to use the API
    • potential errors
  • Databases

    • ER Diagram
  • Dependencies

    • API level dependency multiplier
  • Performance

    • QPS analysis and potential bottlenecks. Might not need very accurate numbers here but can have some kind of estimations or justifications why this is not a concern.
    • Can also include performance issues that might not show up now but might be an issue in the future
  • Backward Compatibility

    • Feature Toggle
    • Clean up code
  • Monitoring

  • Downgrade/Rollback Plan

    • E.g., Rate limit, circuit breaker
  • System Limitation

  • Implementation Details

  • Investigations

    • Put the investigation result here
      • Alternatives that were considered but were not chosen due to xxx
      • Supporting data
  • Clean-up Plan

    • If this feature/project, needs any cleanup phase after the release, add the details here. (It can also be in a separate document and add a link to the document here)

      Details including, but are not limited to,

      • Code Clean up details
      • DB Clean up details
      • Config Clean up details
  • Financial Risk

  • Result Analysis

  • Task breakdown

  ...


【Architecture】System Design - Key-value Store

Background

A key-value store, also referred to as a key-value database, is a non-relational database. Each unique identifier is stored as a key with its associated value. This data pairing is known as a “key-value” pair.

In a key-value pair, the key must be unique, and the value associated with the key can be accessed through the key. Keys can be plain text or hashed values. For performance reasons, a short key works better. What do keys look like? Here are a few examples:

  • Plain text key: “last_logged_in_at”
  • Hashed key: 253DDEC4

The value in a key-value pair can be strings, lists, objects, etc. The value is usually treated as an opaque object in key-value stores, such as Amazon dynamo, Memcached, Redis, etc.

  ...