西维蜀黍

【Architecture】System Design - Rate Limiter

Background

If the API request count exceeds the threshold defined by the rate limiter, all the excess calls are blocked. Here are a few examples:

  • A user can write no more than 2 posts per second.
  • You can create a maximum of 10 accounts per day from the same IP address.
  • You can claim rewards no more than 5 times per week from the same device.

The benefits of using an API rate limiter:

  • Prevent resource starvation caused by Denial of Service (DoS) attack. Almost all APIs published by large tech companies enforce some form of rate limiting. For example, Twitter limits the number of tweets to 300 per 3 hours. Google docs APIs have the following default limit: 300 per user per 60 seconds for read requests. A rate limiter prevents DoS attacks, either intentional or unintentional, by blocking the excess calls.
  • Reduce cost. Limiting excess requests means fewer servers and allocating more resources to high priority APIs. Rate limiting is extremely important for companies that use paid third party APIs. For example, you are charged on a per-call basis for the following external APIs: check credit, make a payment, retrieve health records, etc. Limiting the number of calls is essential to reduce costs.
  • Prevent servers from being overloaded. To reduce server load, a rate limiter is used to filter out excess requests caused by bots or users’ misbehavior.

限流(Rate Limit)

Rate limiting is a technique used to control the rate at which requests are made to a network, server, or other resource. It is used to prevent excessive or abusive use of a resource and to ensure that the resource is available to all users.

Rate limiting is often used to protect against denial-of-service (DoS) attacks, which are designed to overwhelm a network or server with a high volume of requests, rendering it unavailable to legitimate users. It can also be used to limit the number of requests made by individual users, to ensure that a resource is not monopolized by a single user or group of users.

Scope

Functional Requirements

- Candidate: What kind of rate limiter are we going to design? Is it a client-side rate limiter or server-side API rate limiter?
- Interviewer: Great question. We focus on the server-side API rate limiter.
- client-side may be bypassed
    • I could assume we use the rate limiter for one API or like different APIs in different services, i.e., the system work in a distributed environment
    • Candidate: Does the rate limiter throttle API requests based on IP, the user ID, or other properties?
    • Interviewer: The rate limiter should be flexible enough to support different sets of throttle rules.
    • Candidate: Do we need to inform users who are throttled? Interviewer: Yes.
  • Let us assume that we don’t wanna integrate the rate limiter into any existing middlewares

    • E.g., for service mesh, we need rate limiting during RPC calls
    • For gateways, normally we need the functionality of the rating limiter as well.
  • Let us assume the scope of authentication is not under this rate limit

  • Do we wanna control the limit in real-time? or just hard-coded?

  ...


【Database】Transactions - Consistent Anomalies - 幻读(Phantom Read)

Phantom Rows

The phantom reads anomaly is a special case of Non-repeatable reads when Transaction 1 repeats a ranged SELECT ... WHERE query and, between both operations, Transaction 2 creates new rows (i.e., INSERT) or deletes existing rows (i.e., DELETE) (in the target table) which fulfill that WHERE clause.

In the diagram above, the flow of statements goes like this:

  1. Alice and Bob start two database transactions.
  2. Bob’s reads all the post_comment records associated with the post row with the identifier value of 1.
  3. Alice adds a new post_comment record which is associated with the post row having the identifier value of 1.
  4. Alice commits her database transaction.
  5. If Bob’s re-reads the post_comment records having the post_id column value equal to 1, he will observe a different version of this result set.

How to aviod Phantom Reads

If the highest level of isolation (i.e., SERIALIZABLE isolation level) were maintained, the same set of rows should be returned both times, and indeed that is what is mandated to occur in a database operating at the SERIALIZABLE isolation level. However, at the lesser isolation levels, a different set of rows may be returned the second time.

In the SERIALIZABLE isolation mode, Query 1 would result in all records with post_id =1 being locked, thus Query 2 would block until the first transaction was committed. In REPEATABLE READ mode, the range would not be locked, allowing the record to be inserted and the second execution of Query 1 to include the new row in its results.

How to prevent:

The 2PL-based Serializable isolation prevents Phantom Reads through the use of predicate locking while MVCC (Multi-Version Concurrency Control) database engines address the Phantom Read anomaly by returning consistent snapshots.

