Apollo GraphQL Fetch Policy

How you want your component to interact with the Apollo cache. Defaults to “cache-first”.

There are many options to this: “cache-first” | “cache-and-network” | “network-only” | “cache-only” | “no-cache” | “standby”

 const { loading, error, data } = useQuery(QUERY, {
    variables: queryVariables,
    fetchPolicy: 'no-cache'

Multilevel Index

Suppose we have an index on id column for a table with 10 million rows. The index will eventually become too large that can’t be fit in memory, so we would have to store it somewhere in disk blocks.

The search for an entry in the index would require several disk blocks access. One way to improve this is to use multilevel index. We treat the index just as a database table, and construct another index (outer index) on this index (inner index).

We can repeat this process as many times as needed, until the outer index has small size that can be kept in memory. This would reduce the number of disk accesses significantly.


References: https://www.cs.uct.ac.za/mit_notes/database/htmls/chp11.html#multilevel-indexes

Dictionary Attack

Dictionary attack is a technique used to breach a password-protected system.

This method attempts to guess the correct password by systematically entering each word in a dictionary. This works because many users use normal words in dictionary as their passwords.

There are several ways to prevent this:

  • Account Locking: Lock the account after several unsuccessful attempts
  • Or in the encryption algorithm, the attack can be deterred by adding extra random bits to the end of the value before encryption, such extra bits is referred to as initialization vector, or salt
  • ……….

How to start and stop PostgreSQL server?

1. On MacOS:

If you installed PostgreSQL via Homebrew:

  • To start manually: pg_ctl -D /usr/local/var/postgres start
  • To stop manually: pg_ctl -D /usr/local/var/postgres stop
  • To start PostgreSQL server now and relaunch at login:
    brew services start postgresql
  • And stop PostgreSQL: brew services stop postgresql

2. On Windows:

First, you need to find the PostgreSQL database directory, it can be something like C:\Program Files\PostgreSQL\10.4\data. Then open Command Prompt and execute this command:

  • To start the server: pg_ctl -D "C:\Program Files\PostgreSQL\9.6\data" start
  • To stop the server: pg_ctl -D "C:\Program Files\PostgreSQL\9.6\data" stop
  • To restart the server: pg_ctl -D "C:\Program Files\PostgreSQL\9.6\data" restart

3. On Linux:

  • Start the PostgreSQL server: sudo service postgresql start
  • Stop the PostgreSQL server: sudo service postgresql stop

Link reference:


Comparison Time Attacks

Using == to compare sensitive hashes leaves you vulnerable to timing attacks. This is because == returns false as soon as it finds two characters that don’t match. An attacker can make many requests with different values and compare times to figure out how many characters were correct (the shorter the response, the fewer correct characters).

Solution: use a constant-time comparison algorithm.

  • Ruby: Rack::Utils.secure_compare or ActiveSupport::SecurityUtils.secure_compare
  • NodeJS: crypto.timingSafeEqual


Memory layout of OS processes


  • Text: The executable code (source code compiled to binary)
  • Data: initialized or uninitialized data
  • Heap: Dynamic allocated memory during program run time
  • Stack: Temporary data storage when invoking functions (local variables, function parameters, return addresses)

The sizes of text and data are fixed.

However, heap and stack can grow and shrink during run time. Each time a function is called, a record contains local variables, function parameters, return addresses is pushed to the stack. When the function finished, that record will be popped from the stack.

Similarly, the heap will grow as memory is dynamically allocated, and shrink when the memory is returned to the system. Notice that stack and heap size can grow toward each other but the OS has to make sure that they won’t overlap one another.


COPY results will be faster than INSERT due to they don’t have to do round trip, PostgreSQL doesn’t have to do planning and executing multiple INSERT statements.

COPY table_name [ ( column_name [, ...] ) ]
    FROM { 'filename' | STDIN }
    [ [ WITH ] ( option [, ...] ) ]

COPY { table_name [ ( column_name [, ...] ) ] | ( query ) }
    TO { 'filename' | STDOUT }
    [ [ WITH ] ( option [, ...] ) ]

where option can be one of:

    FORMAT format_name
    OIDS [ boolean ]
    DELIMITER 'delimiter_character'
    NULL 'null_string'
    HEADER [ boolean ]
    QUOTE 'quote_character'
    ESCAPE 'escape_character'
    FORCE_QUOTE { ( column_name [, ...] ) | * }
    FORCE_NOT_NULL ( column_name [, ...] )
    ENCODING 'encoding_name'

Smoother & sharper shadows in CSS

Default box-shadow

.box {
 box-shadow: 0 3px 3px rgba(0,0,0,0.2);

Create smoother box-shadows by layering multiple

.shadow-5 {
 box-shadow:  0 1px 1px rgba(0,0,0,0.12), 
              0 2px 2px rgba(0,0,0,0.12), 
              0 4px 4px rgba(0,0,0,0.12), 
              0 8px 8px rgba(0,0,0,0.12),
              0 16px 16px rgba(0,0,0,0.12);

Shadows with decreasing alpha

.blog-shadow-sharp {
 box-shadow: 0 1px 1px rgba(0,0,0,0.25), 
             0 2px 2px rgba(0,0,0,0.20), 
             0 4px 4px rgba(0,0,0,0.15), 
             0 8px 8px rgba(0,0,0,0.10),
             0 16px 16px rgba(0,0,0,0.05);


Elixir ETS table

ETS tables are in-memory storage that stores tuples as rows.

ETS is a common way of sharing states between processes/GenServers. But keep in mind that ETS is a datastore, not a database. So the support for transactions is very limited.

Working with ETS tables is done via erlang :ets module

Reference: link