• Devops


compare to memcached

  • support persistant volume
    • RDB
    • AOF
  • support multiple data types
  • pub/sub


  • redis-cli: command line interface
  • redis-sentinel: cluster managing tool
  • redis-server: run server
  • redis-benchmark: stress testing
  • redis-check-aof: check AOF
  • redis-check-dump: check RDB


Use redis.conf. Docker official redis image not contain this file. Mount it yourself or through redis-server arguments.


  • String: get, set, mget, mset
  • Integer: incr, decr, setbit
  • List: lpush, lrange, lpop
  • Hash Map: hset, hget, hmset, hmget
  • Set: sadd, smember, sdiff, sinter, sunion

use docker

Before start

To connect a container, you need to know the name and the port, in the associated networks to be able to discover the service.

There is no DNS resolution in docker deault bridge network. In default network, you need to specify --link to connect the containers. The --link is a legacy feature.

Therefore, create a user-defined network is recommanded, it provide automatic DNS resolution.

Create a bridge newrok

Run a redis instance in user-defined network

Run a redis-cli connect to the redis instance


all commands are executed as a single isolated operation, serialized and executed sequentially
atomic: all failed or all succeed

  • MULTI: open a transaction and always return OK
  • EXEC: execute commands in transaction
  • DISCARD: flush commands and exit transaction
  • WATCH: check and set, if watched key changes, not execute


  • before EXEC: e.g. syntax error
  • after EXEC: e.g. value error

The pipeline discarding the transaction automatically if there was an error during the command queueing

… To be continued


  • Python


  • A type of iterator
  • generator function: function that uses yield statement
  • implement the iterator protocal, call next
  • raise StopIteration exhausted

Less code

Implement an iterator

Implement a generator

More efficient

Generator Comprehensions

  • local scope
  • lazy evaluation
  • is an iterator, can be exhausted

Delegating Generator

Use the syntax yield from to yield items in a generator


  • Devops


Store and retrieve data in memory(not persistent) base on specific hash function.


  • Slab: allocate as many pages as the ones available

  • Page: a memory area of default 1MB which contains as many chunks

  • Chunk: minimum allocated space for a single item

  • LRU: least recently used list

ref: Journey to the centre of memcached

we could say that we would run out of memory when all the available pages are allocated to slabs

memcached is designed to evict old/unused items in order to store new ones

every item operation (get, set, update or remove) requires the item in question to be locked

memcached only tries to remove the first 5 items of the LRU — after that it simply gives up and answers with OOM (out of memory)

Read More »Memcached

Iterable and Iterator

  • Python

Iterator & Iterable


  • get next item (__next__)
  • no indexes needed (Don’t need to be Sequence type)
  • consumable


  • collections that implement iterator


Python need to count on certain funcionality: __next____iter__StopIteration

compare to sequence type

iteration can be more general than sequential indexing, we only need:

  • a bucket of items: collection, container
  • a way to get the next item, no need to care about ordering
  • an exception to raise if there is no next item

try to custom an iterator ourselfs:

Why re-create?

Seperate the Collection from the iterator

Iterable object

  • Maintaining the data of the collection is one object
  • Created once
  • implements __iter__, return a new iterator instance

Iterator object

  • Iterating over that data should be another object
  • throw away the iterator but don’t throw away the collection
  • Created every time
  • implements __iter__, return itself
  • implements __next__, return next item

iterable can be lazy

Caculate the next itme in an iterable until it’s actually requested

lazy evaluation

  • often used in class properties
  • properties of classes may not always populated when the object is created
  • value of property only becomes known when the property is requested/deferred

infnite iterables

  • itertools.cycle

Python Built-ins

  • range: return iterable
  • zip: return iterator
  • enumerate: return iterator
  • open: return iterator
  • reversed: return iterator

The type is important. Iterator object can be only iter over once.


when iter is called:

  • Python first looks for __iter__, if not then:
  • look for __getitem__ and create an iterator, if not then:
  • raise TypeError

Test it:

The __iter__ must return an iterator!

Iterating callable

iterator delegation

Example 1

Example 2

IP Subnetting

  • Devops

Something you need to know first: Binary Odometer + 1 = + 1 = + 1 =

in reverse: – 1 =

Example 1 or with the mask

Binary Method

image alt

Quick Method

Figure out the subnets:

  1. network and host split in third octect
  2. subtract: 256 – 240 = 16, it means that network are incrementing in values of 16: 0, 16, 32, 48…
  3. 35 in the range of 32 and 48, so is on subnet; next subnet is

First subnet =

Next subnet =

Broadcast address = next subnet – 1

First host = Subnet + 1

Last host = Broadcast – 1


  • Class A subnetting ( support 1677214 (2^24) host per network, that way too much
  • Class B subnetting ( support 16382 (2^16) host per network, that way too much
  • Class C subnetting ( support 254 (2^8) host, more likely we subnet down to at least 254 hosts or even further

If you subnetting a network only has 2 hosts, you can subnet with ( or CIDR as /31

Network, host number

  • Networks: 2^(network bits)
    • one allocate for the subnet
    • one allocate for the broadcast
  • Hosts: 2^(host bits) – 2

Subnetting to be short

  1. “stealing” or “taking away” bits from the host portion of an address, and
  2. allocating those bits to network portion

Example 2

Origin network need at least 30 subnets as many hosts as possible


  1. draw the line with /18 to split network and host
  2. 2^5 > 30, need 5 subnet bit, draw the line to split subnet and host
  3. network/subnet portion is 8+8+7=23 bits, host portion is 32-23=9 bits
  • First subnet:
  • Second subnet:
  • Last subnet:





18.12.07 確定接下案子

在這之前,第一次和負責的團隊成員——很年輕的法律系學生 @Eddy 見面,一開始當然不知道呱吉會打算怎麼做這個系統,原本以為會有前輩主導開發,去參與討論或插花即可,但實際上是最多找2-3個人做;也以為是要做能讓多個議員註冊、使用的平台,但需求聽起來卻像呱吉個人的宣傳式網站

我認知到一件事,那就是 Eddy 沒辦法準確評估我的技術水準,只知道我有做過網站。令人訝異的是,當天握手之後就直接把任務交給我了。當然,這是風險評估出了問題呢,還是完全信任夥伴?我選擇相信後者。(團隊的風氣感覺是年輕人放手去做就對了,烙賽沒關係?)


  • 我希望這是一個開源專案,如果要開源,會有資安上須考量的風險,加上呱吉是公眾人物,容易成為箭靶
  • 短期會有高流量,分散式部署是必要的;自己沒有實際維運過分散系統,大概知道是怎麼做,但沒把握
  • 重點是前端視覺與使用者體驗,但自己頂多做後端,前端雖然會寫,但根本在標準之下

Read More »第一次開發選服就烙賽