Protocols
TCP
UDP
REST
WebSockets
Long Polling
gRPC
GraphQL
Load Balancer
Type of load balancer
Layer 4
Layer 7
Round Robin
Software
NGinx
HAProxy
CDN
What to store?
Images
CSS
HTML
Javascript
Types of CDN
Push
When it’s upload it’s automatically upload to all the CDNs. It’s good
when you don’t have static
Pull
If a user request a content that the CDN doesn’t have it. It will pull from
server and store it in the CDNs . Lots of static
Cache
Types
Cache Aside
App has access to cache and storage. If it’s in cache return it if not go to
database store it in cache and return it
Read Through
App has only access to the cache. If cache miss go to database update
and return
Write Through
App has only access to cache. When write cache is updated and then
database is
Write Behind
Same as write through with the di erence that the cache will wait for
some secs or when multiple updates are stack
Eviction
Least Recently Used LRU
First items in the list are deleted. When an item is requested is moved at
the end of the list
Least Frequently Used LFU
Every key has a counter which is increment it when is a key hit the
counter of the item is reset to 0
Redis
ff
In memory key value store limited by RAM
TTL time to live how long the key are stored
Queues
Models
Message Queue
Pub / Subs
RabbitMQ
Every queue has a key that represents the topic
Exchange - Load Balancer that receives the messages and put them in the
correct queue
Direct
Just put the message to the queue with that name if there are
multiple consumers use round robin
Topic/Header
This is shared witch means that for example can be divided
because the payment options are di erent for countries. The
name is used to divide the messages
Fan - out Exchange
It acts as a pub/sub
Channels - Consumers use TCP connections that they establish to the queue, to
consume messages faster you can establish multiple tcp connections from di erent threads in
the same service. But TCP connections are expensive instead RabbitMQ has channels. It will
establish one connection but each thread willl get a separate channel. It allows a single
resource to process more messages without overhead
Acknowledgements - When do we need to delete the messages
Automatic
It can acknowledge right away
store the message and then acknowledge
fully process the message and then acknowledge
Explicit
Concurrency
Procesess
Each process has separate memory space
Interprocess communication - how they communicate If they
don’t share memory
File
Signal
Send signal to another process
Socket
8080 open a socket
network sockets browsers
Unix domain faster but limited to the same
machine
Pipe
Standart output of one process become the
standard input of another process
Threads
Di ent threads can run in parallel
Stack
Heap
ff
ff
ff
Process has all his threads in heap
Contention
Threads compite against themselves for resources in the
same process
Locks
CPU time
Shared resources
Thread pool allocatie a speci c number of threads and work with
that, consumers need to wait
Databases
Index - without them it will perform a full table scan searching the entire
table row by row
What type of data structure is an index?
Tree lookup - O(log(N)) b-tree because each parent can
have at most three children
Sharding - smaller databases
When data grows fast, you have one table which is huge and no
database can handle.
Split it into smaller ones. How the application choose which
database to use.
Tenant Base sharding
Country separation for drivers and clients copy the user
Hash based sharding
First we calculate in which shard this entity is located
Partitioning - smaller tables
Strategies
List of values:
Separate rows based on its status, Amazon
example we want to focus on In progress
Range of dates
By range
CAP - Consistency Availability Partition Tolerance
Mostly about NoSQL
Mostly about writes
Consistency - all nodes see the same data
Availability - all nodes are available to write
ACID
Atomicity - Either everything happens in a transaction or nothing
happens at all
Consistency
Uniqness, if it’s wrong your transaction fails
Isolation
While a transaction is committed other users won’t see
the result
Durability
When users commit a transaction is saved to disk
Interview
Assumptions - Describe how the system should work on a user level
Market How many users the system needs to support
How many active users will we have?
How many chats we need to support at the same time?
How many messages per day?
What platforms do we need to support?
fi
Do we need grout chats?