However, a concurrent transaction can still modify the range of records that was read previously. Even if the MVCC database engine introspects the transaction schedule, the outcome is not always the same as a 2PL-based implementation. One such example is when the second transaction issues an insert without reading the same range of records as the first transaction. In this particular use case, some MVCC database engines will not end up rolling back the first transaction.

  ...


【MySQL】设置隔离级别(Isolation Levels)

Show Transaction Isolation Level

check session transaction level (mysql8+)

SELECT @@transaction_ISOLATION;

check global transaction level (mysql8+)

SELECT @@global.transaction_ISOLATION;
  ...


【Database】Transactions - Consistent Anomalies Demo

MySQL Demo

Show Transaction Isolation Level

check session transaction level (mysql8+)

SELECT @@transaction_ISOLATION;

check global transaction level (mysql8+)

SELECT @@global.transaction_ISOLATION;
  ...


【Concurrent Control】MVCC

Multiversion concurrency control (MVCC)

这里的版本号可以任何属性,只要当一次数据修改操作被执行后,这个属性一定会被改变即可,比如数据被修改的次数、版本号、时间戳(timestamp)。

  ...


【macOS】搜索不出来存在的文件

  ...


【Python】pip 安装 numpy 失败

Context

$ pip3 install numpy
===>  _PYTHON_HOST_PLATFORM=x86_64-pc-linux-gnu /home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/cross/bin/pip wheel --disable-pip-version-check --no-binary :all: --cache-dir /home/spksrc/py310.6/spksrc/spk/bazarr/../../distrib/pip --no-deps --wheel-dir /home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/wheelhouse --no-build-isolation greenlet==1.1.2
Collecting greenlet==1.1.2
  Using cached greenlet-1.1.2.tar.gz (91 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Building wheels for collected packages: greenlet
  Building wheel for greenlet (setup.py): started
  Building wheel for greenlet (setup.py): finished with status 'done'
  Created wheel for greenlet: filename=greenlet-1.1.2-cp310-cp310-linux_x86_64.whl size=142558 sha256=d3ac376cd9b322f76ee60208a103be9f52d989d3b3a8a1185fb9857c4a28de25
  Stored in directory: /home/spksrc/py310.6/spksrc/distrib/pip/wheels/51/01/b7/16995feb30f4eed2504aed2260a4058a1164472c9be5ab44b1
Successfully built greenlet
make[3]: Leaving directory '/home/spksrc/py310.6/spksrc/spk/bazarr'
===>  [numpy]           
make[3]: Entering directory '/home/spksrc/py310.6/spksrc/spk/bazarr'
===>  _PYTHON_HOST_PLATFORM=x86_64-pc-linux-gnu /home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/cross/bin/pip wheel --disable-pip-version-check --no-binary :all: --cache-dir /home/spksrc/py310.6/spksrc/spk/bazarr/../../distrib/pip --no-deps --wheel-dir /home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/wheelhouse --no-build-isolation numpy==1.22.4
Collecting numpy==1.22.4
  Using cached numpy-1.22.4.zip (11.5 MB)
  Preparing metadata (pyproject.toml): started
  Preparing metadata (pyproject.toml): finished with status 'error'
  error: subprocess-exited-with-error
  
  × Preparing metadata (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [75 lines of output]
      Running from numpy source directory.
      running dist_info
      running build_src
      INFO: build_src
      creating /tmp/pip-modern-metadata-8a5sb6si/numpy.egg-info
      writing /tmp/pip-modern-metadata-8a5sb6si/numpy.egg-info/PKG-INFO
      writing dependency_links to /tmp/pip-modern-metadata-8a5sb6si/numpy.egg-info/dependency_links.txt
      writing entry points to /tmp/pip-modern-metadata-8a5sb6si/numpy.egg-info/entry_points.txt
      writing top-level names to /tmp/pip-modern-metadata-8a5sb6si/numpy.egg-info/top_level.txt
      writing manifest file '/tmp/pip-modern-metadata-8a5sb6si/numpy.egg-info/SOURCES.txt'
      /home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/command/egg_info.py:643: SetuptoolsDeprecationWarning: Custom 'build_py' does not implement 'get_data_files_without_manifest'.
      Please extend command classes from setuptools instead of distutils.
        warnings.warn(
      INFO: unifing config_cc, config, build_clib, build_ext, build commands --compiler options
      INFO: unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
      Traceback (most recent call last):
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 363, in <module>
          main()
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 345, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 164, in prepare_metadata_for_build_wheel
          return hook(metadata_directory, config_settings)
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/build_meta.py", line 369, in prepare_metadata_for_build_wheel
          self.run_setup()
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/build_meta.py", line 474, in run_setup
          super(_BuildMetaLegacyBackend,
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/build_meta.py", line 334, in run_setup
          exec(code, locals())
        File "<string>", line 461, in <module>
        File "<string>", line 453, in setup_package
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/__init__.py", line 87, in setup
          return distutils.core.setup(**attrs)
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 185, in setup
          return run_commands(dist)
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 201, in run_commands
          dist.run_commands()
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 973, in run_commands
          self.run_command(cmd)
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/dist.py", line 1217, in run_command
          super().run_command(command)
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 992, in run_command
          cmd_obj.run()
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/command/dist_info.py", line 96, in run
          self.egg_info.run()
        File "/tmp/pip-wheel-zx645048/numpy_62d5a1a8355144f781d4c843ede91f36/numpy/distutils/command/egg_info.py", line 25, in run
          _egg_info.run(self)
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/command/egg_info.py", line 308, in run
          self.find_sources()
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/command/egg_info.py", line 316, in find_sources
          mm.run()
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/command/egg_info.py", line 560, in run
          self.add_defaults()
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/command/egg_info.py", line 597, in add_defaults
          sdist.add_defaults(self)
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/command/sdist.py", line 107, in add_defaults
          self._add_defaults_build_sub_commands()
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/command/sdist.py", line 127, in _add_defaults_build_sub_commands
          self.filelist.extend(chain.from_iterable(files))
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/command/egg_info.py", line 503, in extend
          self.files.extend(filter(self._safe_path, paths))
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/command/sdist.py", line 126, in <genexpr>
          files = (c.get_source_files() for c in cmds if hasattr(c, "get_source_files"))
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/command/sdist.py", line 125, in <genexpr>
          cmds = (self.get_finalized_command(c) for c in missing_cmds)
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/_distutils/cmd.py", line 306, in get_finalized_command
          cmd_obj.ensure_finalized()
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/_distutils/cmd.py", line 109, in ensure_finalized
          self.finalize_options()
        File "/tmp/pip-wheel-zx645048/numpy_62d5a1a8355144f781d4c843ede91f36/numpy/distutils/command/config_compiler.py", line 69, in finalize_options
          v = getattr(c, a)
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/Cython/Distutils/old_build_ext.py", line 157, in __getattr__
          return _build_ext.build_ext.__getattr__(self, name)
        File "/home/spksrc/py310.6/spksrc/spk/bazarr/work-x64-6.1/crossenv/build/lib/python3.10/site-packages/setuptools/_distutils/cmd.py", line 105, in __getattr__
          raise AttributeError(attr)
      AttributeError: fcompiler. Did you mean: 'compiler'?
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
make[3]: *** [../../mk/spksrc.wheel.mk:139: cross-compile-wheel-numpy] Error 1
make[3]: Leaving directory '/home/spksrc/py310.6/spksrc/spk/bazarr'
make[2]: *** [../../mk/spksrc.wheel.mk:81: build_wheel_target] Error 1
make[2]: Leaving directory '/home/spksrc/py310.6/spksrc/spk/bazarr'
make[1]: *** [../../mk/spksrc.spk.mk:603: build-arch-x64-6.1] Error 1
make[1]: Leaving directory '/home/spksrc/py310.6/spksrc/spk/bazarr'
make: *** [../../mk/spksrc.spk.mk:597: arch-x64-6.1] Error 2
  ...


【Git】设置代理

Via .gitconfig

Globally

So, for example if your proxy server configuration is as follows:

Server: myproxyserver
Port: 8080
Username: mydomain\myusername
Password: mypassword
  ...


【Golang】泛型(Generics)

  ...


【MySQL】Logs - Undo Logs

Undo Logs

An undo log is a collection of undo log records associated with a single read-write transaction. An undo log record contains information about how to undo the latest change by a transaction to a clustered index record. If another transaction needs to see the original data as part of a consistent read operation, the unmodified data is retrieved from undo log records. Undo logs exist within undo log segments, which are contained within rollback segments. Rollback segments reside in undo tablespaces and in the global temporary tablespace.

Undo logs that reside in the global temporary tablespace are used for transactions that modify data in user-defined temporary tables. These undo logs are not redo-logged, as they are not required for crash recovery. They are used only for rollback while the server is running. This type of undo log benefits performance by avoiding redo logging I/O.

  ...