Author Archives: lipeng


Each segment has multiple entries. Each entry, it starts with a LinkedList. When it has too many, it changes to Black-Red-Tree.

When write, if two writes are for different segment, then it allows. When multiple writes to same segment, it uses CAS(compare-and-set) to write and handle conflict.

Each operation on segment has a counter. The counter only increases. When resize, it will get the counter first. Then it does resize. After resize, compare if the counter changes. If no change, then resize done, if not will do it again. If still fails, it tries to acquire lock on each segment.


ReentrantLock, ReentrantReadWriteLock

RentrantLock is almost the same as synchronized keyword. Only difference is that synchrnozied keyword needs a block. It maintains a queue for the threads.

RentrantLock lock = new ReentrantLock();

try {
    do something..
} finally {

ReentrantReadWriteLock. It also maintains a queue for threads. When it has queue:
[readThread1, readThread2, writeThread3, readThread4]

When it is released, readThread1, readThread2 can read the resource concurrently. However, readThread4 will be blocked, until writeThread3 finishes the write.

ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
ReentrantReadWriteLock.ReadLock readLock = lock.readLock();
ReentrantReadWriteLock.WritLock writeLock = lock.writeLock();

private void readResource() {
    // read

private void writeResource() {
    // write


max, min




label vs no label vs “_”

Below are the ways passing variables

func myFunc(label name: Int, lable name2: Int, age: Int, _ value: String) {






myFunc(label: 4, lable: 3, age: 5, “aa”)

default, no value

var a: String?

In this case, a is a String type with nil as value.

struct vs class

struct is value type. class is reference type like in Java.

Array, String, Dictionary are all value types. class supports inheritance.

struct Resolution {
    var height = 0

var s = Resolution()
var ss = s
ss.height = 1

print(s.height) // 0
print(ss.height) // 1
class Resolution {
    var height = 0

var s = Resolution()
var ss = s
ss.height = 1

print(s.height) // 1
print(ss.height) // 1


var arr = ["a", "b", "c"]
for i in 0...2 {
var arr = ["a", "b", "c"]

for (idx, ele) in arr.enumerated() {
    print(String(idx) + ele)
var a = 1
while (true) {

struct is value type. class is reference type like in Java.


manifest, in kubernetes, it is used to create/modify resources, such as pod. Normally it is .yaml file. For example kubectl apply -f my-file.yaml
artifact, a deployable resource. Could be docker image, git repo, github file, http file, s3 object etc
chart, a combined K8S yaml manifests
, is a tool. Similar to yum, apt in

Category: web

aws profile

➜ GettingStarted git:(master) ✗ aws configure –profile “sandbox”
AWS Access Key ID [None]:
AWS Secret Access Key [None]:
Default region name [None]: us-west-2
Default output format [None]:

➜ GettingStarted git:(master) ✗ aws s3 ls s3://xxxx-bucket –profile sandbox

Category: aws

How Kinesis getShardIterator works

getShardIterator, by its name, it gets the iterator for the shard.

--stream-name <value>
--shard-id <value>
--shard-iterator-type <value>
[--starting-sequence-number <value>]

Let’s say we use getShardIterator, we got iterator on shard2, seq2_0. Then we use this iterator to iterate message in Kinesis shard2 from seq0_0. After one iteration, it returns next iterator which points to seq2_1.

If we want to iterator message in all shards, we need to generate the iterators on each shard.


Another caveat is that the neighbor sequence numbers for neighbor message in one shard is not sequential order. For example, it’s not the case that first message sequence number is 2355, then next message sequence number is 2366.

In this case, we can understand why in Kinesis when we talk about “lag” concept in Kafka, it only talks about millisbehindlatest instead of sequence number.

Category: aws

Thought about leaderboard

First of all, we can have below schema for the players and its score:

contest_id(partitionKey), user_id1(sortKey), problem1_finish_time, problem2_finish_time, score(secondaryIndex)

For the score and user_id, we can build a BST. Let’s say we want to find the players who ranks between 50-60. We can find the 50th player in O(logN) time. Check this leetcode problem. Then, we can do extra 10 range scan based on the searched element.

Implementation can be done with redis ZADD, ZRANGE

Below is the command we> zadd players 400 d
(integer) 1> zadd players 500 e
(integer) 1

Here is a full list of the result with scores:> zrange players 0 -1 withscores
1) “b”
2) “100”
3) “a”
4) “200”
5) “c”
6) “300”
7) “d”
8) “400”
9) “e”
10) “500”

By running ZRANGE board_name from to, it returns the result:> zrange players 2 4
1) "c"
2) "d"
3) "e"

Check this post